【问题标题】:Using DjangoFilterConnectionField with custom Connection in graphene_django在 graphene_django 中使用带有自定义连接的 DjangoFilterConnectionField
【发布时间】:2019-03-15 17:06:55
【问题描述】:

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

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。

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

【问题讨论】:

    标签: python django graphql graphene-python


    【解决方案1】:

    答案可以在这里找到:

    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

    【讨论】:

    • 我仍然不明白您如何真正设置连接对象中“额外”字段的值。您能否在答案中添加一个 resolve_users_connection 的定义来说明如何做到这一点?
    猜你喜欢
    • 2021-10-25
    • 2016-02-02
    • 2023-03-27
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多