resolvers-tag.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict'
  2. /* global wiki */
  3. const gql = require('graphql')
  4. module.exports = {
  5. Query: {
  6. tags(obj, args, context, info) {
  7. return wiki.db.Tag.findAll({ where: args })
  8. }
  9. },
  10. Mutation: {
  11. assignTagToDocument(obj, args) {
  12. return wiki.db.Tag.findById(args.tagId).then(tag => {
  13. if (!tag) {
  14. throw new gql.GraphQLError('Invalid Tag ID')
  15. }
  16. return wiki.db.Document.findById(args.documentId).then(doc => {
  17. if (!doc) {
  18. throw new gql.GraphQLError('Invalid Document ID')
  19. }
  20. return tag.addDocument(doc)
  21. })
  22. })
  23. },
  24. createTag(obj, args) {
  25. return wiki.db.Tag.create(args)
  26. },
  27. deleteTag(obj, args) {
  28. return wiki.db.Tag.destroy({
  29. where: {
  30. id: args.id
  31. },
  32. limit: 1
  33. })
  34. },
  35. removeTagFromDocument(obj, args) {
  36. return wiki.db.Tag.findById(args.tagId).then(tag => {
  37. if (!tag) {
  38. throw new gql.GraphQLError('Invalid Tag ID')
  39. }
  40. return wiki.db.Document.findById(args.documentId).then(doc => {
  41. if (!doc) {
  42. throw new gql.GraphQLError('Invalid Document ID')
  43. }
  44. return tag.removeDocument(doc)
  45. })
  46. })
  47. }
  48. },
  49. Tag: {
  50. documents(tag) {
  51. return tag.getDocuments()
  52. }
  53. }
  54. }