You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.5 KiB

  1. const _ = require('lodash')
  2. const fs = require('fs')
  3. // const gqlTools = require('graphql-tools')
  4. const path = require('path')
  5. const autoload = require('auto-load')
  6. const PubSub = require('graphql-subscriptions').PubSub
  7. const util = require('util')
  8. const winston = require('winston')
  9. /* global WIKI */
  10. WIKI.logger.info(`Loading GraphQL Schema...`)
  11. // Init Subscription PubSub
  12. WIKI.GQLEmitter = new PubSub()
  13. // Schemas
  14. let typeDefs = []
  15. let schemas = fs.readdirSync(path.join(WIKI.SERVERPATH, 'graph/schemas'))
  16. schemas.forEach(schema => {
  17. typeDefs.push(fs.readFileSync(path.join(WIKI.SERVERPATH, `graph/schemas/${schema}`), 'utf8'))
  18. })
  19. // Resolvers
  20. let resolvers = {}
  21. const resolversObj = _.values(autoload(path.join(WIKI.SERVERPATH, 'graph/resolvers')))
  22. resolversObj.forEach(resolver => {
  23. _.merge(resolvers, resolver)
  24. })
  25. // Directives
  26. let schemaDirectives = autoload(path.join(WIKI.SERVERPATH, 'graph/directives'))
  27. // Live Trail Logger (admin)
  28. let LiveTrailLogger = winston.transports.LiveTrailLogger = function (options) {
  29. this.name = 'livetrailLogger'
  30. this.level = 'debug'
  31. }
  32. util.inherits(LiveTrailLogger, winston.Transport)
  33. LiveTrailLogger.prototype.log = function (level, msg, meta, callback) {
  34. WIKI.GQLEmitter.publish('livetrail', {
  35. loggingLiveTrail: {
  36. timestamp: new Date(),
  37. level,
  38. output: msg
  39. }
  40. })
  41. callback(null, true)
  42. }
  43. WIKI.logger.add(new LiveTrailLogger({}))
  44. WIKI.logger.info(`GraphQL Schema: [ OK ]`)
  45. module.exports = {
  46. typeDefs,
  47. resolvers,
  48. schemaDirectives
  49. }