如何编写嵌套外键的架构和查询?我检查了文档,没有找到如何做到这一点的例子。下面是我基于github和堆栈溢出回答的尝试,让我们说我有以下模型:
class Address(models.Model):
name = models.CharField()
class Person(models.Model):
name = models.CharField()
address = models.ForeignKey('Address', on_delete=models.CASCADE, blank=False, null=False)
class Blog(models.Model):
person = models.ForeignKey('Person', on_delete=models.CASCADE, blank=False, null=False)
text = models.TextField()
我试着编写了这样一个模式:
class AddressInput(graphene.InputObjectType):
name = graphene.String(required=True)
class PersonInput(graphene.InputObjectType):
name = graphene.String(required=True)
address =graphene.Field(AddressInput)
class CreateNewBlog(graphene.Mutation):
blog=graphene.Field(BlogType)
class Arguments:
address_data = AddressInput()
person_data = PersonInput()
text = graphene.String()
@staticmethod
def mutate(root, info, person_data=None, address_data=None, **input):
address = Address.objects.create(name=address_data.name)
person = Person.objects.create(address=address, name=person_data.name)
blog = Blog.objects.create(person =person, text=input['text'])
blog.save()
return CreateNewBlog(blog=blog)
我用了这样的查询:
mutation {
CreateNewBlog(person: { address: {name: "aaa"},
name: "First Last" }, text: "hi hi") {
Blog {
person{
name
address{
name
}
},
text
}
}
}
我收到了一条错误消息:
{
"errors": [
{
"message": "'NoneType' object has no attribute 'name'",
"locations": [
{
"line": 32,
"column": 9
}
],
"path": [
"CreateNewBlog"
]
}
],
"data": {
"CreateNewBlog": null
}
}
我认为问题在于我编写schema.py文件的方式。将InputFields嵌套到另一个InputField中不起作用的地方。还有其他方法来写一个突变吗?
发布于 2020-10-26 05:53:41
好吧这里有几件事。首先,您应该生成您的schema.graphql
文件,因为这将显示Graphene构建的模式的实际最终形状,这将使您的调试变得更容易。或者,您可以使用GraphiQL测试查询,并让它的文档和自动完成为您完成繁重的工作。
但具体而言,你的石墨烯突变定义将产生一个突变,如下所示:
input AddressInput {
name: String!
}
input PersonInput {
name: String!
address: AddressInput
}
type CreateNewBlogOutput {
blog: Blog
}
type Mutation {
CreateNewBlog(addressData: AddressInput, personData: PersonInput, text: String): CreateNewBlogOutput!
}
值得注意的是,在这里提供AddressInput有两种方法,一种是根方式,另一种是在PersonInput中。这可能不是你想要做的。其次,不需要任何根参数,这会导致错误消息非常无助,因为问题是您调用的是不正确的变异参数,但是查询验证器允许它通过,因为您的类型非常允许。
我相信,如果你像下面这样运行这种突变,它实际上会起作用:
mutation {
CreateNewBlog(
personData: {
address: {
name: "aaa"
},
name: "First Last"
},
text: "hi hi"
) {
blog {
person {
name
address {
name
}
}
text
}
}
}
我在这里只做了两个修改,person
被更改为personData
(为了与您的突变定义相匹配,Graphene会自动完成从蛇案例到骆驼案例的对话),以及在字段选择中将Blog
更改为blog
。
但让我们再往前走一步,这是我如何制造突变的方法。
class AddressInput(graphene.InputObjectType):
name = graphene.String(required=True)
class PersonInput(graphene.InputObjectType):
name = graphene.String(required=True)
address = AddressInput(required=True)
class CreateNewBlogInput(graphene.InputObjectType):
person = PersonInput(required=True)
text = graphene.String(required=True)
class CreateNewBlogPayload(graphene.ObjectType):
blog = graphene.Field(BlogType, required=True)
class CreateNewBlog(graphene.Mutation):
class Arguments:
input_data = CreateNewBlogInput(required=True, name="input")
Output = CreateNewBlogPayload
@staticmethod
def mutate(root, info, input_data):
address = Address.objects.create(name=input_data.person.address.name)
person = Person.objects.create(address=address, name=input_data.person.name)
blog = Blog.objects.create(person=person, text=input_data.text)
blog.save()
return CreateNewBlogPayload(blog=blog)
在构造Graphene的突变对象时,我还会将CreateNewBlog
更改为createNewBlog
,因为GraphQL惯例是使用较低的camel大小写进行突变。
然后你就会这样运行:
mutation {
createNewBlog(
input: {
person: {
address: {
name: "aaa"
},
name: "First Last"
}
text: "hi hi"
}
) {
blog {
person {
name
address {
name
}
}
text
}
}
}
为什么将整个输入封装在一个输入字段中?主要是因为它使得在使用变量时更容易在客户端调用变异,所以您可以只提供正确形状的单个输入arg,而不是多个。
// So instead of this
mutation OldCreateNewBlog($person: PersonInput, $text: String) {
createNewBlog(
personData: $person
text: $text
) {
blog {
person {
name
address {
name
}
}
text
}
}
}
// You have this
mutation NewCreateNewBlog($input: CreateNewBlogInput!) {
createNewBlog(
input: $input
) {
blog {
person {
name
address {
name
}
}
text
}
}
}
后者使更改输入形状变得更容易,并且只需在客户端代码中的一个位置进行更改。
https://stackoverflow.com/questions/64421773
复制相似问题