Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing queries which are part of standard graphene.ObjectType #20

Open
cramshaw opened this issue Apr 30, 2019 · 1 comment
Open

Comments

@cramshaw
Copy link
Contributor

cramshaw commented Apr 30, 2019

Hi! First off thanks for this module, it's really great, and for the release yesterday, I'd just come across the prefetch/select related bug and it was fixed before I could report it!

So I have been playing extensively and want to use this alongside some sort of pagination. Currently I have a query that looks like:

  wi4(page: 2) {
    totalCount
    page
    results {
      idInstance
      image {
         idImage
      }
    }
  }

Where results is a Django queryset.

To make this work I have a resolver which does:

class Query:
    wi_4 = graphene.Field(
        PaginatedInstanceType, page=graphene.Int())

    def resolve_wi_1(self, info, page=1, **kwargs):
        qs = gql_optimizer.query(
            Instance.objects.all(), info)
        return get_paginator(qs, PaginatedInstanceType, page=page)

and the type:

class PaginatedInterface(graphene.Interface):
    page = graphene.Int()
    pages = graphene.Int()
    has_next = graphene.Boolean()
    has_prev = graphene.Boolean()
    total_count = graphene.Int()
    results = None

class PaginatedInstanceType(graphene.ObjectType):
    results = graphene.List(InstanceType)

    class Meta:
        interfaces = (PaginatedInterface,)

Where InstanceType is a standard django-graphene DjangoObjectType.

Unfortunately this seems to result in the optimizer being disregarded and it runs n+1 queries. I've tried the recommended advanced usages but they all seem to rely on the ObjectType being an actual DjangoObjectType, which doesn't work here as I don't have a model at the top level.

I have found that if I 'trick' it by settings the results selection as the main selection, the field_name to results and the parent type to the GraphQLObject of the PaginatedInstanceType (passed in here as object_type) then it all works beautifully.

       results = filter(
            lambda x: x.name.value == 'results', info.field_asts[0].selection_set.selections)
        info.field_asts = list(results)

        info.parent_type = info.schema.get_type(
            str(object_type))

        info.field_name = 'results'

Before I start trying to make any changes, I wanted to check that this isn't currently supported and I haven't just missed how to go about it?

Thanks!

@Instrumedley
Copy link

no updates for this I guess :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants