From 35cfa497b495a9e42c00dc8ead36c94b0bd2bbce Mon Sep 17 00:00:00 2001 From: bobo1133 Date: Mon, 30 Sep 2019 14:14:52 +0800 Subject: [PATCH] feat: support typescript --- lib/load_connector.js | 5 ++--- lib/load_schema.js | 21 +++++++-------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/lib/load_connector.js b/lib/load_connector.js index da84ec6..57368ef 100644 --- a/lib/load_connector.js +++ b/lib/load_connector.js @@ -16,11 +16,10 @@ module.exports = app => { const classes = new Map(); types.forEach(type => { - - const connectorFile = path.join(basePath, type, 'connector.js'); + const connectorFile = app.loader.resolveModule(path.join(basePath, type, 'connector')); /* istanbul ignore else */ if (fs.existsSync(connectorFile)) { - const Connector = require(connectorFile); + const Connector = app.loader.loadFile(connectorFile); classes.set(path.basename(type), Connector); } }); diff --git a/lib/load_schema.js b/lib/load_schema.js index f69fad7..ad92f07 100644 --- a/lib/load_schema.js +++ b/lib/load_schema.js @@ -16,7 +16,6 @@ module.exports = app => { const schemas = []; const resolverMap = {}; - const resolverFactories = []; const directiveMap = {}; const schemaDirectivesProps = {}; const { defaultEmptySchema = false } = app.config.graphql; @@ -39,27 +38,23 @@ module.exports = app => { } // Load resolver - const resolverFile = path.join(basePath, type, 'resolver.js'); + const resolverFile = app.loader.resolveModule(path.join(basePath, type, 'resolver')); if (fs.existsSync(resolverFile)) { - const resolver = require(resolverFile); - if (_.isFunction(resolver)) { - resolverFactories.push(resolver); - } else if (_.isObject(resolver)) { - _.merge(resolverMap, resolver); - } + const resolver = app.loader.loadFile(resolverFile); + _.merge(resolverMap, resolver); } // Load directive resolver - const directiveFile = path.join(basePath, type, 'directive.js'); + const directiveFile = app.loader.resolveModule(path.join(basePath, type, 'directive')); if (fs.existsSync(directiveFile)) { - const directive = require(directiveFile); + const directive = app.loader.loadFile(directiveFile); _.merge(directiveMap, directive); } // Load schemaDirectives - let schemaDirectivesFile = path.join(basePath, type, 'schemaDirective.js'); + let schemaDirectivesFile = app.loader.resolveModule(path.join(basePath, type, 'schemaDirective')); if (fs.existsSync(schemaDirectivesFile)) { - schemaDirectivesFile = require(schemaDirectivesFile); + schemaDirectivesFile = app.loader.loadFile(schemaDirectivesFile); _.merge(schemaDirectivesProps, schemaDirectivesFile); } }); @@ -67,8 +62,6 @@ module.exports = app => { Object.defineProperty(app, 'schema', { get() { if (!this[SYMBOL_SCHEMA]) { - resolverFactories.forEach(resolverFactory => _.merge(resolverMap, resolverFactory(app))); - this[SYMBOL_SCHEMA] = makeExecutableSchema({ typeDefs: schemas, resolvers: resolverMap,