简体   繁体   English

在突变中传递链接数组 [graphene/python/graphql]

[英]Pass array of links in mutation [graphene/python/graphql]

i'm a little bit confuses about using graphene.我对使用石墨烯有点困惑。 I am using the example of mutations on https://www.howtographql.com/graphql-python/3-mutations/ , but here only the example is shown how to create ONE link.我正在使用https://www.howtographql.com/graphql-python/3-mutations/上的突变示例,但这里仅显示如何创建 ONE 链接的示例。 Now it is more realistic for me that you have a list of links or other objects that you pass to your backend and later database.现在对我来说更现实的是,您有一个链接列表或传递给后端和以后的数据库的其他对象。 Is there anyone who has already implemented such an example?有没有人已经实现了这样的例子?

I have taken a different example from https://docs.graphene-python.org/en/latest/types/mutations/#inputfields-and-inputobjecttypes .我从https://docs.graphene-python.org/en/latest/types/mutations/#inputfields-and-inputobjecttypes举了一个不同的例子。 Below code snippet should help you in creating multiple instances in a single mutation.下面的代码片段应该可以帮助您在单个突变中创建多个实例。

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)

mutation:突变:

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
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM