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.

68 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 { LEVEL, MESSAGE } = require('triple-beam')
  8. const Transport = require('winston-transport')
  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. class LiveTrailLogger extends Transport {
  29. constructor(opts) {
  30. super(opts)
  31. this.name = 'liveTrailLogger'
  32. this.level = 'debug'
  33. }
  34. log (info, callback = () => {}) {
  35. WIKI.GQLEmitter.publish('livetrail', {
  36. loggingLiveTrail: {
  37. timestamp: new Date(),
  38. level: info[LEVEL],
  39. output: info[MESSAGE]
  40. }
  41. })
  42. callback(null, true)
  43. }
  44. }
  45. WIKI.logger.add(new LiveTrailLogger({}))
  46. WIKI.logger.info(`GraphQL Schema: [ OK ]`)
  47. module.exports = {
  48. typeDefs,
  49. resolvers,
  50. schemaDirectives
  51. }