tfoxy/graphene-django-optimizer

Resolver_Hints root None

Closed this issue · 4 comments

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

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?

So you mean like this?
image

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

Thank you

tfoxy commented

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)

Thank you very much!