Optimize pagination of relay connection fields
See original GitHub issueCopied from https://github.com/tfoxy/graphene-django-optimizer/issues/1#issuecomment-419340157.
@tfoxy Do you use optimizers with any pagination? We’re also using Apollo as the client and we want to provide cursor-based pagination. But we also have queries with nested connections e.g.:
{
products(first: 10) {
edges {
node {
name
variants(first: 5) {
edges {
node {
name
}
}
}
}
}
}
}
Using just the ORM I’m able to fetch the data only with two DB queries:
Product.objects.prefetch_related('variants')
I’m trying to achieve the same in the API but unfortunately any pagination seems to break the prefetches as they do slicing on querysets internally which results in refetching objects that were already cached by prefetch_related
.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Relay Connections | Caliban
The field can be paginated forwards by using first (number of items) and after (the current cursor), or backwards by using last (number...
Read more >Pagination - Relay
By default, Relay will automatically append new items to the connection upon completing a pagination request, and will make them available to your...
Read more >Enforce pagination in graphene relay.ConnectionField
ConnectionField implementation that ships with graphene does not by default paginate queries. (The connection can optionally be paginated using ...
Read more >Windowed pagination with Relay - aamir.j
Relay -style pagination is a popular option for paginating graphql queries. ... Connection types must have fields named edges and pageInfo .
Read more >Step by step guide to pagination in GraphQL - Daily.dev
We see that in cursor-based pagination, the pagination is done based on the fields of the record in the database. It is more...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@nwaxiomatic Here is an example usage of optimization hints in my project. There is a
Product
model and each product can have multiple images (1-N relationship). From these images, we choose one as a product thumbnail to display in the UI.Now, if we had a query that fetches first 5 products and a thumbnail for each of them:
Without optimization hints For each product, Django would do a query to fetch all its images and return one as a thumbnail.
With optimization hints
@gql_optimizer.resolver_hints(prefetch_related='images')
tells Graphene to prefetchimages
relation when theresolve_thumbnail_url
resolver is used. As a result, each time the thumbnail field is used, I’m able to take advantage of prefetched images and avoid duplicated database queries.@tfoxy Yes it does, but I still can benefit from prefetching data in one query instead of doing duplicated queries for each row of data.
Anyway, I managed to use prefetching by using optimization hints and the slightly modified version of
DjangoConnectionField
, as suggested in this comment. I’ll keep experimenting with it. For now, I’d consider my question resolved, so you can close the issue.