Single Query or Multiple Query | Community
Skip to main content
Level 2
November 22, 2024
Solved

Single Query or Multiple Query

  • November 22, 2024
  • 2 replies
  • 665 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Madhur-Madan

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

2 replies

Madhur-Madan
Community Advisor
Madhur-MadanCommunity AdvisorAccepted solution
Community Advisor
November 22, 2024

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

gkalyan
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 22, 2024

@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-headless/graphql/advanced-tutorial/graphql-persisted-queries