From 574e4b97f40d087c5267a7ad976329aec357cc2e Mon Sep 17 00:00:00 2001 From: NGPixel Date: Sat, 19 Aug 2017 22:51:25 -0400 Subject: [PATCH] feat: GraphQL setting, right, folder resolvers --- server/models/_relations.js | 2 ++ server/models/document.js | 5 +++ server/modules/graphql.js | 4 +++ server/schemas/resolvers-comment.js | 43 ++++++++++++++++++++++ server/schemas/resolvers-document.js | 18 ++++++++++ server/schemas/resolvers-folder.js | 7 ++++ server/schemas/resolvers-group.js | 7 ++++ server/schemas/resolvers-right.js | 54 ++++++++++++++++++++++++++++ server/schemas/resolvers-setting.js | 25 +++++++++++++ server/schemas/resolvers-tag.js | 7 ++++ server/schemas/resolvers-user.js | 17 +++++++++ server/schemas/types.graphql | 44 ++++++++++++++++++++++- 12 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 server/schemas/resolvers-comment.js create mode 100644 server/schemas/resolvers-right.js create mode 100644 server/schemas/resolvers-setting.js diff --git a/server/models/_relations.js b/server/models/_relations.js index 61b84250..e14fe007 100644 --- a/server/models/_relations.js +++ b/server/models/_relations.js @@ -7,7 +7,9 @@ module.exports = db => { db.User.belongsToMany(db.Group, { through: 'userGroups' }) db.Group.belongsToMany(db.User, { through: 'userGroups' }) db.Group.hasMany(db.Right) + db.Right.belongsTo(db.Group) db.Document.belongsToMany(db.Tag, { through: 'documentTags' }) + db.Document.hasMany(db.Comment) db.Tag.belongsToMany(db.Document, { through: 'documentTags' }) db.File.belongsTo(db.Folder) db.Folder.hasMany(db.File) diff --git a/server/models/document.js b/server/models/document.js index 76c9e0c7..2e0d9717 100644 --- a/server/models/document.js +++ b/server/models/document.js @@ -41,6 +41,11 @@ module.exports = (sequelize, DataTypes) => { allowNull: false, defaultValue: false }, + isDraft: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: false + }, searchContent: { type: DataTypes.TEXT, allowNull: true, diff --git a/server/modules/graphql.js b/server/modules/graphql.js index 58304fd4..2224d64b 100644 --- a/server/modules/graphql.js +++ b/server/modules/graphql.js @@ -10,16 +10,20 @@ const _ = require('lodash') const typeDefs = fs.readFileSync(path.join(wiki.SERVERPATH, 'schemas/types.graphql'), 'utf8') const DateScalar = require('../schemas/scalar-date') +const CommentResolvers = require('../schemas/resolvers-comment') const DocumentResolvers = require('../schemas/resolvers-document') const FolderResolvers = require('../schemas/resolvers-folder') const GroupResolvers = require('../schemas/resolvers-group') +const SettingResolvers = require('../schemas/resolvers-setting') const TagResolvers = require('../schemas/resolvers-tag') const UserResolvers = require('../schemas/resolvers-user') const resolvers = _.merge( + CommentResolvers, DocumentResolvers, FolderResolvers, GroupResolvers, + SettingResolvers, TagResolvers, UserResolvers, DateScalar diff --git a/server/schemas/resolvers-comment.js b/server/schemas/resolvers-comment.js new file mode 100644 index 00000000..f8d00ee2 --- /dev/null +++ b/server/schemas/resolvers-comment.js @@ -0,0 +1,43 @@ +'use strict' + +/* global wiki */ + +module.exports = { + Query: { + comments(obj, args, context, info) { + return wiki.db.Comment.findAll({ where: args }) + } + }, + Mutation: { + createComment(obj, args) { + return wiki.db.Comment.create({ + content: args.content, + author: args.userId, + document: args.documentId + }) + }, + deleteComment(obj, args) { + return wiki.db.Comment.destroy({ + where: { + id: args.id + }, + limit: 1 + }) + }, + modifyComment(obj, args) { + return wiki.db.Comment.update({ + content: args.content + }, { + where: { id: args.id } + }) + } + }, + Comment: { + author(cm) { + return cm.getAuthor() + }, + document(cm) { + return cm.getDocument() + } + } +} diff --git a/server/schemas/resolvers-document.js b/server/schemas/resolvers-document.js index 59963ace..666f8fef 100644 --- a/server/schemas/resolvers-document.js +++ b/server/schemas/resolvers-document.js @@ -19,9 +19,27 @@ module.exports = { }, limit: 1 }) + }, + modifyDocument(obj, args) { + return wiki.db.Document.update({ + title: args.title, + subtitle: args.subtitle + }, { + where: { id: args.id } + }) + }, + moveDocument(obj, args) { + return wiki.db.Document.update({ + path: args.path + }, { + where: { id: args.id } + }) } }, Document: { + comments(doc) { + return doc.getComments() + }, tags(doc) { return doc.getTags() } diff --git a/server/schemas/resolvers-folder.js b/server/schemas/resolvers-folder.js index de7add8e..4acf2806 100644 --- a/server/schemas/resolvers-folder.js +++ b/server/schemas/resolvers-folder.js @@ -19,6 +19,13 @@ module.exports = { }, limit: 1 }) + }, + renameFolder(obj, args) { + return wiki.db.Folder.update({ + name: args.name + }, { + where: { id: args.id } + }) } }, Folder: { diff --git a/server/schemas/resolvers-group.js b/server/schemas/resolvers-group.js index 189c0fd3..61afc2d0 100644 --- a/server/schemas/resolvers-group.js +++ b/server/schemas/resolvers-group.js @@ -47,6 +47,13 @@ module.exports = { return grp.removeUser(usr) }) }) + }, + renameGroup(obj, args) { + return wiki.db.Group.update({ + name: args.name + }, { + where: { id: args.id } + }) } }, Group: { diff --git a/server/schemas/resolvers-right.js b/server/schemas/resolvers-right.js new file mode 100644 index 00000000..72e42b76 --- /dev/null +++ b/server/schemas/resolvers-right.js @@ -0,0 +1,54 @@ +'use strict' + +/* global wiki */ + +const gql = require('graphql') + +module.exports = { + Query: { + rights(obj, args, context, info) { + return wiki.db.Right.findAll({ where: args }) + } + }, + Mutation: { + addRightToGroup(obj, args) { + return wiki.db.Group.findById(args.groupId).then(grp => { + if (!grp) { + throw new gql.GraphQLError('Invalid Group ID') + } + return wiki.db.Right.create({ + path: args.path, + role: args.role, + exact: args.exact, + allow: args.allow, + group: grp + }) + }) + }, + removeRightFromGroup(obj, args) { + return wiki.db.Right.destroy({ + where: { + id: args.rightId + }, + limit: 1 + }) + }, + modifyRight(obj, args) { + return wiki.db.Right.update({ + path: args.path, + role: args.role, + exact: args.exact, + allow: args.allow + }, { + where: { + id: args.id + } + }) + } + }, + Right: { + group(rt) { + return rt.getGroup() + } + } +} diff --git a/server/schemas/resolvers-setting.js b/server/schemas/resolvers-setting.js new file mode 100644 index 00000000..1ece4a0e --- /dev/null +++ b/server/schemas/resolvers-setting.js @@ -0,0 +1,25 @@ +'use strict' + +/* global wiki */ + +const _ = require('lodash') + +module.exports = { + Query: { + settings(obj, args, context, info) { + return wiki.db.Setting.findAll({ where: args, raw: true }).then(entries => { + return _.map(entries, entry => { + entry.config = JSON.stringify(entry.config) + return entry + }) + }) + } + }, + Mutation: { + setConfigEntry(obj, args) { + return wiki.db.Setting.update({ + value: args.value + }, { where: { key: args.key } }) + } + } +} diff --git a/server/schemas/resolvers-tag.js b/server/schemas/resolvers-tag.js index ecf05fda..ad11f674 100644 --- a/server/schemas/resolvers-tag.js +++ b/server/schemas/resolvers-tag.js @@ -47,6 +47,13 @@ module.exports = { return tag.removeDocument(doc) }) }) + }, + renameTag(obj, args) { + return wiki.db.Group.update({ + key: args.key + }, { + where: { id: args.id } + }) } }, Tag: { diff --git a/server/schemas/resolvers-user.js b/server/schemas/resolvers-user.js index 140ba83b..694306e2 100644 --- a/server/schemas/resolvers-user.js +++ b/server/schemas/resolvers-user.js @@ -19,6 +19,23 @@ module.exports = { }, limit: 1 }) + }, + modifyUser(obj, args) { + return wiki.db.User.update({ + email: args.email, + name: args.name, + provider: args.provider, + providerId: args.providerId, + role: args.role + }, { + where: { id: args.id } + }) + }, + resetUserPassword(obj, args) { + return false + }, + setUserPassword(obj, args) { + return false } }, User: { diff --git a/server/schemas/types.graphql b/server/schemas/types.graphql index 9518dc5a..18bcf1d9 100644 --- a/server/schemas/types.graphql +++ b/server/schemas/types.graphql @@ -52,6 +52,7 @@ type Document implements Base { isDirectory: Boolean! isEntry: Boolean! searchContent: String + comments: [Comment] tags: [Tag] } @@ -93,6 +94,7 @@ type Right implements Base { role: RightRole! exact: Boolean! allow: Boolean! + group: Group! } type Setting implements Base { @@ -145,6 +147,14 @@ type Query { # Mutations (Create, Update, Delete) type Mutation { + addRightToGroup( + groupId: Int! + path: String! + role: RightRole! + exact: Boolean! + allow: Boolean! + ): Right + assignTagToDocument( tagId: Int! documentId: Int! @@ -155,6 +165,12 @@ type Mutation { groupId: Int! ): OperationResult + createComment( + userId: Int! + documentId: Int! + content: String! + ): Comment + createDocument( path: String! title: String! @@ -182,6 +198,10 @@ type Mutation { role: UserRole! ): User + deleteComment( + id: Int! + ): OperationResult + deleteDocument( id: Int! ): OperationResult @@ -202,6 +222,11 @@ type Mutation { id: Int! ): OperationResult + modifyComment( + id: Int! + content: String! + ): Document + modifyDocument( id: Int! title: String @@ -217,6 +242,14 @@ type Mutation { role: UserRole ): User + modifyRight( + id: Int! + path: String + role: RightRole + exact: Boolean + allow: Boolean + ): Right + moveDocument( id: Int! path: String! @@ -234,7 +267,7 @@ type Mutation { renameTag( id: Int! - name: String! + key: String! ): OperationResult removeTagFromDocument( @@ -242,6 +275,10 @@ type Mutation { documentId: Int! ): OperationResult + removeRightFromGroup( + rightId: Int! + ): OperationResult + removeUserFromGroup( userId: Int! groupId: Int! @@ -251,6 +288,11 @@ type Mutation { id: Int! ): OperationResult + setConfigEntry( + key: String! + value: String! + ): OperationResult + setUserPassword( id: Int! passwordRaw: String!