5

In the documentation for LWC, Expose Apex Methods to Lightning Web Components, the note says:

You can’t use an Apex inner class as a parameter or return value for an Apex method that's called by a Lightning web component.

You can’t use the @NamespaceAccessible Apex annotation for an @AuraEnabled Apex method referenced from a Lightning web component. A Lightning web component installed from a package can’t call an Apex method from an Apex class in another package, even if both packages are in the same namespace.

As per documentation below code/pattern is not recommended, but it works so my question is why documentation says not to use it?

LWC:

const requestData = { 
    request: {
        data: 'data from LWC'
    }
};
const result = await getData(requestData);

Apex:

public class MyController() {

    @AuraEnabled
    public static Response getErrors(Request request) {
        // some code
        return new Response('data from LWC');
    }

    public class Request {
        // some properties
    }

    public class Response {
        @AuraEnabled
        public String message { get; set; }

        public Response(String message) {
            this.message = message;
        }
    }
}

Edit: We'd prefer to have the above way of working unless we really have to avoid it, other options are:

  1. Create individual classes instead of inner class
    • Con: Functionality would be scattered across different files and would take more time to read code.
  2. Send a JSON string from LWC and deserialize in apex, and vice versa
    • Con: Feels unnecessary and may have slight impact on performance due to serialize/deserialize at both client and server side.
  3. Send the data as a Map
    • Con: Parsing complex data type is harder and would require more code
11
  • 1
    This is really a question for the doc team. I use this pattern all the time, have never observed an issue. I'll circle back.
    – sfdcfox
    May 4, 2022 at 15:30
  • 1
    Agreed, this used to be some problem specifically with Aura where it throws internal server errors, with LWC we too never had this problem. But due to this statement in documentation developers are sort of forced to create outer class instead of inner classes, that can lead to many classes making readability harder.
    – Raul
    May 4, 2022 at 15:34
  • 1
    Does this answer your question? How to pass Apex type to method from LWC?
    – Phil W
    May 4, 2022 at 15:37
  • 2
    @PhilW In my experience, as long as you always use @AuraEnabled and { get; set; } for every property, things tend to work as they should. I've used this in all of my projects everywhere for years, and never realized the docs specifically warn against it or why. TBH, if it's not supported, the compiler should have complained. Or, even better, it should be fixed, because it's clearly desirable for most developers to want to put their data model and code close to each other.
    – sfdcfox
    May 4, 2022 at 16:00
  • 1
    @Raul Most likely what you will find is that if you put this into a namespaced managed package this might not work as those classes won't be exposed. But I never tried that so hard to tell.
    – zaitsman
    May 5, 2022 at 0:53

0

You must log in to answer this question.

Browse other questions tagged .