将DjangoFilterConnectionField与graphene_django中的自定义连接一起使用

时间:2018-10-10 17:52:44

标签: python django graphql graphene-python

我正在尝试使用与此类似的东西:

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()
    )

根据我的理解,用户节点需要在其元数据上传递UserConnection,而UserConnection需要在其元数据上传递User。

但是,它会创建一个交叉引用。有帮助吗?

1 个答案:

答案 0 :(得分:2)

答案可以在这里找到

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())

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