GraphQL学习第五篇 -GraphQl 中定义schema实现增、删、改、查。

const DB = require('../model/db.js');

const {
    GraphQLObjectType,
    GraphQLString,
    GraphQLInt,
    GraphQLList,
    GraphQLSchema,
    GraphQLID,
    GraphQLFloat,
    GraphQLNonNull
} = require('graphql')

// 1.定义导航的schema,映射要操作表的字段
var NavSchema = new GraphQLObjectType({
    name: 'nav',
    fields: {
        _id: { type: GraphQLString },
        title: {type: GraphQLString}, 
        url: {type: GraphQLString},
        sort: {type: GraphQLInt},
        status: {type: GraphQLString},
        add_time: {type: GraphQLString}
    }
})

// 2.1.定义一个根,配置查询的方法
var RootSchema = new GraphQLObjectType({
    name: 'root',
    fields: {
        navList: {
            type: GraphQLList(NavSchema),
            async resolve(parent, args) {
                var navList = await DB.find('nav', {});
                return navList;
            }
        },
        oneNavList: {
            type: NavSchema,
            args: {
                _id: {type: GraphQLString}
            },
            async resolve(parent, args) {
                var oneNavList = await DB.find('nav', { "_id": DB.getObjectId(args._id)});
                return oneNavList[0];
            }
        }
    }
})

// 2.2.定义一个根,配置增加、修改、删除的方法
var MutationSchema=new GraphQLObjectType({
    name:"mutation",
    fields:{
        // 添加的方法
        addNav:{
            // 数据类型
            type:NavSchema,
            // 接收的参数
            args:{
                title: {type: new GraphQLNonNull(GraphQLString)},
                url: {type: GraphQLNonNull(GraphQLString)},
                sort: {type: GraphQLInt},
                status: {type: GraphQLString},
                add_time: {type: GraphQLString}
            },
            // 操作的方法
            async resolve(parent, args) {
                var result = await DB.insert('nav', {
                    title:args.title,
                    url:args.url,
                    sort:args.sort,
                    status:args.status,
                    add_time:new Date().getTime()
                });
                return result.ops[0];
            }
        },
        // 编辑的方法
        editNav:{
            type:NavSchema,
            args:{
                // GraphQLNonNull代表必传字段
                _id:{type: new GraphQLNonNull(GraphQLString)},
                title: {type: new GraphQLNonNull(GraphQLString)},     
                url: {type: GraphQLNonNull(GraphQLString)},
                sort: {type: GraphQLInt},
                status: {type: GraphQLString},
                add_time: {type: GraphQLString}
            },
            async resolve(parent, args) {
                await DB.update('nav', {"_id":DB.getObjectId(args._id)},{
                    title:args.title,
                    url:args.url,
                    sort:args.sort,
                    status:args.status,
                    add_time:new Date().getTime()
                });
                return {
                    _id:args._id,
                    title:args.title,
                    url:args.url,
                    sort:args.sort,
                    status:args.status,
                    add_time:new Date().getTime()
                }
            }
        },
        deleteNav:{
            type:NavSchema,
            args:{
                _id:{type: new GraphQLNonNull(GraphQLString)},
            },
            async resolve(parent, args) {
                var oneNavList = await DB.find('nav', { "_id": DB.getObjectId(args._id)});
                var deleteResult = await DB.remove('nav', {"_id":DB.getObjectId(args._id)});
                // 用影响的行业来判断是否成功
                if(deleteResult.result.n){
                    return oneNavList[0];  
                }else{
                    return {}
                }
            }
        }  
    }
})

// 3.将定义的所有的根,挂载到GraphQLSchema上
module.exports = new GraphQLSchema({
    query: RootSchema,
    mutation:MutationSchema
})

 

发布了248 篇原创文章 · 获赞 205 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览