首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >传递突变中的链接数组[石墨烯/python/graphql]

传递突变中的链接数组[石墨烯/python/graphql]
EN

Stack Overflow用户
提问于 2021-04-04 18:27:10
回答 1查看 196关注 0票数 1

我对使用石墨烯有点困惑。我使用的是https://www.howtographql.com/graphql-python/3-mutations/上的突变示例,但这里只展示了如何创建一个链接。现在对我来说更现实的是,你有一个链接或其他对象的列表,你可以传递给你的后端和以后的数据库。有没有人已经实现了这样的例子?

EN

回答 1

Stack Overflow用户

发布于 2021-04-05 18:16:18

我举了一个来自https://docs.graphene-python.org/en/latest/types/mutations/#inputfields-and-inputobjecttypes的不同的例子。下面的代码片段应该可以帮助您在单个突变中创建多个实例。

代码语言:javascript
运行
复制
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)

突变:

代码语言:javascript
运行
复制
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
    }
  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66940260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档