feat: GraphQL mutations for folder, group, tag, user

pull/621/head
NGPixel 7 years ago
parent 1405b822f4
commit 840eb7e705

@ -6,9 +6,11 @@
module.exports = db => { module.exports = db => {
db.User.belongsToMany(db.Group, { through: 'userGroups' }) db.User.belongsToMany(db.Group, { through: 'userGroups' })
db.Group.belongsToMany(db.User, { through: 'userGroups' }) db.Group.belongsToMany(db.User, { through: 'userGroups' })
db.Group.hasMany(db.Right, { as: 'groupRights' }) db.Group.hasMany(db.Right)
db.Document.hasMany(db.Tag, { as: 'documentTags' }) db.Document.belongsToMany(db.Tag, { through: 'documentTags' })
db.Tag.belongsToMany(db.Document, { through: 'documentTags' })
db.File.belongsTo(db.Folder) db.File.belongsTo(db.Folder)
db.Folder.hasMany(db.File)
db.Comment.belongsTo(db.Document) db.Comment.belongsTo(db.Document)
db.Comment.belongsTo(db.User, { as: 'author' }) db.Comment.belongsTo(db.User, { as: 'author' })
} }

@ -10,11 +10,15 @@ const _ = require('lodash')
const typeDefs = fs.readFileSync(path.join(wiki.SERVERPATH, 'schemas/types.graphql'), 'utf8') const typeDefs = fs.readFileSync(path.join(wiki.SERVERPATH, 'schemas/types.graphql'), 'utf8')
const DateScalar = require('../schemas/scalar-date') const DateScalar = require('../schemas/scalar-date')
const FolderResolvers = require('../schemas/resolvers-folder')
const GroupResolvers = require('../schemas/resolvers-group') const GroupResolvers = require('../schemas/resolvers-group')
const TagResolvers = require('../schemas/resolvers-tag')
const UserResolvers = require('../schemas/resolvers-user') const UserResolvers = require('../schemas/resolvers-user')
const resolvers = _.merge( const resolvers = _.merge(
FolderResolvers,
GroupResolvers, GroupResolvers,
TagResolvers,
UserResolvers, UserResolvers,
DateScalar DateScalar
) )

