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.

71 lines
1.6 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. const { createRateLimitTypeDef } = require('graphql-rate-limit-directive')
  10. /* global WIKI */
  11. WIKI.logger.info(`Loading GraphQL Schema...`)
  12. // Init Subscription PubSub
  13. WIKI.GQLEmitter = new PubSub()
  14. // Schemas
  15. let typeDefs = [createRateLimitTypeDef()]
  16. let schemas = fs.readdirSync(path.join(WIKI.SERVERPATH, 'graph/schemas'))
  17. schemas.forEach(schema => {
  18. typeDefs.push(fs.readFileSync(path.join(WIKI.SERVERPATH, `graph/schemas/${schema}`), 'utf8'))
  19. })
  20. // Resolvers
  21. let resolvers = {}
  22. const resolversObj = _.values(autoload(path.join(WIKI.SERVERPATH, 'graph/resolvers')))
  23. resolversObj.forEach(resolver => {
  24. _.merge(resolvers, resolver)
  25. })
  26. // Directives
  27. let schemaDirectives = {
  28. ...autoload(path.join(WIKI.SERVERPATH, 'graph/directives'))
  29. }
  30. // Live Trail Logger (admin)
  31. class LiveTrailLogger extends Transport {
  32. constructor(opts) {
  33. super(opts)
  34. this.name = 'liveTrailLogger'
  35. this.level = 'debug'
  36. }
  37. log (info, callback = () => {}) {
  38. WIKI.GQLEmitter.publish('livetrail', {
  39. loggingLiveTrail: {
  40. timestamp: new Date(),
  41. level: info[LEVEL],
  42. output: info[MESSAGE]
  43. }
  44. })
  45. callback(null, true)
  46. }
  47. }
  48. WIKI.logger.add(new LiveTrailLogger({}))
  49. WIKI.logger.info(`GraphQL Schema: [ OK ]`)
  50. module.exports = {
  51. typeDefs,
  52. resolvers,
  53. schemaDirectives
  54. }