Skip to main content

Mutations

A brief look on how mutations for this project looks like. In case you want to know what mutations are within GraphQL you can check this link

Mutation declaration#

FieldType
addBookBook
addAuthorAuthor

addBook#

type: Book#
ArgumentTypeRequired
nameGraphQLStringtrue
genreGraphQLStringtrue
authorIdGraphQLIDtrue

addAuthor#

type: Author#
ArgumentTypeRequired
nameGraphQLStringtrue
ageGraphQLInttrue

You can find the mutation declaration here. This is an example on how it looks like:

const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
addAuthor: {
type: AuthorType,
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
age: { type: new GraphQLNonNull(GraphQLInt) },
},
resolve(parent, args) {
const author = new Author({
name: args.name,
age: args.age,
});
// Mongo DB save
}
},
addBook: {
type: BookType,
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
genre: { type: new GraphQLNonNull(GraphQLString) },
authorId: { type: new GraphQLNonNull(GraphQLID) },
},
resolve(parent, args) {
const book = new Book({
name: args.name,
genre: args.genre,
authorId: args.authorId
});
// Mongo DB save
}
},
},
});