1. Home
  2. Docs
  3. Advanced Guides
  4. Deferred Resolvers

Deferred Resolvers

One of the most annoying problems with data fetching is a so-called N+1 problem. Consider the following GraphQL Query:

Naive field resolution process would query once for the 10 posts, then make 10 more calls to get the author for each author of each of the 10 posts.

The GraphQL-PHP library under WPGraphQL provides tools to mitigate this problem by allowing you to defer actual field resolution to a later stage when one batched query could be executed instead of 10 distinct queries.

You can see this technique in Action in the plugin here and here.

How does deferred resolving work?

Instead of resolving a field immediately, part of the data will be added to a cache for execution later. 

In the case above, we are querying for a list of posts, and on each post we have a post_author attribute which is the ID of the User that authored the post. 

Instead of querying for the Author immediately, we cache the user ID, and once all posts have been resolved, GraphQL comes back and executes a single WP_User_Query to get all users connected to all of the posts, then returns the data in the field resolver. 

You can read more about how GraphQL-PHP handles this here.

Was this article helpful to you? Yes No

How can we help?