Here are my requirements:
"add" mutation, every field(or called scalar) of
BookInput
input type should have additional type modifiers "!" to validate the non-null value. Which means when I add a book, the argument must havetitle
andauthor
field, like{title: "angular", author: "novaline"}
"update" mutation, I want to update a part of fields of the book, don't want to update whole book(MongoDB document, And, I don't want front-end to pass graphql server a whole big book mutation argument for saving bandwidth). Which means the book argument can be
{title: "angular"}
or{title: "angular", author: "novaline"}
.
Here are my type definitions:
const typeDefs = `
input BookInput {
title: String!
author: String!
}
type Book {
id: ID!
title: String!
author: String!
}
type Query {
books: [Book!]!
}
type Mutation{
add(book: BookInput!): Book
update(id: String!, book: BookInput!): Book
}
`;
For now, "add" mutation works fine. But "update" mutation cannot pass the non-null check if I pass {title: "angular"}
argument
Here is a mutation which does not pass the non-null check, lack of "author" field for BookInput
input type.
mutation {
update(id: "1", book: {title: "angular"}) {
id
title
author
}
}
So, graphql will give me an error:
{
"errors": [
{
"message": "Field BookInput.author of required type String! was not provided.",
"locations": [
{
"line": 2,
"column": 24
}
]
}
]
}
How do I design the BookInput
input type? Don't want to define addBookInput
and updateBookInput
. It's duplicated.