1 | import { loadSchema as loadSchemaToolkit, loadDocuments as loadDocumentsToolkit } from 'graphql-toolkit';
|
2 | import { GraphQLSchema } from 'graphql';
|
3 | import { DetailedError } from './errors';
|
4 | function getCustomLoaderByPath(path) {
|
5 | const requiredModule = require(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 | }
|
14 | export 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 = 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 | return loadSchemaToolkit(pointToSchema, options);
|
57 | }
|
58 | catch (e) {
|
59 | throw new DetailedError('Failed to load schema', `
|
60 | Failed to load schema from ${schemaDef}.
|
61 |
|
62 | GraphQL Code Generator supports:
|
63 | - ES Modules and CommonJS exports
|
64 | - Introspection JSON File
|
65 | - URL of GraphQL endpoint
|
66 | - Multiple files with type definitions
|
67 | - String in config file
|
68 |
|
69 | Try to use one of above options and run codegen again.
|
70 |
|
71 | `);
|
72 | }
|
73 | };
|
74 | export const loadDocuments = async (documentDef, config) => {
|
75 | if (typeof documentDef === 'object' &&
|
76 | documentDef[Object.keys(documentDef)[0]] &&
|
77 | documentDef[Object.keys(documentDef)[0]].loader &&
|
78 | typeof documentDef[Object.keys(documentDef)[0]].loader === 'string') {
|
79 | const pointToDoc = Object.keys(documentDef)[0];
|
80 | const defObject = documentDef[pointToDoc];
|
81 | const loaderString = defObject.loader;
|
82 | try {
|
83 | const customDocumentLoader = getCustomLoaderByPath(loaderString);
|
84 | if (customDocumentLoader) {
|
85 | const returned = await customDocumentLoader(pointToDoc, config);
|
86 | if (returned && Array.isArray(returned)) {
|
87 | return returned;
|
88 | }
|
89 | else {
|
90 | throw new Error(`Return value of a custom schema loader must be an Array of: { filePath: string, content: DocumentNode }`);
|
91 | }
|
92 | }
|
93 | else {
|
94 | throw new Error(`Unable to find a loader function! Make sure to export a default function from your file`);
|
95 | }
|
96 | }
|
97 | catch (e) {
|
98 | throw new DetailedError('Failed to load custom documents loader', `
|
99 | Failed to load documents from ${pointToDoc} using loader "${loaderString}":
|
100 |
|
101 | ${e.message}
|
102 | `);
|
103 | }
|
104 | }
|
105 | return loadDocumentsToolkit(documentDef);
|
106 | };
|
107 |
|
\ | No newline at end of file |