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 || ; 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:,
watched: false
}) {
id,
title
}
}

[GraphQL] Create an Input Object Type for Complex Mutations的更多相关文章

  1. [terry笔记]IMPDP报错ORA-39083 Object type TYPE failed to create ORA-02304

    今天在使用impdp导入的时候(同一数据库中转换schema),遇到了 ORA-39083: Object type TYPE failed to create with error: ORA-023 ...

  2. Object type TYPE failed to create with error

    ORA-39083: Object type TYPE failed to create with error: ORA-02304: invalid object identifier litera ...

  3. impdp报错ORA-39083 ORA-02304 Object type TYPE failed to create

    环境Red Hat Enterprise Linux Server release 5.8 (Tikanga)ORACLE Release 11.2.0.3.0 Production 我用expdp, ...

  4. [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type

    ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...

  5. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  6. [Liferay6.2.2]AUI的小坑:input的type属性

    <aui:input name="name" label="姓名" value="<%=student.getName() %>&q ...

  7. 修改input的type属性

    在ff和chrome中是可以直接修改input的type属性的,但是在ie下面是不允许的. 用jquery 的attr方法去修改,在jquery1.8.3版本会直接抛出异常,但在1.9及以上版本就不再 ...

  8. input的type属性的修改

    记录一下成长的历程吧! 刚开始写,没什么文笔,也没什么技术含量,可能主要的是纪录一下平常工作学习中遇到的问题,以及解决的办法吧.或者只有问题,没有解决办法. 前两天项目中遇到的一个问题,由于之前一直没 ...

  9. How to create QTP Shared Object Repository

    How to create QTP Shared Object Repository To create a shared object repository by performing follow ...

随机推荐

  1. Android学习笔记进阶十三获得本地全部照片

    这是Intent的一个用法. 在ActivityAction里面有一个“ACTION_GET_CONTENT”字符串常量,该常量让用户选择特定类型的数据. intent.setType("i ...

  2. 10.查看npm安装信息和版本号

    转自:http://www.runoob.com/nodejs/nodejs-express-framework.html 你可以使用以下命令来查看所有全局安装的模块: $ npm list -g ├ ...

  3. DG观察日志传输

    --primary端查询v$archived_log视图,确认日志是否被应用:   set lines 300 pages 300 col name for a20 select name,dest_ ...

  4. atxserver2安装与使用

    atxserver2的使用 1.首先clone atxserver2代码,此时使用pip3 install requirements后执行python main.py 会提示“ [WinError 1 ...

  5. 【hdu 1083】Courses

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=1083 [Description] 有p门的课,每门课都有若干学生,现在要为每个课程分配一名课代表, ...

  6. log4j的总结

    概述 log4j是日志处理的框架,相当于.net中的log4net.因为之前在.net中学习过log4net.所以.在学习log4j上,感觉很的亲切.本篇博客主要是讲一个图,好了进入正题. log4j ...

  7. background-size在PC端和移动端使用媒体查询的不同

    1.PC端background-size:100%:是展现原图的大小. 2.使用媒体查询的移动端的background-size:100%:是根据内容的高度自动拉伸高度的.

  8. 几个移动web app开发框架

    几个移动web app开发框架 一.总结 1.有amaze ui,有app.js(登录注册界面用到的)  二.几个移动web app开发框架 jQuery Mobile jQuery Mobile框架 ...

  9. StringBuilder和String的区别

      使用   StringBuilder 语言 C# String   对象是不可改变的.每次使用   System.String   类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为 ...

  10. NSAttributeString创建各种文字效果

    NSDictionary *attributes =@{ NSForegroundColorAttributeName: [UIColorredColor], NSFontAttributeName: ...