【问题标题】:How can I accept a dictionary/object as input in a graphene(GraphQL) mutation?如何接受字典/对象作为石墨烯(GraphQL)突变的输入?
【发布时间】:2020-05-03 09:41:40
【问题描述】:
mutation{
createPayment(p_obj={"bob": 80, "job": 100}){
     <fields here>
     }
}

我能找到的是接受一个对象列表作为输入,例如:

[ {username: "bob", "amount": 80}, {username: "job", "amount": 100} ]

【问题讨论】:

标签: python-3.x graphql graphene-python graphene-django


【解决方案1】:

你可以这样做 -

class PaymentInputType(graphene.InputObjectType):
      username = graphene.String()
      amount = graphene.Int()

并在您的突变中使用 InputType,如下所示。

class CreatePayment(graphene.Mutation):
    class Arguments:
       input = PaymentInputType(required=True)

    ok = graphene.Boolean()

    @staticmethod
    def mutate(root, info, input):
        # save the changes here 
        return CreatePayment(ok=True)

【讨论】:

    猜你喜欢
    • 2018-03-06
    • 2020-09-07
    • 2020-09-01
    • 2017-05-11
    • 2023-03-08
    • 2018-07-30
    • 2017-03-18
    • 2017-05-13
    • 2020-07-27
    相关资源
    最近更新 更多