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.

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