question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dataloader and N+1 Issue

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

1reaction
filipeximenescommented, Oct 10, 2018

Here is how I got it working:

def get_sorter(ids):
    def order_by(items):
        items_dict = {i.id: i for i in items}
        return [items_dict[i] for i in ids]
    return order_by

class CompanyNode(DjangoObjectType):

    yeardata = DjangoConnectionField(
        YearDataNode, 
        filter1=graphene.String(),
        filter2=graphene.DateTime(),
    )

    class Meta:
        model = CompanyModel
        interfaces = (relay.Node, )

    def resolve_yeardata(self, info, *args, **kwargs):
        ids = YearDataFilter(
            kwargs, YearDataModel.objects.all()).qs.values_list('id', flat=True)
        return (year_data_loader
                .load_many(ids)
                .then(get_sorter(id)))

Not perfect nor the most optimized, but should be enough for a temporary solution

0reactions
stale[bot]commented, Jun 11, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found