GraphQL
GraphQL是一个用于API的查询语言。 是一个使用基于类型系统来执行查询的服务端运行时。
GraphQL服务
通过定义类型和类型上的字段,然后给每个类型里的每个字段提供解析函数。
type UserStatus {
authUser: User
}
type User {
name: String
}
javascript实现
服务端代码
const { graphql, buildSchema } = require('graphql');
const schema = buildSchema(`
type User {
id: String,
username: String,
password: String
}
type Query {
user: User
}
`);
const users = {
'001': {
id: '001',
username: 'fancy',
password: '123'
},
'002': {
id: '002',
username: 'sword',
password: '123'
}
};
const userRoot = {
user: ({id}) => users[id]
};
const query = request.query.query;
graphql(schema, query, userRoot).then((result) =>{
//...
});
客户端请求
params:
query = query {
user(id: "001") {
id,
username,
password
}
}