Skip to content

TypeError: How to resolve fields with ListField(GenericReferenceField())? #227

Open
@7homasSutter

Description

@7homasSutter

I'm attempting to use GenericReferenceField or GenericLazyReferenceField fields in combination with ListField. However, it seems that the fields cannot be resolved correctly. I have the following model and graphene schema defined:

Model:

from mongoengine import ListField, Document, GenericReferenceField
   
class AndroidApp(Document):
    ...
    generic_field_list = ListField(GenericReferenceField())
    ...

Schema:

import graphene
from graphene.relay import Node
from graphene_mongo import MongoengineObjectType
from graphql_jwt.decorators import superuser_required
from model import AndroidApp

class AndroidAppType(MongoengineObjectType):
    class Meta:
        model = AndroidApp
        interfaces = (Node,)

class AndroidAppQuery(graphene.ObjectType):
    android_app_list = graphene.List(AndroidAppType,
                                     object_id_list=graphene.List(graphene.String),
                                     name="android_app_list")

    def resolve_android_app_list(self, info, object_id_list):
        return AndroidApp.objects(pk__in=object_id_list)

Whenever I add the ListField(GenericReferenceField()) the following stack trace occurs which says that the field cannot be resolved. A ListField in combination with LazyReferenceField works with the same model but not with the GenericLazyReferenceField or GenericReferenceField classes.

Stacktrace:

File "/usr/local/lib/python3.9/site-packages/graphene/types/schema.py", line 440, in __init__
     self.graphql_schema = GraphQLSchema(
File "/usr/local/lib/python3.9/site-packages/graphql/type/schema.py", line 224, in __init__
    collect_referenced_types(query)
File "/usr/local/lib/python3.9/site-packages/graphql/type/schema.py", line 433, in collect_referenced_types
    collect_referenced_types(field.type)
File "/usr/local/lib/python3.9/site-packages/graphql/type/schema.py", line 433, in collect_referenced_types
     collect_referenced_types(field.type)
File "/usr/local/lib/python3.9/site-packages/graphql/type/schema.py", line 432, in collect_referenced_types
     for field in named_type.fields.values():
File "/usr/local/lib/python3.9/functools.py", line 993, in __get__
    val = self.func(instance)
File "/usr/local/lib/python3.9/site-packages/graphql/type/definition.py", line 811, in fields
     raise cls(f"{self.name} fields cannot be resolved. {error}") from error

TypeError: AndroidAppType fields cannot be resolved. Expected Graphene type, but received: <class 'NoneType'>.

What is the intended way of using GenericReferenceField with ListField? I guess according to #70 it should be supported to use the GenericReferenceField.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions