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"]
}
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With