@ -9,7 +9,7 @@ const moment = require('moment')
const path = require('path') const path = require('path')
const entryHelper = require('../helpers/entry') const entryHelper = require('../helpers/entry')
module.exports = (job, done) => { module.exports = (job) => {
return wiki.git.resync().then(() => { return wiki.git.resync().then(() => {
// -> Stream all documents // -> Stream all documents

@ -7,7 +7,7 @@ const fs = Promise.promisifyAll(require('fs-extra'))
const moment = require('moment') const moment = require('moment')
const path = require('path') const path = require('path')
module.exports = (job, done) => { module.exports = (job) => {
return fs.readdirAsync(wiki.UPLTEMPPATH).then((ls) => { return fs.readdirAsync(wiki.UPLTEMPPATH).then((ls) => {
let fifteenAgo = moment().subtract(15, 'minutes') let fifteenAgo = moment().subtract(15, 'minutes')

@ -0,0 +1,29 @@
'use strict'
/* global wiki */
module.exports = {
Query: {
folders(obj, args, context, info) {
return wiki.db.Folder.findAll({ where: args })
}
},
Mutation: {
createFolder(obj, args) {
return wiki.db.Folder.create(args)
},
deleteGroup(obj, args) {
return wiki.db.Folder.destroy({
where: {
id: args.id
},
limit: 1
})
}
},
Folder: {
files(grp) {
return grp.getFiles()
}
}
}

@ -2,6 +2,8 @@
/* global wiki */ /* global wiki */
const gql = require('graphql')
module.exports = { module.exports = {
Query: { Query: {
groups(obj, args, context, info) { groups(obj, args, context, info) {
@ -9,8 +11,42 @@ module.exports = {
} }
}, },
Mutation: { Mutation: {
assignUserToGroup(obj, args) {
return wiki.db.Group.findById(args.groupId).then(grp => {
if (!grp) {
throw new gql.GraphQLError('Invalid Group ID')
}
return wiki.db.User.findById(args.userId).then(usr => {
if (!usr) {
throw new gql.GraphQLError('Invalid User ID')
}
return grp.addUser(usr)
})
})
},
createGroup(obj, args) { createGroup(obj, args) {
return wiki.db.Group.create(args) return wiki.db.Group.create(args)
},
deleteGroup(obj, args) {
return wiki.db.Group.destroy({
where: {
id: args.id
},
limit: 1
})
},
removeUserFromGroup(obj, args) {
return wiki.db.Group.findById(args.groupId).then(grp => {
if (!grp) {
throw new gql.GraphQLError('Invalid Group ID')
}
return wiki.db.User.findById(args.userId).then(usr => {
if (!usr) {
throw new gql.GraphQLError('Invalid User ID')
}
return grp.removeUser(usr)
})
})
} }
}, },
Group: { Group: {

@ -0,0 +1,57 @@
'use strict'
/* global wiki */
const gql = require('graphql')
module.exports = {
Query: {
tags(obj, args, context, info) {
return wiki.db.Tag.findAll({ where: args })
}
},
Mutation: {
assignTagToDocument(obj, args) {
return wiki.db.Tag.findById(args.tagId).then(tag => {
if (!tag) {
throw new gql.GraphQLError('Invalid Tag ID')
}
return wiki.db.Document.findById(args.documentId).then(doc => {
if (!doc) {
throw new gql.GraphQLError('Invalid Document ID')
}
return tag.addDocument(doc)
})
})
},
createTag(obj, args) {
return wiki.db.Tag.create(args)
},
deleteTag(obj, args) {
return wiki.db.Tag.destroy({
where: {
id: args.id
},
limit: 1
})
},
removeTagFromDocument(obj, args) {
return wiki.db.Tag.findById(args.tagId).then(tag => {
if (!tag) {
throw new gql.GraphQLError('Invalid Tag ID')
}
return wiki.db.Document.findById(args.documentId).then(doc => {
if (!doc) {
throw new gql.GraphQLError('Invalid Document ID')
}
return tag.removeDocument(doc)
})
})
}
},
Tag: {
documents(tag) {
return tag.getDocuments()
}
}
}

@ -11,6 +11,14 @@ module.exports = {
Mutation: { Mutation: {
createUser(obj, args) { createUser(obj, args) {
return wiki.db.User.create(args) return wiki.db.User.create(args)
},
deleteUser(obj, args) {
return wiki.db.User.destroy({
where: {
id: args.id
},
limit: 1
})
} }
}, },
User: { User: {

@ -73,6 +73,7 @@ type Folder implements Base {
createdAt: Date createdAt: Date
updatedAt: Date updatedAt: Date
name: String! name: String!
files: [File]
} }
type Group implements Base { type Group implements Base {
@ -107,7 +108,8 @@ type Tag implements Base {
id: Int! id: Int!
createdAt: Date createdAt: Date
updatedAt: Date updatedAt: Date
key: String! key: String!,
documents: [Document]
} }
# A User # A User
@ -138,13 +140,23 @@ type Query {
# Mutations (Create, Update, Delete) # Mutations (Create, Update, Delete)
type Mutation { type Mutation {
assignTagToDocument(
tagId: Int!
documentId: Int!
): Boolean
assignUserToGroup( assignUserToGroup(
userId: Int! userId: Int!
groupId: Int! groupId: Int!
): Boolean ): Boolean
createFolder(
name: String!
): Folder
createGroup( createGroup(
name: String! name: String!
): Group ): Group
createTag(
name: String!
): Tag
createUser( createUser(
email: String! email: String!
name: String name: String
@ -152,12 +164,22 @@ type Mutation {
providerId: String providerId: String
role: UserRole! role: UserRole!
): User ): User
deleteFolder(
id: Int!
): Boolean
deleteGroup( deleteGroup(
id: Int! id: Int!
): Boolean ): Boolean
deleteTag(
id: Int!
): Boolean
deleteUser( deleteUser(
id: Int! id: Int!
): Boolean ): Boolean
removeTagFromDocument(
tagId: Int!
documentId: Int!
): Boolean
removeUserFromGroup( removeUserFromGroup(
userId: Int! userId: Int!
groupId: Int! groupId: Int!

Loading…
Cancel
Save