When we have certain mutations this require more complex input parameters, we can leverage the input Object Type in GRAPHQ L. In the this video, we'll learn how to create an Input Object type and how to add it to a graphql Mutation Type.
ConstExpress = require ('Express');ConstGraphqlhttp = require ('express-graphql');Const{Getvideobyid, Getvideos, createvideo}= Require ('./data/index');ConstServer =Express ();ConstPort = Process.env.PORT | | the;Const{graphqlschema, graphqlobjecttype, graphqlinputobjecttype, graphqlstring, Graphqllist, GraphQLInt, Graphqlnonnull, Graphqlboolean, graphqlid}= Require ('graphql');ConstVideotype =NewGraphqlobjecttype ({name:'Video', Description:'A Video on Egghead.io', fields: {ID: {type:graphqlid, Description:'The ID of the video'}, Title: {type:graphqlstring, Description:'The title of the video'}, Duration: {type:graphqlint, Description:'The duration of the video'}, watched: {Type:graphqlboolean, Description:'Whether or no the viewer watched the video' } }});ConstVideoinputtype =New Graphqlinputobjecttype({name:'Videoinput', Fields: {title: {type:NewGraphqlnonnull (graphqlid), Description:'The title of the video'}, Duration: {type:NewGraphqlnonnull (graphqlint), Description:'The duration of the video'}, watched: {type:Newgraphqlnonnull (Graphqlboolean)}});ConstMutationtype =NewGraphqlobjecttype ({name:'Mutation', Description:'The root Mutation type', fields: {createvideo: {type:videotype, args: {vide O: { type: New graphqlnonnull (Videoinputtype) ,},}, Resolve: (_, args)= { returnCreatevideo (Args.video)}} }});ConstQueryType =NewGraphqlobjecttype ({name:'QueryType', Description:'The root query type', fields: {videos: {type:Newgraphqllist (Videotype), Resolve:getvideos}, Video: {type:videotype, Args: {id: {type:NewGraphqlnonnull (graphqlid), Description:'The ID of the video'}}, Resolve: (_, args)=Getvideobyid (Args.id)}});Constschema =NewGraphqlschema ({query:querytype, mutation:mutationtype}); Server.use ('/graphql', Graphqlhttp ({schema, GRAPHIQL:true,//Use GRAPHIQL interface}); Server.listen (port, ()={console.log (' Listening on HTTP ')})
In GRAPHIQL:
mutation Video { Createvideo (video: { title:"graphql"), Duration:+, false }) { id, title }}
[GRAPHQL] Create an Input Object Type for Complex mutations