UNPKG

4.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const graphql_toolkit_1 = require("graphql-toolkit");
4const graphql_1 = require("graphql");
5const errors_1 = require("./errors");
6async function getCustomLoaderByPath(path) {
7 const requiredModule = await Promise.resolve().then(() => require(path));
8 if (requiredModule && requiredModule.default && typeof requiredModule.default === 'function') {
9 return requiredModule.default;
10 }
11 if (requiredModule && typeof requiredModule === 'function') {
12 return requiredModule;
13 }
14 return null;
15}
16exports.loadSchema = async (schemaDef, config) => {
17 if (typeof schemaDef === 'object' &&
18 schemaDef[Object.keys(schemaDef)[0]] &&
19 schemaDef[Object.keys(schemaDef)[0]].loader &&
20 typeof schemaDef[Object.keys(schemaDef)[0]].loader === 'string') {
21 const pointToSchema = Object.keys(schemaDef)[0];
22 const defObject = schemaDef[pointToSchema];
23 const loaderString = defObject.loader;
24 try {
25 const customSchemaLoader = await getCustomLoaderByPath(loaderString);
26 if (customSchemaLoader) {
27 const returnedSchema = await customSchemaLoader(pointToSchema, config, defObject);
28 if (returnedSchema && returnedSchema instanceof graphql_1.GraphQLSchema) {
29 return returnedSchema;
30 }
31 else {
32 throw new Error(`Return value of a custom schema loader must be of type "GraphQLSchema"!`);
33 }
34 }
35 else {
36 throw new Error(`Unable to find a loader function! Make sure to export a default function from your file`);
37 }
38 }
39 catch (e) {
40 throw new errors_1.DetailedError('Failed to load custom schema loader', `
41 Failed to load schema from ${pointToSchema} using loader "${loaderString}":
42
43 ${e.message}
44 ${e.stack}
45 `, `${pointToSchema} using loader "${loaderString}"`);
46 }
47 }
48 try {
49 let pointToSchema = null;
50 let options = {};
51 if (typeof schemaDef === 'string') {
52 pointToSchema = schemaDef;
53 }
54 else if (typeof schemaDef === 'object') {
55 pointToSchema = Object.keys(schemaDef)[0];
56 options = schemaDef[pointToSchema];
57 }
58 if (config.pluckConfig) {
59 options.tagPluck = config.pluckConfig;
60 }
61 return graphql_toolkit_1.loadSchema(pointToSchema, options);
62 }
63 catch (e) {
64 throw new errors_1.DetailedError('Failed to load schema', `
65 Failed to load schema from ${schemaDef}.
66
67 GraphQL Code Generator supports:
68 - ES Modules and CommonJS exports
69 - Introspection JSON File
70 - URL of GraphQL endpoint
71 - Multiple files with type definitions
72 - String in config file
73
74 Try to use one of above options and run codegen again.
75
76 `);
77 }
78};
79exports.loadDocuments = async (documentDef, config) => {
80 if (typeof documentDef === 'object' &&
81 documentDef[Object.keys(documentDef)[0]] &&
82 documentDef[Object.keys(documentDef)[0]].loader &&
83 typeof documentDef[Object.keys(documentDef)[0]].loader === 'string') {
84 const pointToDoc = Object.keys(documentDef)[0];
85 const defObject = documentDef[pointToDoc];
86 const loaderString = defObject.loader;
87 try {
88 const customDocumentLoader = await getCustomLoaderByPath(loaderString);
89 if (customDocumentLoader) {
90 const returned = await customDocumentLoader(pointToDoc, config);
91 if (returned && Array.isArray(returned)) {
92 return returned;
93 }
94 else {
95 throw new Error(`Return value of a custom schema loader must be an Array of: { filePath: string, content: DocumentNode }`);
96 }
97 }
98 else {
99 throw new Error(`Unable to find a loader function! Make sure to export a default function from your file`);
100 }
101 }
102 catch (e) {
103 throw new errors_1.DetailedError('Failed to load custom documents loader', `
104 Failed to load documents from ${pointToDoc} using loader "${loaderString}":
105
106 ${e.message}
107 `);
108 }
109 }
110 return graphql_toolkit_1.loadDocuments(documentDef, config.pluckConfig
111 ? {
112 tagPluck: config.pluckConfig
113 }
114 : {});
115};
116//# sourceMappingURL=load.js.map
\No newline at end of file