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

Resolvers that uses .get instead of .filter (i.e. returning Model, rather than QuerySet instance) on Query. #61

Open
cansin opened this issue Feb 23, 2021 · 1 comment

Comments

@cansin
Copy link

cansin commented Feb 23, 2021

We are trying to get graphene-django-optimizer to work with our setup. Most of our queries start from a query { me { ... } } root. As such the implementation of the Query is:

class Query(graphene.ObjectType):
    me = graphene.Field(UserNode)

    @classmethod
    def resolve_me(cls, root, info):
        user = info.context.user
        if not user.is_authenticated:
            raise Exception("Authentication credentials were not provided")
        return user

or another example:

class Query(graphene.ObjectType):
    category = graphene.Field(CategoryNode, slug=graphene.String(required=True))

    @classmethod
    def resolve_category(cls, root, info, slug):
        return CategoryDefinition.objects.get(slug=slug)

We are not able to figure out how to get the subtree of such an entry point to optimize. Simply extending UserNode from OptimizedDjangoObjectType does not make any difference in the queries being run. Any idea on how we could utilize graphene-django-optimizer?

@wodCZ
Copy link

wodCZ commented Mar 4, 2021

import graphene_django_optimizer as gql_optimizer

class Query(graphene.ObjectType):
    category = graphene.Field(CategoryNode, slug=graphene.String(required=True))

    @classmethod
    def resolve_category(cls, root, info, slug):
        return gql_optimizer.query(CategoryDefinition.objects.get_queryset().filter(slug=slug), info).first()

You should wrap the queryset in the query method, then call get (or first, I believe get didn't work as expected) on the result.

(Also see related discussion in #46)

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