我对使用石墨烯有点困惑。我使用的是https://www.howtographql.com/graphql-python/3-mutations/上的突变示例,但这里只展示了如何创建一个链接。现在对我来说更现实的是,你有一个链接或其他对象的列表,你可以传递给你的后端和以后的数据库。有没有人已经实现了这样的例子?
发布于 2021-04-05 18:16:18
我举了一个来自https://docs.graphene-python.org/en/latest/types/mutations/#inputfields-and-inputobjecttypes的不同的例子。下面的代码片段应该可以帮助您在单个突变中创建多个实例。
import graphene
from .models import Person
class PersonInput(graphene.InputObjectType):
name = graphene.String(required=True)
age = graphene.Int(required=True)
class PersonType(DjangoObjectType):
class Meta:
model = Person
class CreatePerson(graphene.Mutation):
class Arguments:
person_objects = graphene.List(PersonInput, required=True)
persons = graphene.List(PersonType)
def mutate(root, info, person_objects):
persons = list()
for person_data in person_objects:
person = Person.objects.create(
name=person_data.name,
age=person_data.age
)
persons.append(person)
return CreatePerson(persons=persons)
突变:
createPerson(personObjects: [{name: "testing multiple instance creation in single mutation" age:28}, {name: "testing multiple instance creation in single MUTATIONS" age:29}]){
persons{
name
age
}
}
https://stackoverflow.com/questions/66940260
复制相似问题