Expand my Community achievements bar.

SOLVED

Single Query or Multiple Query

Avatar

Level 2

Is it a good practice to combine unrelated models in a single GraphQL query in AEM, or should I create separate queries for each model?
Could you please share some insights on this

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

When dealing with unrelated models in AEM, it's generally better to use multiple queries. This approach keeps things simple, avoids over-fetching data, and offers better flexibility for performance optimization and caching.

However, if the data from these models is needed together on the same page (and combining them in a single query helps reduce network overhead), then a single query might be justified. But, in most cases where the models are unrelated or used in different contexts, multiple queries are the cleaner and more maintainable choice.

Single Query (Combining Models)
Pros:

  1. Reduced Network Requests: A single query eliminates the need for multiple network calls, potentially speeding up data retrieval when all models are needed together.
  2. Simplified Client Code: Handling one response is less complex than managing multiple responses, especially when the data is displayed together on the same page.

Cons

  1. Over-fetching: You may retrieve unnecessary fields from models you don't need, increasing data transfer size and processing time.
  2. Query Complexity: As the number of models grows, the query can become complex, making it harder to maintain and debug.

Multiple Queries (Separate Requests for Each Model)

Pros

  1. Granular Control: You can fetch only the necessary data for each model, reducing over-fetching and improving efficiency.
  2. Better Performance: Separating queries allows for better optimization and caching for each model independently, which can improve performance for large datasets.

Cons

  1. Increased Network Requests: More queries mean more network round trips, which could introduce latency if many queries are made.
  2. Complex Client Logic: Managing multiple asynchronous queries and synchronizing their responses can make the frontend code more complicated.

 

Thanks and Regards,
Madhur

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

When dealing with unrelated models in AEM, it's generally better to use multiple queries. This approach keeps things simple, avoids over-fetching data, and offers better flexibility for performance optimization and caching.

However, if the data from these models is needed together on the same page (and combining them in a single query helps reduce network overhead), then a single query might be justified. But, in most cases where the models are unrelated or used in different contexts, multiple queries are the cleaner and more maintainable choice.

Single Query (Combining Models)
Pros:

  1. Reduced Network Requests: A single query eliminates the need for multiple network calls, potentially speeding up data retrieval when all models are needed together.
  2. Simplified Client Code: Handling one response is less complex than managing multiple responses, especially when the data is displayed together on the same page.

Cons

  1. Over-fetching: You may retrieve unnecessary fields from models you don't need, increasing data transfer size and processing time.
  2. Query Complexity: As the number of models grows, the query can become complex, making it harder to maintain and debug.

Multiple Queries (Separate Requests for Each Model)

Pros

  1. Granular Control: You can fetch only the necessary data for each model, reducing over-fetching and improving efficiency.
  2. Better Performance: Separating queries allows for better optimization and caching for each model independently, which can improve performance for large datasets.

Cons

  1. Increased Network Requests: More queries mean more network round trips, which could introduce latency if many queries are made.
  2. Complex Client Logic: Managing multiple asynchronous queries and synchronizing their responses can make the frontend code more complicated.

 

Thanks and Regards,
Madhur

Avatar

Community Advisor

@ShamliKh Combining queries on different models is great for performance but comes at a cost of readbility. 

 

The usecase and functionality you want to achieve by doing this drives which path you choose.

 

If models doesn't talk to each other, it is better to have seprated queries for redability,  maintance when you scale. You can still achieve performace using peristed queries.

 

See this documentation from Adobe https://experienceleague.adobe.com/en/docs/experience-manager-learn/getting-started-with-aem-headles...