[GRAPHQL] Create an Input Object Type for Complex mutations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.