Graphcool / GraphQL create mutation with relation

时间:2017-08-04 12:12:29

标签: graphql graphcool

I have the following GraphQL schema. It has Books and Authors with a many to many relationship. How do I create a new Book mutation for an existing Author?


schema

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

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

variables

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

1 个答案:

答案 0 :(得分:7)

这显然被称为“连接突变”。 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
    }
  }