首页 > 解决方案 > 使用 InputObjectType 实例作为中继突变的输入

问题描述

根据 Graphene 文档,如果我们要在实现时从用户那里获取输入relay.clientIDMutation,那么它需要在 Input 子类下,如下所示:

class Foo(relay.clientIDMuation):
  class Input:
    arg1 = graphene.String()
    arg2 = graphene.Sring()

# the return parameters and mutate and get payload method after this

但是,我们也可以指定 InputObjectTypes。

class Bar(graphene.InputObjectType):
  arg1 = graphene.String()
  arg2 = graphene.String()

如果我们要使用普通的石墨烯突变对象,您可以在 Arguments 子类中使用指定输入。

class NewFoo(graphene.Mutation):
  class Arguments:
    input = Bar()

  # return arguments and mutate method after this

当继承自 NewFoo 突变同时仍能够将 clientMutationID 作为输入参数传递时,如何BarInputObjectType其用于 Input 属性?relay.clientIDMutation

class NewFoo(relay.clientIDMutation):
  class Attribute:
    input = Foo()

# the return parameters and mutate and get payload method after this

注意:我已经尝试添加具有上述输入属性的 Arguments 子类,但是当类继承自relay.clientIDMuation.

标签: pythongraphqlrelaygraphene-python

解决方案


您不需要class Attribute:relay.clientIDMutation类一起使用。

尝试这个。在开始你的中继类之前声明这个类。

class Bar(graphene.InputObjectType):
   arg1 = graphene.String()
   arg2 = graphene.String()

在你的接力班里,

class NewFoo(relay.clientIDMutation):
   class Input:
   inputarray = Foo()

你的 Mutation 应该是这样的,

mutation{
  NewFoo(input:{
      inputarray:{
        arg1:"Hello"
        arg2:"world"
        }
      }
    )
 }

尽管您仍然无法将输入数组传递给该字段。


推荐阅读