In order to change the data that we can query for in a GraphQL Schema, we have to define what is called a mutation in GraphQL. Mutations allow us to specify ways to create, update, and delete data. It also provides a way to fetch information after the mutation occurs. In this video, we’ll learn how to create a Mutation Type in GraphQL and specify the information we want returned.

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, 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 mutationType = new GraphQLObjectType({
name : 'Mutation',
description : 'The root Mutation type',
fields : {
createVideo : {
type : videoType,
args : {
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)
}
},
resolve : (_, args) => {
return createVideo(args)
}
}
}
}); 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`)
})

Write mutation in GraphiQL:

mutation video {
createVideo(title: "new Redux", duration:, watched: false) {
id,
title
}
}

Data:

const videoA = {
id: 'a',
title: 'Create a GraphQL Schema',
duration: ,
released: true,
};
const videoB = {
id: 'b',
title: 'Ember.js CLI',
duration: ,
released: false,
};
const videos = [videoA, videoB];
const getVideoById = (id) => new Promise((resolve) => {
const [video] = videos.filter((video) => {
return video.id === id;
}); resolve(video);
});
const getVideos = () => new Promise((resolve) => resolve(videos));
const createVideo = ({ title, duration, released }) => {
const video = {
id: (new Buffer(title, 'utf8')).toString('base64'),
title,
duration,
released,
}; videos.push(video); return video;
}; exports.getVideoById = getVideoById;
exports.getVideos = getVideos;
exports.createVideo = createVideo;

最新文章

  1. C程序范例(1)——学生管理系统”数组“实现
  2. Android 自定义ListView
  3. Mvc利用淘宝Kissy uploader实现图片批量上传附带瀑布流的照片墙
  4. Cocos2d-x 3.2 学习笔记(九)EventDispatcher事件分发机制
  5. [Unity] Unity3D研究院编辑器之独立Inspector属性
  6. C#对泛型List<T>系列化与反系列化
  7. [luogu2982][USACO10FEB]慢下来Slowing down(树状数组+dfs序)
  8. href="#"会导致location.replace(location.href);脚本不工作
  9. hdu 2064
  10. Qt Mac 下软件Release 公布dmg
  11. OGR – Merging Multiple SHP files
  12. 【非专业前端】Vue UI 之 建立Vuetify工程
  13. hadoop 伪分布式搭建
  14. Go学习笔记(五)Go命令工具
  15. 利用 Chrome 原生功能截图网页全图
  16. hadoop distcp hdfs://ns1/aaa hdfs://ns8/bbb UnknownHostException: xxx 两个高可用(ha)集群间distcp 如何识别两个集群逻辑名称
  17. 使用zabbix3.0.4的ICMP Ping模版实现对客户端网络状态的监控
  18. webshpere 节点&环境分析
  19. 如何破解安卓App
  20. python中的接口和依赖注入

热门文章

  1. 关于结构体COORD介绍
  2. 字符设备驱动-------Linux异常处理体系结构
  3. Mac 终端操作数据库
  4. ecshop微信接口基础认识
  5. Solr 倒排索引
  6. 【例题 7-6 UVA - 140】Bandwidth
  7. CodeVs——T 3305 水果姐逛水果街Ⅱ
  8. RMAN冷备份、一致性备份脚本
  9. Flask项目之手机端租房网站的实战开发(二)
  10. 【例题 6-17 UVa 10562】Undraw the Trees