resolvers.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const { withFilter } = require('graphql-subscriptions')
  2. const globby = require('globby')
  3. const merge = require('lodash.merge')
  4. const { GraphQLJSON } = require('graphql-type-json')
  5. // Channels for subscriptions
  6. const channels = require('./channels')
  7. // Connectors
  8. const cwd = require('./connectors/cwd')
  9. const progress = require('./connectors/progress')
  10. const files = require('./connectors/files')
  11. const clientAddons = require('./connectors/client-addons')
  12. const sharedData = require('./connectors/shared-data')
  13. const locales = require('./connectors/locales')
  14. // Utils
  15. const stats = require('./util/stats')
  16. // Start ipc server
  17. require('./util/ipc')
  18. process.env.VUE_CLI_API_MODE = true
  19. const resolvers = [{
  20. JSON: GraphQLJSON,
  21. DescribedEntity: {
  22. __resolveType (obj, context, info) {
  23. return null
  24. }
  25. },
  26. ClientAddon: {
  27. url: (addon, args, context) => clientAddons.getUrl(addon, context)
  28. },
  29. Query: {
  30. cwd: () => cwd.get(),
  31. progress: (root, { id }, context) => progress.get(id, context),
  32. clientAddons: (root, args, context) => clientAddons.list(context),
  33. sharedData: (root, args, context) => sharedData.get(args, context),
  34. locales: (root, args, context) => locales.list(context)
  35. },
  36. Mutation: {
  37. fileOpenInEditor: (root, { input }, context) => files.openInEditor(input, context),
  38. sharedDataUpdate: (root, args, context) => sharedData.set(args, context)
  39. },
  40. Subscription: {
  41. cwdChanged: {
  42. subscribe: (parent, args, { pubsub }) => pubsub.asyncIterator(channels.CWD_CHANGED)
  43. },
  44. progressChanged: {
  45. subscribe: withFilter(
  46. // Iterator
  47. (parent, args, { pubsub }) => pubsub.asyncIterator(channels.PROGRESS_CHANGED),
  48. // Filter
  49. (payload, vars) => payload.progressChanged.id === vars.id
  50. )
  51. },
  52. progressRemoved: {
  53. subscribe: withFilter(
  54. // Iterator
  55. (parent, args, { pubsub }) => pubsub.asyncIterator(channels.PROGRESS_REMOVED),
  56. // Filter
  57. (payload, vars) => payload.progressRemoved === vars.id
  58. )
  59. },
  60. clientAddonAdded: {
  61. subscribe: (parent, args, { pubsub }) => pubsub.asyncIterator(channels.CLIENT_ADDON_ADDED)
  62. },
  63. sharedDataUpdated: {
  64. subscribe: withFilter(
  65. (parent, args, { pubsub }) => pubsub.asyncIterator(channels.SHARED_DATA_UPDATED),
  66. (payload, vars) => {
  67. const result = payload.sharedDataUpdated.id === vars.id && payload.sharedDataUpdated.projectId === vars.projectId
  68. if (result) {
  69. stats.get(`shared-data_${vars.projectId}`, vars.id).value++
  70. }
  71. return result
  72. }
  73. )
  74. },
  75. localeAdded: {
  76. subscribe: (parent, args, { pubsub }) => pubsub.asyncIterator(channels.LOCALE_ADDED)
  77. },
  78. routeRequested: {
  79. subscribe: (parent, args, { pubsub }) => pubsub.asyncIterator(channels.ROUTE_REQUESTED)
  80. }
  81. }
  82. }]
  83. // Load resolvers in './schema'
  84. const paths = globby.sync(['./schema/*.js'], { cwd: __dirname, absolute: true })
  85. paths.forEach(file => {
  86. const { resolvers: r } = require(file)
  87. r && resolvers.push(r)
  88. })
  89. module.exports = merge.apply(null, resolvers)