UNPKG

fridaybe

Version:
240 lines (231 loc) 7.15 kB
const graphql = require('graphql'); const { GraphQLJSON, GraphQLJSONObject } = require('graphql-type-json'); const { GraphQLObjectType, GraphQLString, GraphQLID, GraphQLInt,GraphQLSchema, GraphQLList,GraphQLNonNull, GraphQLBoolean, GraphQLTimestamp } = graphql; const CategoryType = new GraphQLObjectType({ name:"CT", fields: () => ({ category: { type: GraphQLString }, authorID: { type: GraphQLString }, templates:{ type: new GraphQLList(TemplateType), resolve(parent, args){ return Template.find({authorID: parent.authorID,category:parent.category}); } } }) }); const TemplateType = new GraphQLObjectType({ name:"TT", fields: () => ({ _id: { type: GraphQLID }, name: { type: GraphQLString }, content:{ type: GraphQLString }, authorID: { type: GraphQLString }, category: { type: GraphQLString }, tags: { type: GraphQLString } }) }); const AuthorType = new GraphQLObjectType({ name: 'AT', fields: () => ({ _id: { type: GraphQLID }, NTID: { type: GraphQLString }, categories:{ type: new GraphQLList(CategoryType), resolve(parent, args){ var author_cate_array=[]; return Template.distinct('category',{authorID: parent.NTID}).then(res => res.forEach(element => author_cate_array.push({"category":element,"authorID": parent.NTID}))).then(()=> {return author_cate_array}); } } }) }); const SysvarType = new GraphQLObjectType({ name:"SVT", fields: () => ({ _id: { type: GraphQLID }, varname: { type: GraphQLString }, varval: { type: GraphQLString }, createdAt: { type: GraphQLString }, updatedAt: { type: GraphQLString } }) }); const NotificationcaseType = new GraphQLObjectType({ name:"NCT", fields: () => ({ _id: { type: GraphQLID }, caseid: { type: GraphQLString }, merchantflag: { type: GraphQLString }, caseage: { type: GraphQLString }, casehref: { type: GraphQLString }, createdAt: { type: GraphQLString }, updatedAt: { type: GraphQLString } }) }); const NotificationeventType = new GraphQLObjectType({ name:"NET", fields: () => ({ _id: { type: GraphQLID }, cases: { type: new GraphQLList(NotificationcaseType) }, createdAt: { type: GraphQLString }, updatedAt: { type: GraphQLString } }) }); const RootQuery = new GraphQLObjectType({ name: 'RootQueryType', fields: { sysvar: { type: SysvarType, args: { varname: { type: new GraphQLNonNull(GraphQLString) } }, resolve(parent, args) { return Sysvar.findOne({varname:args.varname}); } }, template: { type: TemplateType, args: { id: { type: new GraphQLNonNull(GraphQLID) } }, resolve(parent, args) { return Template.findById(args.id); } }, templates:{ type: new GraphQLList(TemplateType), resolve(parent, args) { return Template.find({}); } }, author:{ type: AuthorType, args: { NTID: { type: new GraphQLNonNull(GraphQLID) } }, resolve(parent, args) { return Author.findOne({NTID:args.NTID}); } }, authors:{ type: new GraphQLList(AuthorType), resolve(parent, args) { return Author.find({}); } }, latestnotificationevent:{ type: new GraphQLList(NotificationeventType), args: { num: { type: GraphQLInt }, caseEmptyAllow: { type: new GraphQLNonNull(GraphQLBoolean), defaultValue:true } }, resolve(parent, args) { if (!args.num){num=1;} else {num=args.num;} if(args.caseEmptyAllow){ return Notificationevent.find().sort('-createdAt').limit(num); } else{ return Notificationevent.find({ "cases.0": { "$exists": true } }).sort('-createdAt').limit(num); } } } } }); const Mutation = new GraphQLObjectType({ name: 'Mutation', fields: { addAuthor: { type: AuthorType, args: { NTID: { type: new GraphQLNonNull(GraphQLString) }, }, resolve(parent, args) { let author = new Author({ NTID: args.NTID, }); return author.save(); } }, addSysvar:{ type: SysvarType, args:{ varname: { type: new GraphQLNonNull(GraphQLString)}, varval: { type: new GraphQLNonNull(GraphQLString)} }, resolve(parent,args){ sysvar = new Sysvar(args); return sysvar.save(); } }, addTemplate:{ type:TemplateType, args:{ name: { type: new GraphQLNonNull(GraphQLString)}, content: { type: new GraphQLNonNull(GraphQLString)}, authorID: { type: new GraphQLNonNull(GraphQLString)}, category: { type: GraphQLString }, tags: { type: GraphQLString } }, resolve(parent,args){ let template = new Template(args); return template.save(); } }, addTemplates:{ type: new GraphQLList(TemplateType), args:{ templates: {type: new GraphQLNonNull(GraphQLList(GraphQLJSON))}, }, resolve(parent,args){ var saved_templates=[]; args.templates.forEach(function(element){ let template = new Template(element); saved_templates.push(template.save()); }); return saved_templates; } }, addNotificationevent: { type: NotificationeventType, args: { cases: { type: new GraphQLNonNull(GraphQLList(GraphQLJSON))} }, resolve(parent, args) { let notificationevent = new Notificationevent(args); return notificationevent.save(); } }, updateSysvar:{ type: SysvarType, args:{ varname: { type: new GraphQLNonNull(GraphQLString)}, varval: { type: new GraphQLNonNull(GraphQLString)} }, resolve(parent,args){ return Sysvar.findOneAndUpdate({varname:args.varname},args); } }, updateTemplate:{ type:TemplateType, args:{ id: { type: new GraphQLNonNull(GraphQLID) }, name: { type: GraphQLString }, content: { type: GraphQLString }, authorID: { type: GraphQLString }, category: { type: GraphQLString }, tags: { type: GraphQLString } }, resolve(parent,args){ return Template.findByIdAndUpdate(args.id,args); } }, delTemplate:{ type:TemplateType, args:{ id: { type: new GraphQLNonNull(GraphQLID)}, }, resolve(parent,args){ return Template.findByIdAndDelete(args.id); } } } }); module.exports={RootQuery,Mutation};