Koa 中集成 GraphQl
- 找到 koa-graphql 官方文档
- 安装 koa-graphql graphql koa-mount
npm install graphql koa-graphql koa-mount --save
- 引入 koa-graphql 配置中间件
const Koa = require('koa');
const mount = require('koa-mount');
const graphqlHTTP = require('koa-graphql');
const GraphQLSchema = require('./schema/default.js');
const app = new Koa();
app.use(mount('/graphql', graphqlHTTP({
schema: GraphQLSchema, graphiql: true
})));
app.listen(4000)
- 定义 GraphQLSchema
- 新建 schema/default.js
- 定义 Schema
const DB = require('../model/db.js');
const {
GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLSchema, GraphQLList
} = require('graphql');
//定义导航 Schema 类型
var GraphQLNav = new GraphQLObjectType({
name: 'nav', fields: {
title: { type: GraphQLString }, url: { type: GraphQLString }, sort: { type: GraphQLInt }, status: { type: GraphQLInt }, add_time: { type: GraphQLString }
}
})
//定义根
var Root = new GraphQLObjectType({
name: "RootQueryType", fields: {
navList: {
type: GraphQLList(GraphQLNav), async resolve(parent, args) {
var navList = await DB.find('nav', {});
console.log(navList)
return navList;
}
}
}
})
module.exports = new GraphQLSchema({
query: Root
});
Koa 中 GraphQl API 聚合
//文章分类
var GraphQLArticleCate = new GraphQLObjectType({
name: 'articleCate', fields: {
_id: { type: GraphQLID }, title: { type: GraphQLString }, description: { type: GraphQLString }, keywords: { type: GraphQLInt }, pid: { type: GraphQLInt }, add_time: { type: GraphQLString }, status: { type: GraphQLInt }
}
});
//文章列表
var GraphQLArticleList = new GraphQLObjectType({
name: 'articleList', fields: {
_id: { type: GraphQLID }, pid: { type: GraphQLID }, catename: { type: GraphQLString }, title: { type: GraphQLString }, author: { type: GraphQLString }, status: { type: GraphQLInt },
is_best: { type: GraphQLInt },
is_hot: { type: GraphQLInt },
is_new: { type: GraphQLInt }, keywords: { type: GraphQLString }, description: { type: GraphQLString }, content: { type: GraphQLString }, sort: { type: GraphQLInt }, cateList: {
type: GraphQLArticleCate, async resolve(parent, args) {
var cateList = await DB.find('articlecate', { '_id': DB.getObjectId(parent.pid) });
return cateList[0]
}
}
}
});
Koa 中使用 GraphQl 实现增删改
//定义导航 Schema 类型
var GraphQLNav = new GraphQLObjectType({
name: 'nav', fields: {
title: { type: GraphQLString }, url: { type: GraphQLString }, sort: { type: GraphQLInt }, status: { type: GraphQLInt }, add_time: { type: GraphQLString }
}
});
//定义根 MutationRoot 实现增删改
const MutationRoot = new GraphQLObjectType({
name: "Mutation", fields: {
addNav: {
type: GraphQLNav, args: {
title: { type: new GraphQLNonNull(GraphQLString) }, description: { type: new GraphQLNonNull(GraphQLString) }, keywords: { type: GraphQLString }, pid: { type: GraphQLString }, add_time: { type: GraphQLString }, status: { type: GraphQLID }
}, async resolve(parent, args) {
var cateList = await
DB.insert('nav', {
title: args.title, description: args.description, keywords: args.keywords, pid: 0, add_ti
me: '', status: 1
});
console.log(cateList.ops[0]);
return cateList.ops[0];
}
}
}
})
//挂载到 GraphQLSchema 的 mutation 字段里面
module.exports = new GraphQLSchema({
query: QueryRoot,
mutation: MutationRoot
});
//客户端工具中如下使用
mutation{
addNav(title: "测试导航", description: "描述"){
title
}
}