首页 > 解决方案 > 如何接受字典/对象作为石墨烯(GraphQL)突变的输入?

问题描述

mutation{
createPayment(p_obj={"bob": 80, "job": 100}){
     <fields here>
     }
}

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

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

标签: python-3.xgraphqlgraphene-pythongraphene-django

解决方案


你可以做这样的事情 -

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)

推荐阅读