wikijs-fork/server/graph/index.js

75 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

const _ = require('lodash')
const fs = require('fs')
// const gqlTools = require('graphql-tools')
const path = require('path')
const autoload = require('auto-load')
const PubSub = require('graphql-subscriptions').PubSub
const { LEVEL, MESSAGE } = require('triple-beam')
const Transport = require('winston-transport')
2019-02-17 06:32:35 +00:00
const { createRateLimitTypeDef } = require('graphql-rate-limit-directive')
// const { GraphQLUpload } = require('graphql-upload')
/* global WIKI */
WIKI.logger.info(`Loading GraphQL Schema...`)
// Init Subscription PubSub
WIKI.GQLEmitter = new PubSub()
// Schemas
2019-02-15 21:36:13 +00:00
let typeDefs = [createRateLimitTypeDef()]
let schemas = fs.readdirSync(path.join(WIKI.SERVERPATH, 'graph/schemas'))
schemas.forEach(schema => {
typeDefs.push(fs.readFileSync(path.join(WIKI.SERVERPATH, `graph/schemas/${schema}`), 'utf8'))
})
// Resolvers
let resolvers = {
// Upload: GraphQLUpload
}
const resolversObj = _.values(autoload(path.join(WIKI.SERVERPATH, 'graph/resolvers')))
resolversObj.forEach(resolver => {
_.merge(resolvers, resolver)
})
// Directives
2019-02-15 21:36:13 +00:00
let schemaDirectives = {
2019-02-17 06:32:35 +00:00
...autoload(path.join(WIKI.SERVERPATH, 'graph/directives'))
2019-02-15 21:36:13 +00:00
}
// Live Trail Logger (admin)
class LiveTrailLogger extends Transport {
constructor(opts) {
super(opts)
this.name = 'liveTrailLogger'
this.level = 'debug'
}
log (info, callback = () => {}) {
WIKI.GQLEmitter.publish('livetrail', {
loggingLiveTrail: {
timestamp: new Date(),
level: info[LEVEL],
output: info[MESSAGE]
}
})
callback(null, true)
}
}
WIKI.logger.add(new LiveTrailLogger({}))
WIKI.logger.info(`GraphQL Schema: [ OK ]`)
module.exports = {
typeDefs,
resolvers,
schemaDirectives
}