Mutations in GraphQL - take 2

Last time we looked into GraphQL mutations, we tried a very simple one. This time we’ll see how to define a mutation with arguments and multiple objects returned.

A real life mutation in GraphQL will have multiple arguments and will return one or more objects.

GraphQL schema definition for a mutation

Here is such a mutation - AddPlayList:

letmutationType=newGraphQLObjectType({name:'RootMutationType',fields:{AddPlayList:{description:'Add a playlist for a given user',type:newGraphQLObjectType({name:'AddPlayListInput',fields:()=>({playList:{type:PlayListType},user:{type:UserType}})}),args:{userId:{type:newGraphQLNonNull(GraphQLString)},title:{type:newGraphQLNonNull(GraphQLString)},description:{type:GraphQLString}},resolve:(parent,args,ast)=>{}},updateCount:{type:GraphQLInt,description:'Updates the count',resolve:function(){count+=1;returncount;}}}});

In this definition:

type defines the return type of this mutation. Here we want to return 2 objects:

the playList that this mutation would create

the user that this playLits would belong to

args lists the arguments that this mutation needs.

resolve as for the queries, this is where the actual processing takes place.