【问题标题】:How change the Connection Arguments (after, before) in graphene-python (relay)?如何更改graphene-python(中继)中的连接参数(之后,之前)?
【发布时间】:2020-05-30 18:31:39
【问题描述】:

使用:

  • Django 3.x [Django-Filters 2.2.0、graphene-django 2.8.0、graphql-relay 2.0.1]
  • Vue 2.x [Vue-Apollo]

在我的 graphQL 搜索中应用了一些过滤器(iContains 等)后,我尝试更改或操作 connection_args,例如 firstafter。我可以在解析器上获取字典,例如 {'first': 2, 'name__icontains': 'eagle'},其中包含我放入 IDE 中的值。如您所见(示例 1 /def resolve_all_birds2)我已经将其用于逻辑。但我不明白在哪里操作before 的 GraphQLArgument 状态。 afterfirstlast继电器自带的功能?

示例 1

class ExtendedConnection(Connection):

    class Meta:
        abstract = True

    total_count = Int()
    edge_count = Int()

    def resolve_total_count(root, info, **kwargs):
        return root.length

    def resolve_edge_count(root, info, **kwargs):
        return len(root.edges)

class Birds2Node(DjangoObjectType):
    class Meta:
        model = Birds
        filter_fields =  {
            'id':  ['exact', 'icontains'],
            'name': ['exact', 'icontains', 'istartswith', 'iendswith'],
        }

        interfaces = (relay.Node, )
        connection_class = ExtendedConnection

    # --- CUSTOM FIELDS -->
    # pkey = _db primary key 
    pKey = Int()
    def resolve_pKey(parent, info):
        return parent.pk

    # qRank = Item Rank in Edge Array
    qRank = Int()
    def resolve_qRank(parent, info, **kwargs):
        return info.path[2]

class Birds2Query(ObjectType):
    birds2 = relay.Node.Field(Birds2Node)
    all_birds2 = DjangoFilterConnectionField(Birds2Node)


    def resolve_all_birds2(self, info, **kwargs):
        if 'name__icontains' in kwargs:
            nameIcon = kwargs['name__icontains']
            nameIconBool = bool(nameIcon.strip()) # if blanks turns False
            if nameIconBool == False: # has blanks         
                return Birds.objects.filter(name=None)
            pass

        if 'name__istartswith' in kwargs:
            nameIsta = kwargs['name__istartswith']
            nameIstaBool = bool(nameIsta.strip()) # if blanks turns False
            if nameIstaBool == False: # has blanks         
                return Birds.objects.filter(name=None)
            pass      

        return 

例如,在我的 IDE 中,我声明 allBirds2(first: 2, name_Icontains: "a")... 我可以使用解析器通过 **kwargs 或通过 args def resolve_all_birds2(self, info, first, name_icontains): 将这些值作为字典获取,到目前为止一切顺利,我可以操纵我的 ModelQuery 并返回每个边缘只有 2 个。

但想象一下,我想在我的后端将 first: 2 更改为 first: 10?我可以更新字典吗?文档意味着是的,但它似乎与您解决的 ObjectTypes (Fields) 密切相关。 例如我试过这个......

示例 2

def resolve_all_birds2(self, info, **kwargs):
     <...>            
    return {'first': '20', 'name__icontains': 'd' }

输出 IDE: "message": "'dict' object has no attribute 'model'"

示例 3

def resolve_all_birds2(self, info, first, **kwargs):
     <...>            
    return f'20, {first}!'

输出 IDE: "message": "name 'first' is not defined",


问题

不幸的是,我在 graphene-python 文档中的 modelquery 上只发现了参数操作。 所以我的问题是我如何在我的后端操纵字段before 的值。 afterfirstlast,中继提供并且已经可以在我的 IDE 中使用。在用户发送请求后,我是否必须在我的 DjangoObjectType 中额外声明它们或创建自定义节点来操作和更改值?

【问题讨论】:

    标签: django graphql graphene-python graphene-django graphql-python


    【解决方案1】:

    添加中间件可能会允许在发出请求之后和运行查询之前更改输入值。石墨烯有一个例子:https://docs.graphene-python.org/en/latest/execution/middleware/

    但是,从文档中(对我来说)并不清楚提到的哪些参数将包含您要操作的 first 字段。

    不过,似乎并不强烈推荐使用中间件方法,因为这是一种不受欢迎的副作用:https://github.com/graphql-python/graphene/issues/1285

    【讨论】:

      猜你喜欢
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多