UNPKG

jsev

Version:

Environment for building Web API's.

105 lines 3.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@graphql-modules/core"); const apollo_server_koa_1 = require("apollo-server-koa"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const util_1 = require("util"); const errors_1 = require("../errors"); const readdir = util_1.promisify(fs_1.default.readdir); const lstat = util_1.promisify(fs_1.default.lstat); const exists = util_1.promisify(fs_1.default.exists); async function loadSchema(modulePath) { const info = await lstat(modulePath); if (!info.isDirectory()) { return null; } const gqlPath = path_1.default.join(modulePath, "graphql"); if (!(await exists(gqlPath))) { return null; } const schema = await Promise.resolve().then(() => __importStar(require(gqlPath))); return schema; } async function loadModule(modules, schemas, moduleName) { let mod = modules[moduleName]; if (mod) { return mod; } const { imports, resolvers, typeDefs } = schemas[moduleName]; let importedModules; if (imports) { // Load dependant modules 1 at a time so that the same dependency is linked to a single instance importedModules = await imports.reduce(async (a, x) => { const list = await a; const depMod = await loadModule(modules, schemas, x); if (!depMod) { throw new errors_1.InvalidOperationError(`Module ${moduleName} depends on module ${x} which was not found, please check your graphql dependencies.`); } list.push(depMod); return list; }, Promise.resolve([])); } mod = new core_1.GraphQLModule({ context: (session) => session.ctx, imports: importedModules, resolvers, typeDefs, }); modules[moduleName] = mod; return mod; } async function loadModules(rootPath) { const modulesPath = path_1.default.join(rootPath, "modules"); const items = await readdir(modulesPath); const schemas = await items.reduce(async (a, x) => { const list = await a; const sch = await loadSchema(path_1.default.join(modulesPath, x)); if (sch) { list[x] = sch; } return list; }, Promise.resolve({})); // Load modules 1 at a time as it may already be loaded as a dependency const schemaKeys = Object.keys(schemas); const modules = await schemaKeys.reduce(async (a, x) => { const mods = await a; const mod = await loadModule(mods, schemas, x); if (mod) { mods[x] = mod; } return mods; }, Promise.resolve({})); return Object.values(modules); } exports.default = async (env) => { const modules = await loadModules(env.rootPath); const appModule = new core_1.GraphQLModule({ context: (session) => session.ctx, imports: modules, }); const { schema, context } = appModule; const server = new apollo_server_koa_1.ApolloServer({ context, formatError: (err) => { env.log.error(err.originalError); return err; }, schema, }); return { apply: () => server.applyMiddleware({ app: env.app }), rank: 98, }; }; //# sourceMappingURL=graphql.js.map