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.

DjangoFilterConnectionField with DataLoader

See original GitHub issue

Consider a query like this:

{
    category(id: 1) {
        products {
            edges {
                node {
                    category {
                        products {
                            edges {
                                node {
                                    category {
                                        products {
                                            edges {
                                                node {
                                                    id
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

If someone runs a query like this we are possibly executing hundreds of queries even if the response contains a fairly small number of unique objects. We can use a DataLoader for the product.category relation but as category.products is a DjangoFilterConnectionField I see no way to have it memorize results within the same execution context.

Ideally I’d want it to only query the database once for each unique combination of category.id and input parameters (pagination, filterset data). Currently it will fetch the category, then fetch all its products, then for each result once again fetch all products in the category and then once again fetch all products in the category for each of those.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
ulgenscommented, May 12, 2020

I think the issue is still relevant. Opening it back.

2reactions
AndrewIngramcommented, May 24, 2018

I’ve hit this same requirement myself, so i’ll see if I can port over my method from Node. The “perfect” solution is variant of Dataloader that’s aware of the semantics of connections.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to combine DjangoObjectType and an additional field in ...
class Query(ObjectType): all_data = DjangoFilterConnectionField( ExampleType, ... How to return two variables from promise DataLoader?
Read more >
Dataloader - Graphene-Python
DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API...
Read more >
graphene Changelog - pyup.io
Graphene now includes an updated version of `aiodataloader` by Syrus Akbary under `graphene.utils.dataloader` due to inactivity of the old repository.
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 >
graphene: Versions - Openbase
Further contributions and updates to dataloader in this repo are welcome! ... support via DjangoFilterConnectionField and filter_fields attr in Django Meta.
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