wikijs-fork/server/graph/resolvers/group.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

const graphHelper = require('../../helpers/graph')
2017-08-07 01:05:10 +00:00
/* global WIKI */
2017-08-07 01:05:10 +00:00
const gql = require('graphql')
2017-08-07 01:05:10 +00:00
module.exports = {
Query: {
async groups() { return {} }
},
Mutation: {
async groups() { return {} }
},
GroupQuery: {
async list(obj, args, context, info) {
return WIKI.models.groups.query().select(
'groups.*',
WIKI.models.groups.relatedQuery('users').count().as('userCount')
)
2018-03-28 04:02:32 +00:00
},
async single(obj, args, context, info) {
return WIKI.models.groups.query().findById(args.id)
}
},
GroupMutation: {
async assignUser(obj, args) {
const grp = await WIKI.models.groups.query().findById(args.groupId)
if (!grp) {
throw new gql.GraphQLError('Invalid Group ID')
}
const usr = await WIKI.models.users.query().findById(args.userId)
if (!usr) {
throw new gql.GraphQLError('Invalid User ID')
}
await grp.$relatedQuery('users').relate(usr.id)
return {
responseResult: graphHelper.generateSuccess('User has been assigned to group.')
}
},
async create(obj, args) {
const group = await WIKI.models.groups.query().insertAndFetch({
name: args.name
})
return {
responseResult: graphHelper.generateSuccess('Group created successfully.'),
group
}
},
2018-03-26 05:11:49 +00:00
async delete(obj, args) {
await WIKI.models.groups.query().deleteById(args.id)
2018-03-26 05:11:49 +00:00
return {
responseResult: graphHelper.generateSuccess('Group has been deleted.')
}
},
async unassignUser(obj, args) {
const grp = await WIKI.models.groups.query().findById(args.groupId)
if (!grp) {
throw new gql.GraphQLError('Invalid Group ID')
}
const usr = await WIKI.models.users.query().findById(args.userId)
if (!usr) {
throw new gql.GraphQLError('Invalid User ID')
}
await grp.$relatedQuery('users').unrelate().where('userId', usr.id)
return {
responseResult: graphHelper.generateSuccess('User has been unassigned from group.')
}
},
async update(obj, args) {
await WIKI.models.groups.query().patch({ name: args.name }).where('id', args.id)
return {
responseResult: graphHelper.generateSuccess('Group has been updated.')
}
}
2017-08-07 01:05:10 +00:00
},
Group: {
2017-08-07 01:05:10 +00:00
users(grp) {
return grp.$relatedQuery('users')
2017-08-07 01:05:10 +00:00
}
}
}