6

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"]
}
Ryan King
  • 3,538
  • 12
  • 48
  • 72
  • This is apparently called a connect mutation. Answer can be found here: https://www.graph.cool/docs/reference/simple-api/nested-connect-mutations-tu9ohwa1ui/ – Ryan King Aug 04 '17 at 12:26
  • 1
    Note that this is specific to the Graphcool API :) Also, feel free to [answer your own question](https://stackoverflow.com/help/self-answer). – marktani Aug 04 '17 at 21:53

1 Answers1

9

This is apparently called a '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
    }
  }
Ryan King
  • 3,538
  • 12
  • 48
  • 72