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;

[GraphQL] Write a GraphQL Mutation的更多相关文章

  1. [GraphQL] Write a GraphQL Schema in JavaScript

    Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but ...

  2. [GraphQL] Serve a GraphQL Schema as Middleware in Express

    If we have a GraphQL Schema expressed in terms of JavaScript, then we have a convenient package avai ...

  3. [GraphQL] Create a GraphQL Schema

    we’ll take a look at the GraphQL Language and write out our first GraphQL Schema. We’ll use the grap ...

  4. ASP.NET Core中使用GraphQL - 第七章 Mutation

    ASP.NET Core中使用GraphQL - 目录 ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间 ...

  5. [GraphQL] Deploy a GraphQL dev playground with graphql-up

    In this lesson we'll use a simple GraphQL IDL schema to deploy and explore a fully functional GraphQ ...

  6. graphql cli 开发graphql api flow

    作用 代码生成 schema 处理 脚手架应用创建 项目管理 安装cli npm install -g graphql-cli 初始化项目(使用.graphqlconfig管理) 以下为demo de ...

  7. Reusing & Composing GraphQL APIs with GraphQL Bindings

    With GraphQL bindings you can embed existing GraphQL APIs into your GraphQL server. In previous blog ...

  8. [GraphQL] Query a GraphQL API with graphql-request

    To query a GraphQL API, all you need to do is send an HTTP request that includes the query operation ...

  9. 使用Hot Chocolate和.NET 6构建GraphQL应用(1)——GraphQL及示例项目介绍

    系列导航 使用Hot Chocolate和.NET 6构建GraphQL应用文章索引 前言 这篇文章是这个系列的第一篇,我们会简单地讨论一下GraphQL,然后介绍一下这个系列将会使用的示例项目. 关 ...

随机推荐

  1. Flume的data flow(数据流)

    data flow描述了数据从产生,传输.处理并最终写入目标的一条路径. 数据的采集的流向!如下图所示.  

  2. mahout-distribution-0.9.tar.gz的安装的与配置、启动与运行自带的mahout算法

    不多说,直接上干货! 首先,别在windows下搭建什么,安装什么Cygwin啊!直接在linux,对于企业里推荐用CentOS6.5,在学校里用Ubuntu. Mahout安装所需软件清单: 软件 ...

  3. 如何使用Linux套接字?

          我们知道许多应用程序,例如E-mail.Web和即时通信都依靠网络才能实现.这些应用程序中的每一个都依赖一种特定的网络协议,但每个协议都使用相同的常规网络传输方法.许多人都没有意识到网络协 ...

  4. 记2018/4/29 qbxt 测试

    记 2018/4/29  qbxt 测试(提高基础班) 简单的 NOIP 模拟赛 竞赛时间: 2018 年 4 月 29 日 13:30-17:00 题目名称 乘法 求和 计数 输入文件名 mul.i ...

  5. 【2017 Multi-University Training Contest - Team 7】 Euler theorem

    [Link]:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1005&cid=765 [Description] 问你a ...

  6. 【2017中国大学生程序设计竞赛 - 网络选拔赛】Palindrome Function

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6156 [题意] 已知函数f(x, k),如果10进制数x在k进制下是个回文数,那么f(x, k)值为k, ...

  7. method initializationerror not found:JUnit4单元測试报错问题

           今天使用JUnit 4进行单元測试时,測试程序一直执行不起来,报method initializationerror not found错误.例如以下:            网上说版本 ...

  8. 11. ZooKeeper之启动、停止服务。

    转自:https://blog.csdn.net/en_joker/article/details/78673607 启动服务 首先我们来看下如何启动ZooKeeper服务.常见的启动方式有两种. J ...

  9. 软件——protel 的pcb电路图制作

    近期一直在学习PCB板的绘制.

  10. cordova android

    Image.png Image.png http://www.jscss.cc/2016/10/18/cordova.html 装一个全局的cordova: 官网介绍:http://cordova.a ...