Using with GraphQL
Gybson is highly suited to building GraphQL apps. It uses DataLoader under the hood to batch and cache database requests.
This allows for deep GraphQL trees to be resolved without an explosion in database request whilst still resolving each field independently.
Other solutions such as eager loading from the root of the tree work for simple apps but are massively limiting in complex data graphs.
See the DataLoader docs for more details on query efficiency in GraphQL.
Apollo example
Add a gybson instance to the context.
important
Make sure to add a new client instance to the context on every request. This will ensure the cache is clean and no stale data is returned. See DataLoader for more details
Then in your resolvers:
Executing the following query results in just 3 database round-trips regardless of how many posts or comments are loaded.