创建一个新的支持Mutation的Schema.

var GraphQLSchema = require('graphql').GraphQLSchema;

var GraphQLObjectType = require('graphql').GraphQLObjectType;

var GraphQLString = require('graphql').GraphQLString;

var GraphQLList = require('graphql').GraphQLList;

var buildSchema  = require('graphql').buildSchema;

var fetch = require('node-fetch');

require("babel-polyfill");

// Construct a schema, using GraphQL schema language

var schema = buildSchema(

'    input MessageInput {'

+ '        content: String'

+ '        author: String'

+ '    }'

+ ''

+ '    type Message {'

+ '        id: ID !'

+ '        content: String'

+ '        author: String'

+ '    }'

+ ''

+ '    type Query {'

+ '        getMessage(id: ID!): Message'

+ '    }'

+ ''

+ '    type Mutation {'

+ '         createMessage(input: MessageInput): Message'

+ '         updateMessage(id: ID!, input: MessageInput): Message'

+ '    }'

);

// If Message had any complex fields, we'd put them on this object.

class Message {

constructor(id, {content, author}) {

this.id = id;

this.content = content;

this.author = author;

}

}

// Maps username to content

var fakeDatabase = {};

var root = {

getMessage: function ( { id }) {

if (!fakeDatabase[id]) {

throw new Error('no message exists with id ' + id);

}

return new Message(id, fakeDatabase[id]);

},

createMessage: function ({input}) {

// Create a random id for our "database".

var id = require('crypto').randomBytes(10).toString('hex');

fakeDatabase[id] = input;

return new Message(id, input);

},

updateMessage: function ({id, input}) {

if (!fakeDatabase[id]) {

throw new Error('no message exists with id ' + id);

}

// This replaces all old data, but some apps might want partial update.

fakeDatabase[id] = input;

return new Message(id, input);

}

};

module.exports.Schema = schema;

module.exports.Root = root;

创建一个新的router来使用这个Schema:

var express = require('express');

var graphQLHTTP = require('express-graphql');

var schema = require('../schemas/Schema2').Schema;

var root = require('../schemas/Schema2').Root;

var router = express.Router();

router.use(graphQLHTTP({

schema: schema,

rootValue: root,

graphiql : true

}));

module.exports = router;

客户端的测试代码如下:

app.js:

//Mutation

var Test7 = require('./Test7');

Test7.Execute();

Test7.js

//Test7: Mutation

var gRequest = require('graphql-request').request;

var util = require('util');

exports.Execute = function () {

var query = 'mutation CreateMessage($input: MessageInput) {'

+ '  createMessage(input: $input) {'

+ '    id,'

+ '    author,'

+ '    content'

+ '  }'

+ '}' ;

var varibles1 =

{

"input":

{

"author": "Tom",

"content": "this is my message"

}

};

//gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(data) });

gRequest('http://localhost:1337/graphql2/graphql', query, varibles1).then(function (data) {

console.log(util.inspect(data, { showHidden: false, depth: null }))

});

};

执行结果如下:

{ createMessage:

{ id: '48ed1228a3b390909365',

author: 'Tom',

content: 'this is my message' } }

示例来自: https://graphql.org/graphql-js/mutations-and-input-types/

GraphQL入门3(Mutation)的更多相关文章

  1. GraphQL入门有这一篇就足够了

    GraphQL入门有这一篇就足够了:https://blog.csdn.net/qq_41882147/article/details/82966783 版权声明:本文为博主原创文章,遵循 CC 4. ...

  2. Vue项目中GraphQL入门学习与应用

    1.GraphQL是什么,能干什么? 正如官网所说,GraphQL是一种用于API查询的语言.Facebook 的移动应用从 2012 年就开始使用 GraphQL.GraphQL 规范于 2015 ...

  3. Graphql入门

    Graphql入门 GraphQL是一个查询语言,由Facebook开发,用于替换RESTful API.服务端可以用任何的语言实现.具体的你可以查看Facebook关于GraphQL的文档和各种语言 ...

  4. GraphQL入门1

    1. 资源: 主站: https://graphql.org/ 中文站: http://graphql.cn 入门视频: https://graphql.org/blog/rest-api-graph ...

  5. 《分享》Graphql入门与实践

    最近项目用到了graphql,学习了一些并在公司做了一个小分享,希望对你有帮助 一.介绍 Graphql是一种面向数据的API查询语言 Graphql给前端提供一种强力的查询工具,我们可以根据自己定义 ...

  6. GraphQL 入门介绍

    写在前面 GraphQL是一种新的API标准,它提供了一种更高效.强大和灵活的数据提供方式.它是由Facebook开发和开源,目前由来自世界各地的大公司和个人维护.GraphQL本质上是一种基于api ...

  7. GraphQL入门2

    将服务器端的代码升级了一下: var GraphQLSchema = require('graphql').GraphQLSchema; var GraphQLObjectType = require ...

  8. [GraphQL] Apollo React Mutation Component

    In this lesson I refactor a React component that utilizes a higher-order component for mutations to ...

  9. GraphQL快速入门教程

    摘要: 体验神奇的GraphQL! 原文:GraphQL 入门详解 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. GraphQL简介 定义 一种用于API调用的数据查询语言 ...

随机推荐

  1. Laravel attribute casting 导致的 Indirect modification of overloaded property

    在 Laravel model 中,设置了某个属性做 array casting. protected $casts = [ 'rounds' => 'array', ]; 但是在 contro ...

  2. MySQL----数据库操作2

    数据库高级操作: SHOW DATABASES; 显示数据库 CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci CR ...

  3. cf799c 树状数组魔改

    这题的树状数组是用来维护区间最大值的!非常神奇 第一次见到这种用法,其实和区间求和也没什么差别 /* 树状数组魔改版 有三种方案:选两种c,选两个d,选一个c一个d 前两种方案需要选出符合条件的魅力值 ...

  4. git代码提交步骤,教程

    代码提交 代码提交一般有五个步骤: 1.查看目前代码的修改状态 2.查看代码修改内容 3.暂存需要提交的文件 4.提交已暂存的文件 5.同步到服务器 1.     查看目前代码的修改状态 提交代码之前 ...

  5. JQuery编写自己的插件(七)

    一:jQuery插件的编写基础1.插件的种类编写插件的目的是给一系列已经方法或函数做一个封装,以便在其他地方重复使用,方便后期维护和提高开发效率.常见的种类有以下三种:封装对象方法的插件

  6. 【C++ Primer | 15】C++虚函数表剖析①

    概述 为了实现C++的多态,C++使用了一种动态绑定的技术.这个技术的核心是虚函数表(下文简称虚表).本文介绍虚函数表是如何实现动态绑定的. C++多态实现的原理: •  当类中声明虚函数时,编译器会 ...

  7. Flink(三)Flink开发IDEA环境搭建与测试

    一.IDEA开发环境 1.pom文件设置 <properties> <maven.compiler.source>1.8</maven.compiler.source&g ...

  8. centos安装autossh

    $ sudo yum install wget gcc make$ wget http://www.harding.motd.ca/autossh/autossh-1.4e.tgz$ tar -xf ...

  9. MySQL存储过程整理

    MySQL存储过程 2018-08-15  23:00:06 1.存储过程介绍 (1) 定义:存储过程是存储在数据库目录中的一段声明性SQL语句. 触发器,其他存储过程以及java,python,ph ...

  10. BZOJ1042 [HAOI2008]硬币购物 完全背包 容斥原理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1042 题目概括 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了t ...