Graphcool / GraphQL create mutation with relation
Asked Answered
N

1

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"]
}
Nevus answered 4/8, 2017 at 12:12 Comment(2)
This is apparently called a connect mutation. Answer can be found here: graph.cool/docs/reference/simple-api/…Nevus
Note that this is specific to the Graphcool API :) Also, feel free to answer your own question.Monarchist
N
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
    }
  }
Nevus answered 5/8, 2017 at 1:25 Comment(1)
Welp, that link is dead and the web archive doesn't have it archived. :\Izak

© 2022 - 2024 — McMap. All rights reserved.