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引擎 ...
随机推荐
- .NET开源Protobuf-net组件葵花手册
一.前言 我们都知道 protobuf是由Google开发的一款与平台无关,语言无关,可扩展的序列化结构数据格式,可用做数据存储格式, 通信协议 ! 在前面<.NET开源Protobuf-net ...
- go微服务框架go-micro深度学习(五) stream 调用过程详解
上一篇写了一下rpc调用过程的实现方式,简单来说就是服务端把实现了接口的结构体对象进行反射,抽取方法,签名,保存,客户端调用的时候go-micro封请求数据,服务端接收到请求时,找到需要调用调 ...
- SQL DCL 数据控制语句
前言 DCL(Data Control Language)语句:数据控制语句,用于控制不同数据段直接的许可和访问级别的语句.这些语句定义了数据库.表.字段.用户的访问权限和安全级别.主要的语句关键字包 ...
- AYUI7 WPF MVC插件欣赏
AYUI7 MVC 提前预览 一个插件安装,享受所有快捷操作 静态图: 支持xaml中aymvc快速绑定多个操作 支持controller中 ayaction快速创建action代码块, 在AYU ...
- 抽奖 mark
https://blog.csdn.net/Oversdownload/article/details/77454006?utm_source=blogxgwz5
- CountDownLatch、CyclicBarrier和Semaphore 使用示例及原理
备注:博客园的markDown格式支持的特别不友好.也欢迎查看我的csdn的此篇文章链接:CountDownLatch.CyclicBarrier和Semaphore 使用示例及原理 CountDow ...
- 物联网架构成长之路(30)-Spring Boot Admin微服务WebUI监控
0. 前言 一个完整的微服务解决方案包含了许多微服务,基于我们需要观察各个微服务的运行状态,因此Spring Boot 生态提供了Spring Boot Admin 这个组件来实现微服务管理WEB U ...
- Nginx SSL 结合Tomcat 重定向URL变成HTTP的问题
http://www.siven.net/posts/d925bb5d.html *********************************************** 问题描述 由于要配置服 ...
- Syncfusion SfDataGrid 导出Excel
var options = new ExcelExportingOptions { ExcelVersion = ExcelVersion.Excel2013, }; //不需要导出的字段 optio ...
- 匿名函数gc分析
测试一:使用member function创建action会产生gc,不管该函数是否访问外部变量: private System.Action memberAct = null; // gc 112B ...