UNPKG

2.47 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.pickExportFromModuleSync = exports.pickExportFromModule = void 0;
4const graphql_1 = require("graphql");
5const helpers_js_1 = require("./helpers.js");
6const identifiersToLookFor = ['default', 'schema', 'typeDefs', 'data'];
7// Pick exports
8/**
9 * @internal
10 */
11function pickExportFromModule({ module, filepath }) {
12 ensureModule({ module, filepath });
13 return resolveModule(ensureExports({ module, filepath }));
14}
15exports.pickExportFromModule = pickExportFromModule;
16/**
17 * @internal
18 */
19function pickExportFromModuleSync({ module, filepath }) {
20 ensureModule({ module, filepath });
21 return resolveModuleSync(ensureExports({ module, filepath }));
22}
23exports.pickExportFromModuleSync = pickExportFromModuleSync;
24// module
25async function resolveModule(identifiers) {
26 const exportValue = await (0, helpers_js_1.pick)(await identifiers, identifiersToLookFor);
27 return resolveExport(exportValue);
28}
29function resolveModuleSync(identifiers) {
30 const exportValue = (0, helpers_js_1.pick)(identifiers, identifiersToLookFor);
31 return resolveExport(exportValue);
32}
33// validate
34function ensureModule({ module, filepath }) {
35 if (!module) {
36 throw new Error(`Invalid export from export file ${filepath}: empty export!`);
37 }
38}
39function ensureExports({ module, filepath }) {
40 const identifiers = (0, helpers_js_1.pick)(module, identifiersToLookFor);
41 if (!identifiers) {
42 throw new Error(`Invalid export from export file ${filepath}: missing default export or 'schema' export!`);
43 }
44 return identifiers;
45}
46// Decide what to do with an exported value
47function resolveExport(fileExport) {
48 try {
49 if ((0, graphql_1.isSchema)(fileExport)) {
50 return fileExport;
51 }
52 if ((0, helpers_js_1.isSchemaText)(fileExport)) {
53 return (0, graphql_1.parse)(fileExport);
54 }
55 if ((0, helpers_js_1.isWrappedSchemaJson)(fileExport)) {
56 return (0, graphql_1.buildClientSchema)(fileExport.data);
57 }
58 if ((0, helpers_js_1.isSchemaJson)(fileExport)) {
59 return (0, graphql_1.buildClientSchema)(fileExport);
60 }
61 if ((0, helpers_js_1.isSchemaAst)(fileExport)) {
62 return fileExport;
63 }
64 return null;
65 }
66 catch (e) {
67 throw new Error('Exported schema must be of type GraphQLSchema, text, AST, or introspection JSON.');
68 }
69}