group.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* global WIKI */
  2. const gql = require('graphql')
  3. module.exports = {
  4. Query: {
  5. async groups() { return {} }
  6. },
  7. Mutation: {
  8. async groups() { return {} }
  9. },
  10. GroupQuery: {
  11. list(obj, args, context, info) {
  12. return WIKI.db.Group.findAll({ where: args })
  13. }
  14. },
  15. GroupMutation: {
  16. assignUser(obj, args) {
  17. return WIKI.db.Group.findById(args.groupId).then(grp => {
  18. if (!grp) {
  19. throw new gql.GraphQLError('Invalid Group ID')
  20. }
  21. return WIKI.db.User.findById(args.userId).then(usr => {
  22. if (!usr) {
  23. throw new gql.GraphQLError('Invalid User ID')
  24. }
  25. return grp.addUser(usr)
  26. })
  27. })
  28. },
  29. create(obj, args) {
  30. return WIKI.db.Group.create(args)
  31. },
  32. delete(obj, args) {
  33. return WIKI.db.Group.destroy({
  34. where: {
  35. id: args.id
  36. },
  37. limit: 1
  38. })
  39. },
  40. unassignUser(obj, args) {
  41. return WIKI.db.Group.findById(args.groupId).then(grp => {
  42. if (!grp) {
  43. throw new gql.GraphQLError('Invalid Group ID')
  44. }
  45. return WIKI.db.User.findById(args.userId).then(usr => {
  46. if (!usr) {
  47. throw new gql.GraphQLError('Invalid User ID')
  48. }
  49. return grp.removeUser(usr)
  50. })
  51. })
  52. },
  53. update(obj, args) {
  54. return WIKI.db.Group.update({
  55. name: args.name
  56. }, {
  57. where: { id: args.id }
  58. })
  59. }
  60. },
  61. Group: {
  62. users(grp) {
  63. return grp.getUsers()
  64. }
  65. }
  66. }