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. index action分析

    上一篇从结构上分析了action的,本篇将以index action为例仔分析一下action的实现方式. 再概括一下action的作用:对于每种功能(如index)action都会包括两个基本的类* ...

  2. 初步使用RecyclerView实现瀑布流

    先看效果 关于RecyclerView,真的是很强大. 个人觉得主要方便的地方是 1.直接可以设置条目布局,通过setLayoutManager LinearLayoutManager:线性布局,横向 ...

  3. Java循环链表实现约瑟夫环(搬运)

    public class Josephus { static class Node{ int val; Node next; Node(int v){ val=v; } }//成员类,代表节点,类似于 ...

  4. 14.NPM 常用命令

    转自:http://www.runoob.com/nodejs/nodejs-npm.html PM提供了很多命令,例如install和publish,使用npm help可查看所有命令. NPM提供 ...

  5. 【例题 8-3 UVA - 1152】4 Values whose Sum is 0

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然中间相遇. 自己写了个hash处理一下冲突就可以了. [代码] /* 1.Shoud it use long long ? 2. ...

  6. 基于mybatis的BaseDao及BaseService深度结合(转)

    原文地址:http://zhaoshijie.iteye.com/blog/2003209 关键字:Mybatis通用DAO设计封装(mybatis) 说明: mybatis默认分页机制为逻辑分页,所 ...

  7. 洛谷 P1610 鸿山洞的灯

    P1610 鸿山洞的灯 题目描述 已知n盏灯以及每盏灯的位置p[i],p[i]均不相等,两盏相邻的灯当小于dist时,若这个安全距离里面还有灯是亮着时,就可以关掉该盏灯,(即若第i-1盏与第i+1盏的 ...

  8. OSX: 逻辑卷管理系统Core Storage(1)

    Mac高大上嘛? Mac由于贵就高大上了?Mac由于没有这个哪个就不高大上了?本文没有结论,仅仅是回归技术本源,是不是高大上还要大家自己评说. 大多数Mac用户可能并不在乎苹果的OS X操作系统缺少一 ...

  9. Light OJ 1373 Strongly Connected Chemicals 二分匹配最大独立集

    m种阳离子 n种阴离子 然后一个m*n的矩阵 第i行第j列为1代表第i种阴离子和第j种阴离子相互吸引 0表示排斥 求在阳离子和阴离子都至少有一种的情况下 最多存在多少种离子能够共存 阴阳离子都至少须要 ...

  10. android studio执行 Information:Gradle tasks [:app:assembleDebug]失败处理

    Error:Execution failed for task ‘:app:mergeDebugResources’. > Some file crunching failed, see log ...