[GraphQL] Write a GraphQL Mutation
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的更多相关文章
- [GraphQL] Write a GraphQL Schema in JavaScript
Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but ...
- [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 ...
- [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 ...
- ASP.NET Core中使用GraphQL - 第七章 Mutation
ASP.NET Core中使用GraphQL - 目录 ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间 ...
- [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 ...
- graphql cli 开发graphql api flow
作用 代码生成 schema 处理 脚手架应用创建 项目管理 安装cli npm install -g graphql-cli 初始化项目(使用.graphqlconfig管理) 以下为demo de ...
- Reusing & Composing GraphQL APIs with GraphQL Bindings
With GraphQL bindings you can embed existing GraphQL APIs into your GraphQL server. In previous blog ...
- [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 ...
- 使用Hot Chocolate和.NET 6构建GraphQL应用(1)——GraphQL及示例项目介绍
系列导航 使用Hot Chocolate和.NET 6构建GraphQL应用文章索引 前言 这篇文章是这个系列的第一篇,我们会简单地讨论一下GraphQL,然后介绍一下这个系列将会使用的示例项目. 关 ...
随机推荐
- onvif开发实战1--总结框架搭建
Gsoap及开发框架生成: 一:gsoap下载和编译 1.下载Gsoap:地址:http://sourceforge.net/projects/gsoap2/files/gSOAP/ 2.安装: ...
- JAVA Mail邮件实现发送
package com.test;import java.util.Date;import java.util.Properties;import javax.mail.Message;import ...
- 【Codeforces Round #453 (Div. 2) C】 Hashing Trees
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然只有当a[i]和a[i-1]都大于1的时候才会有不同的情况. a[i] >= a[i-1] 且a[i-1]>=2 则 ...
- js检查元素是否包括在数组中
说明 在系统中须要检查税率填写的正确性,一定是国家规定的某几种税率,当然能够通过if else进行校验,可是还能够使用定义一个数组然后校验是否包括在元素中进行校验. 长处:加入税率无需改动逻辑,仅仅须 ...
- javascript进阶教程第二章对象案例实战
javascript进阶教程第二章对象案例实战 一.学习任务 通过几个案例练习回顾学过的知识 通过案例练习补充几个之前没有见到或者虽然讲过单是讲的不仔细的知识点. 二.具体实例 温馨提示 面向对象的知 ...
- JavaScript---call()使用的一些疑问
疑问:在使用.call()时,调用对象到底是否可以直接拥有了被调用者的方法和属性? 这里输出结果为:ReferenceError: o is not defined function Person(n ...
- 学习笔记:Vue——处理边界情况
访问元素&组件 01.访问根实例 $root // Vue 根实例 new Vue({ data: { foo: 1 }, computed: { bar: function () { /* ...
- js取对象的属性值循环
var data = {name: "liuyang", job: "web", age: "27"} Object.keys(data). ...
- DataTable +chart控件
//这是仿你的DataTable //-----开始--------- DataTable dataTable1 = new System.Data.DataTable(); dataTable1.C ...
- Tomcat redis 配置
http://www.cnblogs.com/interdrp/p/4868740.html http://blog.csdn.net/qq584852076/article/details/4650 ...