1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.pickExportFromModuleSync = exports.pickExportFromModule = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const helpers_js_1 = require("./helpers.js");
|
6 | const identifiersToLookFor = ['default', 'schema', 'typeDefs', 'data'];
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function pickExportFromModule({ module, filepath }) {
|
12 | ensureModule({ module, filepath });
|
13 | return resolveModule(ensureExports({ module, filepath }));
|
14 | }
|
15 | exports.pickExportFromModule = pickExportFromModule;
|
16 |
|
17 |
|
18 |
|
19 | function pickExportFromModuleSync({ module, filepath }) {
|
20 | ensureModule({ module, filepath });
|
21 | return resolveModuleSync(ensureExports({ module, filepath }));
|
22 | }
|
23 | exports.pickExportFromModuleSync = pickExportFromModuleSync;
|
24 |
|
25 | async function resolveModule(identifiers) {
|
26 | const exportValue = await (0, helpers_js_1.pick)(await identifiers, identifiersToLookFor);
|
27 | return resolveExport(exportValue);
|
28 | }
|
29 | function resolveModuleSync(identifiers) {
|
30 | const exportValue = (0, helpers_js_1.pick)(identifiers, identifiersToLookFor);
|
31 | return resolveExport(exportValue);
|
32 | }
|
33 |
|
34 | function ensureModule({ module, filepath }) {
|
35 | if (!module) {
|
36 | throw new Error(`Invalid export from export file ${filepath}: empty export!`);
|
37 | }
|
38 | }
|
39 | function 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 |
|
47 | function 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 | }
|