graphene-django icon indicating copy to clipboard operation
graphene-django copied to clipboard

DjangoFilterConnectionField with DataLoader

Open patrys opened this issue 6 years ago • 8 comments

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.

patrys avatar May 08 '18 15:05 patrys

Also curious

camflan avatar May 15 '18 16:05 camflan

I've done this with graphql-js and dataloader, I just treated all the connection arguments as the "id" for the loader. I did bolt on some cleverer logic, but this was the crux of how I solved this.

AndrewIngram avatar May 21 '18 22:05 AndrewIngram

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.

AndrewIngram avatar May 24 '18 09:05 AndrewIngram

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.

stale[bot] avatar Jun 11 '19 12:06 stale[bot]

@AndrewIngram just curious if you were able to get this working with grapnehe-django? Working on the same problem at the moment

Vitiell0 avatar May 11 '20 22:05 Vitiell0

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

ulgens avatar May 12 '20 03:05 ulgens

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Aug 27 '20 00:08 stale[bot]

Bump

pfcodes avatar Dec 30 '23 01:12 pfcodes