tfoxy / graphene-django-optimizer

Optimize database access inside graphene queries
MIT License
428 stars 84 forks source link

Resolver_Hints root None #19

Closed bobo-le closed 5 years ago

bobo-le commented 5 years ago

Hey, i am trying to retreive a list of categories and her translations with a custom filter. I tried to use a resolver_hint from the examples: image

My models looks like that: image

My Query is the following: image

The response tells the following: "NoneType' object has no attribute 'gql_language_code_de'"

As you see, the root variable is None: image

Can you tell me whats wrong with my code?

tfoxy commented 5 years ago

root is None at the root of a graph query. The resolver_hints would work if it was applied at the translations resolver. Is there a reason why categoryList receives languageCode instead of being translations the one with the argument?

bobo-le commented 5 years ago

So you mean like this? image

I dont know how i can add the param to the translations field..

Thank you

tfoxy commented 5 years ago

Yes, like that!

In graphene, you can have the following schema:

class CategoryType(DjangoObjectType):
    translations = graphene.List(
        CategoryTranslationType,
        language_code=graphene.String(),
    )

    class Meta:
        model = Category

    @gql_optimizer.resolver_hints(
        prefetch_related=lambda info, language_code: Prefetch(
            'translations',
            queryset=gql_optimizer.query(CategoryTranslation.objects.filter(language_code=language_code), info),
            to_attr='gql_translations_with_language_code_' + language_code,
        ),
    )
    def resolve_translations(root, info, language_code):
        return getattr(root, 'gql_translations_with_language_code_' + language_code)

class Query(object):
    category_list = graphene.List(CategoryType)

    def resolve_category_list(root, info):
        return gql_optimizer.query(Category.objects.all(), info)
bobo-le commented 5 years ago

Thank you very much!