UNPKG

graphqldoc

Version:
39 lines (38 loc) 1.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const introspectionParser_1 = require("./introspectionParser"); const graphql_1 = require("graphql"); const utility_1 = require("../utility"); const path_1 = require("path"); exports.jsSchemaLoader = function (options) { return __awaiter(this, void 0, void 0, function* () { const schemaPath = path_1.resolve(options.schemaFile); let schemaModule = require(schemaPath); let schema; // check if exist default in module if (typeof schemaModule === 'object') { schemaModule = schemaModule.default; } // check for array of definition if (Array.isArray(schemaModule)) { schema = schemaModule.join(''); // check for array array wrapped in a function } else if (typeof schemaModule === 'function') { schema = schemaModule().join(''); } else { throw new Error(`Unexpected schema definition on "${schemaModule}", must be an array or function`); } const introspection = yield graphql_1.execute(graphql_1.buildSchema(schema), graphql_1.parse(utility_1.query)); return introspectionParser_1.getSchemaFromIntrospection(introspection); }); };