文档

创建应用

const l = console.log;
var express = require("express");
var graphqlHTTP = require("express-graphql");
var { buildSchema } = require("graphql"); const cats = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" }]; var schema = buildSchema(`
type Query { hello: String cats: [Cat] findCat(id: ID!): Cat
} type Cat {
id: Int
name: String
}
`); // root 提供所有 API 入口端点相应的解析器函数
var root = {
hello: () => "Hello world 233!",
cats: () => cats,
findCat({id}) {
return cats.find(el => el.id === Number(id));
}
}; var app = express(); // 根节点
app.use(
"/graphql",
graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true
})
);
app.listen(4000);

基本类型 String、Int、Float、Boolean 和 ID , String! 表示非空字符串,[Int] 表示一个整数列表

查询

http://localhost:4000/graphql?query={ cats { id name } }  // [Cat]

http://localhost:4000/graphql?query={ findCat(id: 2){ name } }  // Cat.name

http://localhost:4000/graphql?query={ hello } // Hello world 233!

http://localhost:4000/graphql?query={ findCat(id: 2) {name} hello } // data:{ findCat:{ "name": "b" }, hello: "Hello world 233!" }

let r = await axios.post("http://localhost:4000/graphql", {query: `{ findCat(id: ${id}) { name } hello}`});  // axios

增加数据 和 更新数据

  type Mutation {
createCat(cat: CreateCat): [Cat]
updateCat(id: ID!, name: String!): [Cat]
} type Cat {
id: Int
name: String
} input CreateCat {
name: String!
} var root = {
createCat({ cat }) {
cats.push({ id: cats.length + 1, name: cat.name });
return cats;
},
updateCat({ id, name }) {
cats.find(el => el.id == id).name = name;
return cats;
}
};
mutation {
updateCat(id: 2, name: "new name") {
name id
}
} mutation {
createCat(cat:{name: "ajanuw"}) {
name id
}
} // axios 实现 createCat
const query = `mutation createCat($cat: CreateCat) {
createCat(cat: $cat) {
name
}
}`;
async function main() {
let r = await axios.post("http://localhost:4000/graphql", {
query,
variables: {
cat: {
name: 'hello'
}
}
});
l(r.data.data.createCat);
} // updateCat
const query = `mutation updateCat($id: ID!, $name: String!) {
updateCat(id: $id, name: $name) {
name
}
}`;
async function main() {
let r = await axios.post("http://localhost:4000/graphql", {
query,
variables: {
id: 2,
name: '233'
}
});
l(r.data.data.updateCat);
}

获取req 对象

var schema = buildSchema(`
type Query {
ip(msg: String): String
}
`); var root = {
ip(args, req) {
l(args);
return req.ip;
}
}; query={ ip(msg: "233") }

express.js graphql express-graphql的更多相关文章

  1. 《Pro Express.js》学习笔记——概述

    要学Node.js,先学Express.js. Express.js是Node.js官方推荐的基础框架. Express.js框架经过一系列的发展,已经到了4.x版本.新的版本解决了3.x之前版本的依 ...

  2. 透析Express.js

    前言 最近,本屌在试用Node.js,在寻找靠谱web框架时发现了Express.js.Express.js在Node.js社区中是比较出名web框架,而它的定位是“minimal and flexi ...

  3. Node.js、Express、Socket.io 入门

    前言 周末断断续续的写了第一个socket.io Demo.初次接触socket.io是从其官网看到的,看着get started做了一遍,根据官网的Demo能提供简单的服务端和客户端通讯. 这个De ...

  4. Node.js、express、mongodb 实现分页查询、条件搜索

    前言 在上一篇Node.js.express.mongodb 入门(基于easyui datagrid增删改查) 的基础上实现了分页查询.带条件搜索. 实现效果 1.列表第一页. 2.列表第二页 3. ...

  5. Node.js、express、mongodb 入门(基于easyui datagrid增删改查)

    前言 从在本机(win8.1)环境安装相关环境到做完这个demo大概不到两周时间,刚开始只是在本机安装环境并没有敲个Demo,从周末开始断断续续的想写一个,按照惯性思维就写一个增删改查吧,一方面是体验 ...

  6. 《Pro Express.js》学习笔记——Express服务启动常规七步

    Express服务启动常规七步 1.       引用模块 var express=require('express'), compression=require('compression'), bo ...

  7. 在 node.js 的 express web 框架中自动注册路由

    该方法主要是动态注册自己写的 router . 注册器 router 文件名为 loader.js  . var express = require('express'); var fs = requ ...

  8. Node.js的高性能封装 Express.js

    Express 是一个简洁而灵活的 node.js Web应用框架, 提供一系列强大特性帮助你创建各种Web应用.Express 不对 node.js 已有的特性进行二次抽象,我们只是在它之上扩展了W ...

  9. node.js和express.js安装和使用步骤 [windows]

    PS: NODEJS:https://nodejs.org NPM:https://www.npmjs.com/ 一.node.js安装与配置 到https://nodejs.org/en/downl ...

  10. node.js入门及express.js框架

    node.js介绍 javascript原本只是用来处理前端,Node使得javascript编写服务端程序成为可能.于是前端开发者也可以借此轻松进入后端开发领域.Node是基于Google的V8引擎 ...

随机推荐

  1. sqlserver数据库出现可疑错误修复方法

    一.主数据库出现可疑修复方法: 第一种方法: 当数据库发生这种操作故障时,可以按如下操作步骤可处理此要领,打开数据库里的Sql查询编辑器窗口,运行以下的命令: ?修改数据库为紧急模式 ALTER DA ...

  2. PDF.js实现个性化PDF渲染(文本复制)

    我肥来啦

  3. bootstrap-table方法之:合并单元格

    方法一 通过mergeCells方法 演示地址:http://issues.wenzhixin.net.cn/bootstrap-table/methods/mergeCells.html Merge ...

  4. Fiddler Composer 模拟post请求

    在模拟post请求的时候,发现服务器端无法接收post参数 发现原来的请求表头的设置问题加上表头 Content-Type: application/x-www-form-urlencoded 后正常

  5. 小程序学习笔记二:页面文件详解之 .json文件

       页面配置文件—— pageName.json 每一个小程序页面可以使用.json文件来对本页面的窗口表现进行配置,页面中配置项会覆盖 app.json 的 window 中相同的配置项. 页面的 ...

  6. 读取mysql数据库的数据,转为json格式

    # coding=utf-8 ''' Created on 2016-10-26 @author: Jennifer Project:读取mysql数据库的数据,转为json格式 ''' import ...

  7. 在Ubuntu18.04下配置hadoop集群

    服务器准备 启动hadoop最小集群的典型配置是3台服务器, 一台作为Master, NameNode, 两台作为Slave, DataNode. 操作系统使用的Ubuntu18.04 Server, ...

  8. 【转】VS2015详细安装步骤

    亲身经历记录下来,以备后用.也希望能够帮助到有需要的朋友们! 1.安装之前首先下载VS2015,下载地址: [VS2015社区版官方中文版下载]:http://download.microsoft.c ...

  9. 【PHP】解析PHP中的函数

    目录结构: contents structure [-] 可变参数的函数 变量函数 回调函数 自定义函数库 闭包(Closure)函数的使用 在这篇文章中,笔者将会讲解如何使用PHP中的函数,PHP是 ...

  10. mycat偶尔会出现JVM报错double free or corruption并崩溃退出

    mycat偶尔会出现JVM报错double free or corruption并崩溃退出 没有复杂的sql,也没有大量的io INFO | jvm | // :: | *** Error in `j ...