express.js graphql express-graphql
创建应用
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的更多相关文章
- 《Pro Express.js》学习笔记——概述
要学Node.js,先学Express.js. Express.js是Node.js官方推荐的基础框架. Express.js框架经过一系列的发展,已经到了4.x版本.新的版本解决了3.x之前版本的依 ...
- 透析Express.js
前言 最近,本屌在试用Node.js,在寻找靠谱web框架时发现了Express.js.Express.js在Node.js社区中是比较出名web框架,而它的定位是“minimal and flexi ...
- Node.js、Express、Socket.io 入门
前言 周末断断续续的写了第一个socket.io Demo.初次接触socket.io是从其官网看到的,看着get started做了一遍,根据官网的Demo能提供简单的服务端和客户端通讯. 这个De ...
- Node.js、express、mongodb 实现分页查询、条件搜索
前言 在上一篇Node.js.express.mongodb 入门(基于easyui datagrid增删改查) 的基础上实现了分页查询.带条件搜索. 实现效果 1.列表第一页. 2.列表第二页 3. ...
- Node.js、express、mongodb 入门(基于easyui datagrid增删改查)
前言 从在本机(win8.1)环境安装相关环境到做完这个demo大概不到两周时间,刚开始只是在本机安装环境并没有敲个Demo,从周末开始断断续续的想写一个,按照惯性思维就写一个增删改查吧,一方面是体验 ...
- 《Pro Express.js》学习笔记——Express服务启动常规七步
Express服务启动常规七步 1. 引用模块 var express=require('express'), compression=require('compression'), bo ...
- 在 node.js 的 express web 框架中自动注册路由
该方法主要是动态注册自己写的 router . 注册器 router 文件名为 loader.js . var express = require('express'); var fs = requ ...
- Node.js的高性能封装 Express.js
Express 是一个简洁而灵活的 node.js Web应用框架, 提供一系列强大特性帮助你创建各种Web应用.Express 不对 node.js 已有的特性进行二次抽象,我们只是在它之上扩展了W ...
- node.js和express.js安装和使用步骤 [windows]
PS: NODEJS:https://nodejs.org NPM:https://www.npmjs.com/ 一.node.js安装与配置 到https://nodejs.org/en/downl ...
- node.js入门及express.js框架
node.js介绍 javascript原本只是用来处理前端,Node使得javascript编写服务端程序成为可能.于是前端开发者也可以借此轻松进入后端开发领域.Node是基于Google的V8引擎 ...
随机推荐
- 介绍三款串口监控工具:Device Monitoring Studio,portmon,Comspy
在开发上位机下位机通讯程序时,有一个好的监控工具会事半功倍.特在网上找了几款串口监控软件,作了简单对比: 一.Device Monitoring Studio 网址:http://www.hhdsof ...
- C# 坐标系
C#坐标系 一.概述 从数学角度讲,Point是一个二维矢量,包含两个公共整型属性,属性用大写X和Y(c#中公共属性一般约定以大写字母开头).当坐标不是整数值是float时,用PointF代替Poin ...
- 在.NET下如何预防XXE注入攻击
接下来关于.NET中XXE注入的内容来自Dean Fleming单元测试的Web站点:https://github.com/deanf1/dotnet-security-unit-tests.该站点覆 ...
- C#缓存absoluteExpiration、slidingExpiration两个参数的疑惑
看了很多资料终于搞明白cache中absoluteExpiration,slidingExpiration这两个参数的含义. absoluteExpiration:用于设置绝对过期时间,它表示只要时间 ...
- 【Linux】磁盘读写 测试
一.如何查看当前磁盘的IO使用情况 使用命令:iotop Total DISK READ: 3.89 K/s | Total DISK WRITE: 0.00 B/s TID PRIO USER DI ...
- 删除 nuget 文件夹内容
vs2017 ->工具->选项->NuGet 包管理器->清除所有NuGet缓存
- Linux报swap空间占用过高,但物理内存还有空余
收到报警,swap空间占用过高,登录到系统查看内存使用详情,看到物理内存还有很多未使用 问题分析 Swap配置对性能的影响分配太多的Swap空间会浪费磁盘空间,而Swap空间太少,则系统会发生错误.如 ...
- Google I/O 官方应用中的动效设计
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jILRvRTrc/article/details/82881743 作者:Nick Butcher, ...
- Atitit 华为管理者内训书系 以奋斗者为本 华为公司人力资源管理纲要 attilax读后感
Atitit 华为管理者内训书系 以奋斗者为本 华为公司人力资源管理纲要 attilax读后感 序 言上篇:价值创造.评价与分配第一章 全力创造价值1.1 围绕价值创造展开人力资源管理1.1.1 什 ...
- [k8s] flexvolume workflow