【问题标题】:Pass array of links in mutation [graphene/python/graphql]在突变中传递链接数组 [graphene/python/graphql]
【发布时间】:2021-06-30 13:28:54
【问题描述】:

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

【问题讨论】:

    标签: python graphql graphene-python


    【解决方案1】:

    我举了一个与 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
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2020-08-03
      • 2020-05-05
      • 2020-02-03
      • 2020-08-13
      • 2019-11-19
      • 2022-01-24
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      相关资源
      最近更新 更多