Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否有可能传递对象数组(json)作为变异的输入字段?石墨烯蟒蛇_Python_Json_Object_Graphql_Graphene Python - Fatal编程技术网

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

Python 是否有可能传递对象数组(json)作为变异的输入字段?石墨烯蟒蛇,python,json,object,graphql,graphene-python,Python,Json,Object,Graphql,Graphene Python,我正在尝试传递json字段作为graphql变体的输入。 我一直在努力寻找,但没有运气。通过定义graphene.List(graphene.String)可以很好地传递数组,我知道它可以传递字符串数组 我想有一个名为graphene.JSONstring()的类型,如果我将它与graphene.List(graphene.JSONstring)一起使用,我认为它会起作用,但运气不好,仍然会出现错误,说类型不对 我在变异过程中有类似的情况 mutation { creat

我正在尝试传递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)
有人知道这是怎么回事吗


提前感谢

似乎您正在尝试嵌套输入对象。不幸的是,我从未使用过graphene,但也许我可以根据GraphQL规范回答,然后对graphene代码进行有根据的猜测:

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

input NameInput {
  field1: FistLastInput[]
}

input FirstLastInput {
  first: String!
  last: String!
}
现在,我们可以在初始查询中使用输入对象:

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

您可以这样尝试:

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

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

所以基本上是以字符串的形式发送json。

Graphene提供了一个
GenericScalar
类型。您可以使用它输入/输出泛型类型,如
int
str
dict
列表
,等等

from graphene import InputObjectType, Mutation
from graphene.types.generic import GenericScalar

class InputObject(InputObjectType):
    field1 = GenericScalar()

class Create(Mutation):
    class Arguments:
        data = InputObject()

    def mutate(root, info, data):
        # do something with data.field1
然后你的输入看起来像

mutation {
  create (
    data: {
      field1: [
        {
          first: "first",
          last: "last"
        },
        {
          first: "first1",
          last: "last1"
        }
      ]
    }
  )
}
请注意,
field1
可以接受任何通用输入,因此请确保对其进行验证

此外,当使用
GenericScalar
字段进行输出(查询)时,您将无法查询其子字段。但您可以为该字段编写解析器,以确保只返回特定的子字段

是指向相应GitHub问题的链接

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