是否有可能传递对象数组(json)作为变异的输入字段?石墨烯的Python

问题描述 投票:1回答:2

我试图传递json字段作为我的graphql变异的输入。我一直在努力寻找,但没有运气。通过定义graphene.List(graphene.String)可用于传递字符串数组,我可以传递数组。

我认为有一个名为graphene.JSONstring()的类型,如果我将它与graphene.List(graphene.JSONstring)一起使用但没有运气,我认为它会起作用,仍然会出现错误,说类型不对。

突变期间我有这样的事情

    mutation {
        create(data:{
                field1: [
                    {
                        "first": "first",
                        "last": "last"
                    },
                    {
                        "first":"first1",
                        "last":"last1"
                    }
                ]
        })
    }

至于输入类

class NameInput(graphene.InputObjectType):
    # please ignore the same field names, just listing what I have tried
    field1 = graphene.JSONString()  
    field1 = graphene.List(graphene.JSONString)
    field1 = graphene.List(graphene.String)

有没有人知道这将如何工作?

提前致谢

python json object graphql graphene-python
2个回答
1
投票

好像你正在尝试嵌套输入对象。不幸的是,我从未使用石墨烯,但也许我可以回答GraphQL规范,然后对石墨烯代码进行有根据的猜测:

type Mutation {
  create(data: NameInput): Boolean # <- Please don't return just Boolean
}

input NameInput {
  field1: FistLastInput[]
}

input FirstLastInput {
  first: String!
  last: String!
}

这意味着您将需要两个输入对象来描述输入的结构。为您的对象创建一个新类,其中包含字段firstlast

class FirstLastInput(graphene.InputObjectType):
    first = graphene.NonNull(graphene.String)
    last = graphene.NonNull(graphene.String)

现在我们可以在初始查询中使用输入对象:

class NameInput(graphene.InputObjectType):
    field1 = graphene.List(FirstLastInput)

0
投票

你可以尝试这样:

class NameInput(graphene.InputObjectType):
    field1 = graphene.JSONString()

然后:

mutation {
    create(data:{
            field1: "[
                {
                    \"first\": \"first\",
                    \"last\": \"last\"
                },
                {
                    \"first\":\"first1\",
                    \"last\":\"last1\"
                }
            ]"
    })
}

所以基本上把json作为字符串发送。

© www.soinside.com 2019 - 2024. All rights reserved.