【发布时间】:2021-06-30 13:28:54
【问题描述】:
我对使用石墨烯有点困惑。 我在 https://www.howtographql.com/graphql-python/3-mutations/ 上使用突变示例,但这里仅显示如何创建 ONE 链接的示例。现在对我来说更现实的是,您有一个链接列表或传递给后端和以后的数据库的其他对象。有没有人已经实现了这样的例子?
【问题讨论】:
标签: python graphql graphene-python
我对使用石墨烯有点困惑。 我在 https://www.howtographql.com/graphql-python/3-mutations/ 上使用突变示例,但这里仅显示如何创建 ONE 链接的示例。现在对我来说更现实的是,您有一个链接列表或传递给后端和以后的数据库的其他对象。有没有人已经实现了这样的例子?
【问题讨论】:
标签: python graphql graphene-python
我举了一个与 https://docs.graphene-python.org/en/latest/types/mutations/#inputfields-and-inputobjecttypes 不同的例子。下面的代码 sn-p 应该可以帮助您在单个突变中创建多个实例。
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: "Danish Wani" age:28}, {name: "Wani Danish" age:29}]){
persons{
name
age
}
}
【讨论】: