Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphcool / GraphQL create mutation with relation

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"]
}
like image 573
Ryan King Avatar asked Aug 04 '17 12:08

Ryan King


1 Answers

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
    }
  }
like image 98
Ryan King Avatar answered Oct 09 '22 00:10

Ryan King