# GraphQL 入门
GraphQL (opens new window) 是一种用于 API 的查询语言,是一个使用基于类型系统来执行查询的服务端运行时。
# 简单案例
let express = require('express');
let graphqlHTTP = require('express-graphql');
let { buildSchema } = require('graphql');
const schema = buildSchema(`
type Query {
me: User
age: Int
sex: String
}
type User {
id: ID
name: String
}
`);
const root = {
me: () => {
return {
id: 1,
name: 'Hishion'
}
},
age: () => {
return 27
},
sex: () => {
return 'male'
}
};
let app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
← Linux实用命令 GET和POST的区别 →