-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
load.ts
112 lines (101 loc) · 3.03 KB
/
load.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {
loadSchema as loadSchemaToolkit,
loadDocuments as loadDocumentsToolkit,
UnnormalizedTypeDefPointer,
} from '@graphql-tools/load';
import { Types } from '@graphql-codegen/plugin-helpers';
import { GraphQLSchema } from 'graphql';
import { CodeFileLoader } from '@graphql-tools/code-file-loader';
import { GitLoader } from '@graphql-tools/git-loader';
import { GithubLoader } from '@graphql-tools/github-loader';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { JsonFileLoader } from '@graphql-tools/json-file-loader';
import { UrlLoader } from '@graphql-tools/url-loader';
import { ApolloEngineLoader } from '@graphql-tools/apollo-engine-loader';
import { PrismaLoader } from '@graphql-tools/prisma-loader';
import { join, extname } from 'path';
export const defaultSchemaLoadOptions = {
assumeValidSDL: true,
sort: true,
convertExtensions: true,
includeSources: true,
};
export const defaultDocumentsLoadOptions = {
sort: true,
skipGraphQLImport: true,
};
export async function loadSchema(
schemaPointers: UnnormalizedTypeDefPointer,
config: Types.Config
): Promise<GraphQLSchema> {
try {
const loaders = [
new CodeFileLoader(),
new GitLoader(),
new GithubLoader(),
new GraphQLFileLoader(),
new JsonFileLoader(),
new UrlLoader(),
new ApolloEngineLoader(),
new PrismaLoader(),
];
const schema = await loadSchemaToolkit(schemaPointers, {
...defaultSchemaLoadOptions,
loaders,
...config,
...config.config,
});
return schema;
} catch (e) {
throw new Error(
`
Failed to load schema from ${Object.keys(schemaPointers).join(',')}:
${e.message || e}
${e.stack || ''}
GraphQL Code Generator supports:
- ES Modules and CommonJS exports (export as default or named export "schema")
- Introspection JSON File
- URL of GraphQL endpoint
- Multiple files with type definitions (glob expression)
- String in config file
Try to use one of above options and run codegen again.
`
);
}
}
export async function loadDocuments(
documentPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
config: Types.Config
): Promise<Types.DocumentFile[]> {
const loaders = [
new CodeFileLoader({
pluckConfig: {
skipIndent: true,
},
}),
new GitLoader(),
new GithubLoader(),
new GraphQLFileLoader(),
];
const ignore: Array<string> = [];
for (const generatePath of Object.keys(config.generates)) {
if (extname(generatePath) === '') {
// we omit paths that don't resolve to a specific file
continue;
}
ignore.push(join(process.cwd(), generatePath));
}
try {
const loadedFromToolkit = await loadDocumentsToolkit(documentPointers, {
...defaultDocumentsLoadOptions,
ignore,
loaders,
...config,
...config.config,
});
return loadedFromToolkit;
} catch (error) {
if (config.ignoreNoDocuments) return [];
throw error;
}
}