Search code examples
pythondjangographqlgraphene-python

Using DjangoFilterConnectionField with custom Connection in graphene_django


I'm trying to use something similar to this:

class User(DjangoObjectType):
    class Meta:
        model = auth_models.User
        filter_fields = ('email', )
        interfaces = (Node, )
        connection = UserConnection


class UserConnection(Connection):
    extra = graphene.String()

    class Meta:
        node = User

class Query(graphene.ObjectType):
    users_connection = DjangoFilterConnectionField(
        User,
        where=UserWhereInput()
    )

From my understanding, User node needs to be passed UserConnection on it's meta, and UserConnection needs to be passed User on it's meta.

However, it creates a cross reference. Any help?


Solution

  • And the answer can be found here:

    https://github.com/graphql-python/graphene-django/issues/304

    class UserConnection(Connection):
        extra = graphene.String()
    
        class Meta:
            abstract = True
    
    
    class User(DjangoObjectType):
        class Meta:
            model = auth_models.User
            filter_fields = ('email', )
            interfaces = (Node, )
            connection_class = UserConnection
    
    
    class Query(graphene.ObjectType):
        users_connection = DjangoFilterConnectionField(User, where=UserWhereInput())
    

    Reference: https://github.com/graphql-python/graphene-django/pull/313/commits/2a39f5d8eaba3f7772c63b012a974bb9a841fb9f