[GraphQL] Create an Input Object Type for Complex Mutations
for object type an create input GraphQL complex
2023-09-14 09:00:52 时间
When we have certain mutations that require more complex input parameters, we can leverage the Input Object Type in GraphQL. In this video, we’ll learn how to create an Input Object Type and how to add it to a GraphQL Mutation Type.
const express = require('express'); const graphqlHttp = require('express-graphql'); const { getVideoById, getVideos, createVideo } = require('./data/index'); const server = express(); const port = process.env.PORT || 3000; const { GraphQLSchema, GraphQLObjectType, GraphQLInputObjectType, GraphQLString, GraphQLList, GraphQLInt, GraphQLNonNull, GraphQLBoolean, GraphQLID } = require('graphql'); const videoType = new GraphQLObjectType({ 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' } } }); const videoInputType = new GraphQLInputObjectType({ name : 'videoInput', fields : { title : { type : new GraphQLNonNull(GraphQLID), description : 'The title of the video' }, duration : { type : new GraphQLNonNull(GraphQLInt), description : 'The duration of the video' }, watched : { type : new GraphQLNonNull(GraphQLBoolean) } } }); const mutationType = new GraphQLObjectType({ name : 'Mutation', description : 'The root Mutation type', fields : { createVideo : { type : videoType, args : { video : { type : new GraphQLNonNull(videoInputType), }, }, resolve : (_, args) => { return createVideo(args.video) } } } }); const queryType = new GraphQLObjectType({ name : 'QueryType', description : 'The root query type', fields : { videos : { type : new GraphQLList(videoType), resolve : getVideos }, video : { type : videoType, args : { id : { type : new GraphQLNonNull(GraphQLID), description : 'The id of the video' } }, resolve : (_, args) => getVideoById(args.id) } } }); const schema = new GraphQLSchema({ 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:100, watched: false }) { id, title } }
相关文章
- 【说站】javascript中for...of循环是什么
- DoubleTake for Mac(全景图制作软件) v2.6.10注册激活版
- ORA-01609: log string is the current log for thread string – cannot drop members ORACLE 报错 故障修复 远程处理
- ORA-22347: No changes to type specified for ALTER TYPE ORACLE 报错 故障修复 远程处理
- ORA-22870: ALTER TYPE with REPLACE option a non-object type ORACLE 报错 故障修复 远程处理
- ORA-24025: invalid value string, QUEUE_PAYLOAD_TYPE should be RAW or an object type ORACLE 报错 故障修复 远程处理
- ORA-24235: bad value for object type: string ORACLE 报错 故障修复 远程处理
- ORA-24372: invalid object for describe ORACLE 报错 故障修复 远程处理
- ORA-32594: invalid object category for COMMENT command ORACLE 报错 故障修复 远程处理
- ORA-39130: Object type string not imported. Base object name conflicts with the master table ORACLE 报错 故障修复 远程处理
- ORA-03207: subpartitioned type must be specified for composite object ORACLE 报错 故障修复 远程处理
- ORA-04027: self-deadlock during automatic validation for object string.string ORACLE 报错 故障修复 远程处理
- ORA-10631: SHRINK clause should not be specified for this object ORACLE 报错 故障修复 远程处理
- ORA-12041: cannot record ROWIDs for index-organized table “string”.”string” ORACLE 报错 故障修复 远程处理
- ORA-13406: null or invalid GeoRaster object for output ORACLE 报错 故障修复 远程处理
- ORA-14764: FOR VALUES clause cannot be specified for only one partition ORACLE 报错 故障修复 远程处理
- ORA-19006: XMLType TYPE storage option not appropriate for storage type ORACLE 报错 故障修复 远程处理
- Oracle显示游标的使用及游标for循环
- 目录用For循环创建Linux目录的简单技巧(for循环创建linux)
- Mastering Linux: A Comprehensive Guide for C Programmers(linuxc程序员指南)
- MySQL中使用FOR循环快速编写函数(mysql函数for)