https://github.com/StephenGrider/GraphQLCasts
crud -> http -> url
在实际工作中往往会有这种情景出现:比如说我需要展示一个游戏名的列表,可接口却会把游戏的详细玩法,更新时间,创建者等各种各样的 (无用的) 信息都一同返回。
RESTFUL ROUTE的弊端:
graph —>edge ----> graph
理解我们是如何从这个graph中获取数据非常重要。
先找到user(id:23)的所有朋友,然后找到这些朋友的所有公司和职位;
query{
user(id:23){
friends {
company {
name
}
}
}
}
npm init
npm install --save express express-graphql graphql lodash
创建一个express(后台)应用:
const express = require('express');
const app = express();
app.listen(4000, () => {
console.log('listening')
});
express会检查请求,是否是请求graphql, 如果是graphql. 转交到graphql, graphql处理后,将response返回express,然后返回到前端。
主要的目的,注册graphql为一个middleware, 中间件
需要告诉graphQL, 数据是如何存储的,数据如何获取=>schem 文件
需要创建一个schema file.
const graphql = require('graphql');
const {GraphQLObjectType, GraphQLString, GraphQLInt} = graphql;
const UserType = new GraphQLObjectType({
name: 'User', // name
fields: { // properties
id: {type: GraphQLString}, // type
firstName: {type: GraphQLString},
age: {type: GraphQLInt},
}
});
总体来讲,类似于建模。
Root Query =>
user(id:23) =>
entry_point =>
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
user: {
type: UserType,
args: { id: {type: GraphQLString}},
resolve(parentValue, args){
}
}
}
});
const graphql = require('graphql');
const _ = require('lodash');
const {GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLSchema} = graphql;
const UserType = new GraphQLObjectType({
name: 'User', // name
fields: { // properties
id: {type: GraphQLString},
firstName: {type: GraphQLString},
age: {type: GraphQLInt},
}
});
const users = [
{id: "23", firstName: "Tao", age: 20},
{id: "47", firstName: "Tao", age: 20},
{id: "58", firstName: "Tao", age: 20},
];
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
user: {
type: UserType,
args: {id: {type: GraphQLString}},
resolve(parentValue, args) {
return _.find(users, {id: args.id})
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery,
});
graphql作为一个proxy, 去请求不同的数据源, 比如json, databases等
启动一个服务,使用graphQL请求服务,然后将服务返回给前端。
const graphql = require('graphql');
// const _ = require('lodash');
const axios = require('axios');
const {GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLSchema} = graphql;
const UserType = new GraphQLObjectType({
name: 'User', // name
fields: { // properties
id: {type: GraphQLString},
firstName: {type: GraphQLString},
age: {type: GraphQLInt},
}
});
// const users = [
// {id: "23", firstName: "Tao", age: 20},
// {id: "47", firstName: "Loop", age: 210},
// {id: "58", firstName: "Litmus", age: 201},
// ];
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
user: {
type: UserType,
args: {id: {type: GraphQLString}},
resolve(parentValue, args) {
return axios.get(`http://localhost:3000/users/${args.id}`)
.then(resp => resp.data)
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery,
});
每次更改server, 需要重启才能生效;
npm install --save nodemon
#package.json
"dev": "nodemon server.js"
npm run dev
{
"users": [
{
"id": "23",
"firstName": "zoe",
"age": 45,
"companyId": "1"
},
{
"id": "22",
"firstName": "tao jian",
"age": 45,
"companyId": "2"
},
{
"id": "24",
"firstName": "loop12312",
"age": 45,
"companyId": "2"
}
],
"companies": [
{
"id": "1",
"name": "Apple",
"description": " iphone"
},
{
"id": "2",
"name": "Google",
"description": "search"
}
]
}