Dataloader and N+1 Issue
See original GitHub issueI have seen various snippets and various solutions to using a dataloader to limit queries to the db but am struggling to piece together a example that works as I expect.
Does anyone have a concrete example or project that implements prefetch_related, select_related, dataloaders etc? I would also like to use the FilterSet class in my resolver that implements a dataloader. How can I do this?
This is what I have implemented so far:
class YearDataLoader(DataLoader):
def batch_load_fn(self, keys):
return Promise.resolve(YearDataModel.objects.filter(pk__in=keys))
class CompanyNode(DjangoObjectType):
yeardata = DjangoFilterConnectionField(YearDataNode, filterset_class=YearDataFilter)
class Meta:
model = CompanyModel
interfaces = (relay.Node, )
def resolve_yeardata(self, info, *args, **kwargs):
return year_data_loader.load_many(YearDataModel.objects.filter(company=self).values_list('id', flat=True))
If I can figure this out then I would gladly write an optimisation guide, as I feel like this is not covered at all in the current docs. All the pieces are there but someone needs to put them all together…
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:5
Top Results From Across the Web
Deeply Understand the GraphQL N+1 Issue and DataLoader
This article dives deep into the N + 1 issue of GraphQL plus the implementation of DataLoader. Finally, to better understand how DataLoader ......
Read more >Solve N+1 query problem in GraphQL with Dataloader
Dataloader is a library that batches consecutive requests and makes a single data request under the hood. This request can be made to...
Read more >Solving N+1 in GraphQL Python With Dataloader - Jerry Ng
Tips and guide to handle N+1 problem with Dataloader in Django Graphene while developing GraphQL API in Python.
Read more >GraphQL/Graphene: How to Solve the N+1 Problem With ...
Instead, there is an “official” solution based on an object called “DataLoader.” Data Loaders. In every GraphQL implementation, resolver ...
Read more >Spring for GraphQL: How to solve the N+1 Problem? - Techdozo
The n+1 problem means that the GraphQL server executes many unnecessary round trips to fetch nested data. If you check the service log...
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 FreeTop 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
Top GitHub Comments
Here is how I got it working:
Not perfect nor the most optimized, but should be enough for a temporary solution
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.