UNPKG

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