Koa集成GraphQl

Koa 中集成 GraphQl

  1. 找到 koa-graphql 官方文檔

https://github.com/chentsulin/koa-graphql

  1. 安裝 koa-graphql graphql koa-mount

npm install graphql koa-graphql koa-mount --save

  1. 引入 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)
  1. 定義 GraphQLSchema
  • 新建 schema/default.js
  • 定義 Schema
const DB = require('../model/db.js');
const {
    GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLSchema, GraphQLList
} = require('graphql');
//定義導(dǎo)航 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 實現(xiàn)增刪改

//定義導(dǎo)航 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 實現(xiàn)增刪改
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: "測試導(dǎo)航", description: "描述"){
        title
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容