[SalesForce] LWC multiple apex calls on initialization (Best practices question)

PROBLEM:
I have a LWC component which needs to gather data from different objects on initialization event (Objects are: custom object, files and metadata type). This is what I have in place working right now: a LWC component calling the following Apex method which returns a wrapper object and all the data is being processed in LWC after return.

    @AuraEnabled
public static LoanDocumentWrapper initFileComponent(String recordId){
    Loan__c loan = getLoan(recordId);//method call to get record
    List<ContentDocument> loanDocs = Utilities.getAllRelatedFiles(recordId);//get related files to record
    List<Doc__mdt> docs = getDocumentTypes();//get standard docs from metadata

    LoanDocumentWrapper ldw = new LoanDocumentWrapper(loan, loanDocs, docs);//puts everthing in a wrapper class to return

    return ldw;
}

QUESTION:
Is better to use the "design pattern" exposed before, or should I be using async / await and 3 different apex calls?.

COMMENTS: By the way the 3 queries are independent they don't need the result from each other to work.

Thanks

Best Answer

Each approach has its advantages. Generally your single-Apex-call approach will be better for simpler components, while the multiple-Apex-call approach can be useful when components are handling bigger datasets or data that can take longer to retrieve.

The Single-Apex-Call Approach

  • It will be easier to reason about what data you have and what data you don't have client-side at any point in time, avoiding the need for complex if:true attributes in your template and null/undefined checks in your JavaScript. This likely means fewer surprise errors.

The Multiple-Apex-Call Approach

  • Each call to Apex is a separate Apex transaction, which can be advantageous if you find you're getting close to hitting governor limits in any one Apex call.
  • If some of the data you're retrieving in Apex takes longer to get than other data, it may be better to separate the calls so certain parts of the component can render more quickly and give the user the impression of faster load times.
  • If you have multiple Apex calls running in parallel, it's possible that you'll get everything back slightly faster, vs. having a single Apex thread do all the work sequentially.
Related Topic