GraphQL突变可在一个请求中接受动态大小和常见标量类型的数组 [英] GraphQL mutation that accepts an array of dynamic size and common scalar types in one request

查看:65
本文介绍了GraphQL突变可在一个请求中接受动态大小和常见标量类型的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够创建一个用户并在单个请求中添加喜欢的电影(一个对象数组,其中包含对Movies集合及其对每个电影的个人评价的引用).

I need to be able to create a user and add it's favourite movies (An array of objects with a reference to the Movies collection and his personal rating for each movie) in a single request.

看起来像这样的东西(伪代码)

Something that could look like this (pseudocode)

var exSchema = `
 type Mutation {
    addUser(
        name: String!
        favMovies: [{ movie: String! #ref to movies coll
                      personal_rating: Int! # this is different for every movie
                   }]
    ) : User
 }
...
`

在单个请求中执行此操作的graphql方法是什么?我知道我可以通过多个突变/请求来实现结果,但我想一次完成.

What is the graphql way of doing this in a single request? I know I can achieve the result with multiple mutations/requests but I would like to do it in a single one.

推荐答案

您可以传递这样的数组

var MovieSchema = `
  type Movie {
   name: String
  }
  input MovieInput {
   name: String
  }
  mutation {
   addMovies(movies: [MovieInput]): [Movie]
  }
`

然后在您的突变中,您可以传递类似的数组

Then in your mutation, you can pass an array like

mutation {
  addMovies(movies: [{name: 'name1'}, {name: 'name2'}]) {
    name
  }
}

还没有测试代码,但是您知道了

Haven't tested the code but you get the idea

这篇关于GraphQL突变可在一个请求中接受动态大小和常见标量类型的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