Resolver_Hints root None
Closed this issue · 4 comments
bobo-le commented
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:
The response tells the following:
"NoneType' object has no attribute 'gql_language_code_de'"
As you see, the root variable is None:
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?
bobo-le commented
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)
bobo-le commented
Thank you very much!