Graphcool / GraphQL 使用关系创建变异

Graphcool / GraphQL create mutation with relation

我有以下 GraphQL 架构。它有 BooksAuthors 多对多关系。如何为现有作者创建新的图书变更?


架构

type Author implements Node {
  books: [Book!]! @relation(name: "BookAuthors")
  createdAt: DateTime!
  id: ID! @isUnique
  name: String!
  updatedAt: DateTime!
}

type Book implements Node {
  authors: [Author!]! @relation(name: "BookAuthors")
  createdAt: DateTime!
  id: ID! @isUnique
  title: String!
  updatedAt: DateTime!
}

变异

mutation CreateBook($title: String!, $authors: [Author!]) {
    createBook(
      title: $title,
      authors: $authors,
    ) {
      id
      title
    }
  }

变量

{
  "title": "Ryans Book",
  "authors": ["cj5xti3kk0v080160yhwrbdw1"]
}

这显然被称为'connect mutation'。 https://www.graph.cool/docs/reference/simple-api/nested-connect-mutations-tu9ohwa1ui/

mutation CreateBook($title: String!, $authorIds: [ID!]) {
    createBook(
      title: $title,
      authorIds: $authorIds,
    ) {
      id
      title
    }
  }