GraphQL入门3(Mutation)
创建一个新的支持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)的更多相关文章
- GraphQL入门有这一篇就足够了
GraphQL入门有这一篇就足够了:https://blog.csdn.net/qq_41882147/article/details/82966783 版权声明:本文为博主原创文章,遵循 CC 4. ...
- Vue项目中GraphQL入门学习与应用
1.GraphQL是什么,能干什么? 正如官网所说,GraphQL是一种用于API查询的语言.Facebook 的移动应用从 2012 年就开始使用 GraphQL.GraphQL 规范于 2015 ...
- Graphql入门
Graphql入门 GraphQL是一个查询语言,由Facebook开发,用于替换RESTful API.服务端可以用任何的语言实现.具体的你可以查看Facebook关于GraphQL的文档和各种语言 ...
- GraphQL入门1
1. 资源: 主站: https://graphql.org/ 中文站: http://graphql.cn 入门视频: https://graphql.org/blog/rest-api-graph ...
- 《分享》Graphql入门与实践
最近项目用到了graphql,学习了一些并在公司做了一个小分享,希望对你有帮助 一.介绍 Graphql是一种面向数据的API查询语言 Graphql给前端提供一种强力的查询工具,我们可以根据自己定义 ...
- GraphQL 入门介绍
写在前面 GraphQL是一种新的API标准,它提供了一种更高效.强大和灵活的数据提供方式.它是由Facebook开发和开源,目前由来自世界各地的大公司和个人维护.GraphQL本质上是一种基于api ...
- GraphQL入门2
将服务器端的代码升级了一下: var GraphQLSchema = require('graphql').GraphQLSchema; var GraphQLObjectType = require ...
- [GraphQL] Apollo React Mutation Component
In this lesson I refactor a React component that utilizes a higher-order component for mutations to ...
- GraphQL快速入门教程
摘要: 体验神奇的GraphQL! 原文:GraphQL 入门详解 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. GraphQL简介 定义 一种用于API调用的数据查询语言 ...
随机推荐
- mysql8.0CTE实现递归查询
+----+----------+--------------+| ID | ParentID | name |+----+----------+--------------+| 1 ...
- 使用mybatis-spring-boot-starter如何打印sql语句
只需要将接口文件的日志设置为debug即可. 例如你的mapper接口所在的文件夹是 com.demo.mapper 那么在application.properties配置文件中添加 logging. ...
- SpringMVC MongoDB之“基本文档查询(Query、BasicQuery)”
一.简介 spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...
- 《剑指offer》-斐波那契数列
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项. n<=39 这么直接的问fibonacci,显然是迭代计算.递归的问题在于重复计算,而迭代则避免了这一点:递归是自 ...
- Codeforces 757 C Felicity is Coming!
题目大意:有n个训练营,m种宠物,每个训练营里里面有gi 个宠物,现在每只宠物都要完成一次进化,种类 相同的宠物进化之后,种类还是相同,种类不同的宠物不能进化成相同种类,且要求所有宠物进化之后,每个 ...
- unity pattern not found
在之前破解成功再次破解或者电脑上多个版本Unity往往会导致破解失败. 破解失败解决方法如下: 先用破解软件破解,虽然是破解失败但是还是可以在破解的目录下找到那个ulf文件. 这时候直接打开unity ...
- 《Gradle权威指南》--Android Gradle高级自定义
No1: 指定共享库 <uses-library android:name="com.google.android.maps" android:required=" ...
- AngularJS之双向数据绑定,class绑定
之前一直都是用vue来完成一些日常开发,初入AngularJS,记录一些日常开发遇到的问题. 1.双向数据绑定 AngularJS与vue的区别在于,vue采用的是虚拟DOM,模板文件上绑定的一大堆指 ...
- Window环境下,PHP调用Python脚本
参考 php调用python脚本*** php 调用 python脚本的方法 解决办法:php提供了许多调用其他脚本或程序的方法,比如exec/system/popen/proc_open/passt ...
- 模拟页面获取的php数据(三)
<?php return array( "aData" => [//通勤方式 "trafficType" => [ 0 => [ &qu ...