@graphql-codegen/core 2.6.0-alpha-d6c7238c0.0 → 2.6.0-alpha-bd464a586.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{index.js → cjs/codegen.js} +37 -144
- package/cjs/execute-plugin.js +42 -0
- package/cjs/index.js +7 -0
- package/cjs/package.json +1 -0
- package/cjs/utils.js +79 -0
- package/{index.mjs → esm/codegen.js} +11 -118
- package/esm/execute-plugin.js +38 -0
- package/esm/index.js +2 -0
- package/esm/utils.js +68 -0
- package/package.json +24 -17
- package/{codegen.d.ts → typings/codegen.d.ts} +0 -0
- package/{execute-plugin.d.ts → typings/execute-plugin.d.ts} +0 -0
- package/{index.d.ts → typings/index.d.ts} +2 -2
- /package/{utils.d.ts → typings/utils.d.ts} +0 -0
|
@@ -1,122 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
async function executePlugin(options, plugin) {
|
|
11
|
-
var _a;
|
|
12
|
-
if (!plugin || !plugin.plugin || typeof plugin.plugin !== 'function') {
|
|
13
|
-
throw new pluginHelpers.DetailedError(`Invalid Custom Plugin "${options.name}"`, `
|
|
14
|
-
Plugin ${options.name} does not export a valid JS object with "plugin" function.
|
|
15
|
-
|
|
16
|
-
Make sure your custom plugin is written in the following form:
|
|
17
|
-
|
|
18
|
-
module.exports = {
|
|
19
|
-
plugin: (schema, documents, config) => {
|
|
20
|
-
return 'my-custom-plugin-content';
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
`);
|
|
24
|
-
}
|
|
25
|
-
const outputSchema = options.schemaAst || graphql.buildASTSchema(options.schema, options.config);
|
|
26
|
-
const documents = options.documents || [];
|
|
27
|
-
const pluginContext = options.pluginContext || {};
|
|
28
|
-
const profiler = (_a = options.profiler) !== null && _a !== void 0 ? _a : pluginHelpers.createNoopProfiler();
|
|
29
|
-
if (plugin.validate && typeof plugin.validate === 'function') {
|
|
30
|
-
try {
|
|
31
|
-
// FIXME: Sync validate signature with plugin signature
|
|
32
|
-
await profiler.run(async () => plugin.validate(outputSchema, documents, options.config, options.outputFilename, options.allPlugins, pluginContext), `Plugin ${options.name} validate`);
|
|
33
|
-
}
|
|
34
|
-
catch (e) {
|
|
35
|
-
throw new pluginHelpers.DetailedError(`Plugin "${options.name}" validation failed:`, `
|
|
36
|
-
${e.message}
|
|
37
|
-
`);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return profiler.run(() => Promise.resolve(plugin.plugin(outputSchema, documents, typeof options.config === 'object' ? { ...options.config } : options.config, {
|
|
41
|
-
outputFile: options.outputFilename,
|
|
42
|
-
allPlugins: options.allPlugins,
|
|
43
|
-
pluginContext,
|
|
44
|
-
})), `Plugin ${options.name} execution`);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function isObjectMap(obj) {
|
|
48
|
-
return obj && typeof obj === 'object' && !Array.isArray(obj);
|
|
49
|
-
}
|
|
50
|
-
function prioritize(...values) {
|
|
51
|
-
const picked = values.find(val => typeof val === 'boolean');
|
|
52
|
-
if (typeof picked !== 'boolean') {
|
|
53
|
-
return values[values.length - 1];
|
|
54
|
-
}
|
|
55
|
-
return picked;
|
|
56
|
-
}
|
|
57
|
-
function pickFlag(flag, config) {
|
|
58
|
-
return isObjectMap(config) ? config[flag] : undefined;
|
|
59
|
-
}
|
|
60
|
-
function shouldValidateDuplicateDocuments(skipDocumentsValidationOption) {
|
|
61
|
-
// If the value is true, skip all
|
|
62
|
-
if (skipDocumentsValidationOption === true) {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
// If the value is object with the specific flag, only skip this one
|
|
66
|
-
if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipDuplicateValidation) {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
// If the value is falsy or the specific flag is not set, validate
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
function shouldValidateDocumentsAgainstSchema(skipDocumentsValidationOption) {
|
|
73
|
-
// If the value is true, skip all
|
|
74
|
-
if (skipDocumentsValidationOption === true) {
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
// If the value is object with the specific flag, only skip this one
|
|
78
|
-
if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipValidationAgainstSchema) {
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
// If the value is falsy or the specific flag is not set, validate
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
function getSkipDocumentsValidationOption(options) {
|
|
85
|
-
// If the value is set on the root level
|
|
86
|
-
if (options.skipDocumentsValidation) {
|
|
87
|
-
return options.skipDocumentsValidation;
|
|
88
|
-
}
|
|
89
|
-
// If the value is set under `config` property
|
|
90
|
-
const flagFromConfig = pickFlag('skipDocumentsValidation', options.config);
|
|
91
|
-
if (flagFromConfig) {
|
|
92
|
-
return flagFromConfig;
|
|
93
|
-
}
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
const federationDirectives = ['key', 'requires', 'provides', 'external'];
|
|
97
|
-
function hasFederationSpec(schemaOrAST) {
|
|
98
|
-
if (graphql.isSchema(schemaOrAST)) {
|
|
99
|
-
return federationDirectives.some(directive => schemaOrAST.getDirective(directive));
|
|
100
|
-
}
|
|
101
|
-
else if (utils.isDocumentNode(schemaOrAST)) {
|
|
102
|
-
return schemaOrAST.definitions.some(def => def.kind === graphql.Kind.DIRECTIVE_DEFINITION && federationDirectives.includes(def.name.value));
|
|
103
|
-
}
|
|
104
|
-
return false;
|
|
105
|
-
}
|
|
106
|
-
function extractHashFromSchema(schema) {
|
|
107
|
-
var _a;
|
|
108
|
-
if (!schema.extensions) {
|
|
109
|
-
schema.extensions = {};
|
|
110
|
-
}
|
|
111
|
-
return (_a = schema.extensions['hash']) !== null && _a !== void 0 ? _a : null;
|
|
112
|
-
}
|
|
113
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sortPrependValues = exports.codegen = void 0;
|
|
4
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
const execute_plugin_js_1 = require("./execute-plugin.js");
|
|
7
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
8
|
+
const schema_1 = require("@graphql-tools/schema");
|
|
9
|
+
const utils_js_1 = require("./utils.js");
|
|
114
10
|
async function codegen(options) {
|
|
115
11
|
var _a;
|
|
116
12
|
const documents = options.documents || [];
|
|
117
|
-
const profiler = (_a = options.profiler) !== null && _a !== void 0 ? _a :
|
|
118
|
-
const skipDocumentsValidation = getSkipDocumentsValidationOption(options);
|
|
119
|
-
if (documents.length > 0 && shouldValidateDuplicateDocuments(skipDocumentsValidation)) {
|
|
13
|
+
const profiler = (_a = options.profiler) !== null && _a !== void 0 ? _a : (0, plugin_helpers_1.createNoopProfiler)();
|
|
14
|
+
const skipDocumentsValidation = (0, utils_js_1.getSkipDocumentsValidationOption)(options);
|
|
15
|
+
if (documents.length > 0 && (0, utils_js_1.shouldValidateDuplicateDocuments)(skipDocumentsValidation)) {
|
|
120
16
|
await profiler.run(async () => validateDuplicateDocuments(documents), 'validateDuplicateDocuments');
|
|
121
17
|
}
|
|
122
18
|
const pluginPackages = Object.keys(options.pluginMap).map(key => options.pluginMap[key]);
|
|
@@ -128,16 +24,16 @@ async function codegen(options) {
|
|
|
128
24
|
additionalTypeDefs.push(addToSchema);
|
|
129
25
|
}
|
|
130
26
|
}
|
|
131
|
-
const federationInConfig = pickFlag('federation', options.config);
|
|
132
|
-
const isFederation = prioritize(federationInConfig, false);
|
|
133
|
-
if (isFederation && !hasFederationSpec(options.schemaAst || options.schema)) {
|
|
134
|
-
additionalTypeDefs.push(
|
|
27
|
+
const federationInConfig = (0, utils_js_1.pickFlag)('federation', options.config);
|
|
28
|
+
const isFederation = (0, utils_js_1.prioritize)(federationInConfig, false);
|
|
29
|
+
if (isFederation && !(0, utils_js_1.hasFederationSpec)(options.schemaAst || options.schema)) {
|
|
30
|
+
additionalTypeDefs.push(plugin_helpers_1.federationSpec);
|
|
135
31
|
}
|
|
136
32
|
// Use mergeSchemas, only if there is no GraphQLSchema provided or the schema should be extended
|
|
137
33
|
const mergeNeeded = !options.schemaAst || additionalTypeDefs.length > 0;
|
|
138
34
|
const schemaInstance = await profiler.run(async () => {
|
|
139
35
|
return mergeNeeded
|
|
140
|
-
?
|
|
36
|
+
? (0, schema_1.mergeSchemas)({
|
|
141
37
|
// If GraphQLSchema provided, use it
|
|
142
38
|
schemas: options.schemaAst ? [options.schemaAst] : [],
|
|
143
39
|
// If GraphQLSchema isn't provided but DocumentNode is, use it to get the final GraphQLSchema
|
|
@@ -149,30 +45,30 @@ async function codegen(options) {
|
|
|
149
45
|
})
|
|
150
46
|
: options.schemaAst;
|
|
151
47
|
}, 'Create schema instance');
|
|
152
|
-
const schemaDocumentNode = mergeNeeded || !options.schema ?
|
|
153
|
-
if (schemaInstance && documents.length > 0 && shouldValidateDocumentsAgainstSchema(skipDocumentsValidation)) {
|
|
48
|
+
const schemaDocumentNode = mergeNeeded || !options.schema ? (0, plugin_helpers_1.getCachedDocumentNodeFromSchema)(schemaInstance) : options.schema;
|
|
49
|
+
if (schemaInstance && documents.length > 0 && (0, utils_js_1.shouldValidateDocumentsAgainstSchema)(skipDocumentsValidation)) {
|
|
154
50
|
const ignored = ['NoUnusedFragments', 'NoUnusedVariables', 'KnownDirectives'];
|
|
155
51
|
if (typeof skipDocumentsValidation === 'object' && skipDocumentsValidation.ignoreRules) {
|
|
156
|
-
ignored.push(...
|
|
52
|
+
ignored.push(...(0, utils_1.asArray)(skipDocumentsValidation.ignoreRules));
|
|
157
53
|
}
|
|
158
|
-
const extraFragments = pickFlag('externalFragments', options.config) || [];
|
|
54
|
+
const extraFragments = (0, utils_js_1.pickFlag)('externalFragments', options.config) || [];
|
|
159
55
|
const errors = await profiler.run(() => {
|
|
160
56
|
const fragments = extraFragments.map(f => ({
|
|
161
57
|
location: f.importFrom,
|
|
162
|
-
document: { kind:
|
|
58
|
+
document: { kind: graphql_1.Kind.DOCUMENT, definitions: [f.node] },
|
|
163
59
|
}));
|
|
164
|
-
const rules =
|
|
165
|
-
const schemaHash = extractHashFromSchema(schemaInstance);
|
|
60
|
+
const rules = graphql_1.specifiedRules.filter(rule => !ignored.some(ignoredRule => rule.name.startsWith(ignoredRule)));
|
|
61
|
+
const schemaHash = (0, utils_js_1.extractHashFromSchema)(schemaInstance);
|
|
166
62
|
if (!schemaHash || !options.cache || documents.some(d => typeof d.hash !== 'string')) {
|
|
167
|
-
return
|
|
63
|
+
return (0, utils_1.validateGraphQlDocuments)(schemaInstance, [...documents, ...fragments], rules);
|
|
168
64
|
}
|
|
169
65
|
const cacheKey = [schemaHash]
|
|
170
66
|
.concat(documents.map(doc => doc.hash))
|
|
171
67
|
.concat(JSON.stringify(fragments))
|
|
172
68
|
.join(',');
|
|
173
|
-
return options.cache('documents-validation', cacheKey, () =>
|
|
69
|
+
return options.cache('documents-validation', cacheKey, () => (0, utils_1.validateGraphQlDocuments)(schemaInstance, [...documents, ...fragments], rules));
|
|
174
70
|
}, 'Validate documents against schema');
|
|
175
|
-
|
|
71
|
+
(0, utils_1.checkValidationErrors)(errors);
|
|
176
72
|
}
|
|
177
73
|
const prepend = new Set();
|
|
178
74
|
const append = new Set();
|
|
@@ -186,7 +82,7 @@ async function codegen(options) {
|
|
|
186
82
|
...options.config,
|
|
187
83
|
...pluginConfig,
|
|
188
84
|
};
|
|
189
|
-
const result = await profiler.run(() => executePlugin({
|
|
85
|
+
const result = await profiler.run(() => (0, execute_plugin_js_1.executePlugin)({
|
|
190
86
|
name,
|
|
191
87
|
config: execConfig,
|
|
192
88
|
parentConfig: options.config,
|
|
@@ -202,7 +98,7 @@ async function codegen(options) {
|
|
|
202
98
|
if (typeof result === 'string') {
|
|
203
99
|
return result || '';
|
|
204
100
|
}
|
|
205
|
-
|
|
101
|
+
if ((0, plugin_helpers_1.isComplexPluginOutput)(result)) {
|
|
206
102
|
if (result.append && result.append.length > 0) {
|
|
207
103
|
for (const item of result.append) {
|
|
208
104
|
if (item) {
|
|
@@ -225,19 +121,18 @@ async function codegen(options) {
|
|
|
225
121
|
.filter(Boolean)
|
|
226
122
|
.join('\n');
|
|
227
123
|
}
|
|
124
|
+
exports.codegen = codegen;
|
|
228
125
|
function resolveCompareValue(a) {
|
|
229
126
|
if (a.startsWith('/*') || a.startsWith('//') || a.startsWith(' *') || a.startsWith(' */') || a.startsWith('*/')) {
|
|
230
127
|
return 0;
|
|
231
128
|
}
|
|
232
|
-
|
|
129
|
+
if (a.startsWith('package')) {
|
|
233
130
|
return 1;
|
|
234
131
|
}
|
|
235
|
-
|
|
132
|
+
if (a.startsWith('import')) {
|
|
236
133
|
return 2;
|
|
237
134
|
}
|
|
238
|
-
|
|
239
|
-
return 3;
|
|
240
|
-
}
|
|
135
|
+
return 3;
|
|
241
136
|
}
|
|
242
137
|
function sortPrependValues(values) {
|
|
243
138
|
return values.sort((a, b) => {
|
|
@@ -252,6 +147,7 @@ function sortPrependValues(values) {
|
|
|
252
147
|
return 0;
|
|
253
148
|
});
|
|
254
149
|
}
|
|
150
|
+
exports.sortPrependValues = sortPrependValues;
|
|
255
151
|
function validateDuplicateDocuments(files) {
|
|
256
152
|
// duplicated names
|
|
257
153
|
const definitionMap = {};
|
|
@@ -269,7 +165,7 @@ function validateDuplicateDocuments(files) {
|
|
|
269
165
|
const definitionKindMap = definitionMap[node.kind];
|
|
270
166
|
const length = definitionKindMap[node.name.value].contents.size;
|
|
271
167
|
definitionKindMap[node.name.value].paths.add(file.location);
|
|
272
|
-
definitionKindMap[node.name.value].contents.add(
|
|
168
|
+
definitionKindMap[node.name.value].contents.add((0, graphql_1.print)(node));
|
|
273
169
|
if (length === definitionKindMap[node.name.value].contents.size) {
|
|
274
170
|
return null;
|
|
275
171
|
}
|
|
@@ -278,7 +174,7 @@ function validateDuplicateDocuments(files) {
|
|
|
278
174
|
}
|
|
279
175
|
files.forEach(file => {
|
|
280
176
|
const deduplicatedDefinitions = new Set();
|
|
281
|
-
|
|
177
|
+
(0, graphql_1.visit)(file.document, {
|
|
282
178
|
OperationDefinition(node) {
|
|
283
179
|
addDefinition(file, node, deduplicatedDefinitions);
|
|
284
180
|
},
|
|
@@ -310,13 +206,10 @@ function validateDuplicateDocuments(files) {
|
|
|
310
206
|
`.trimRight())
|
|
311
207
|
.join('');
|
|
312
208
|
const definitionKindName = kind.replace('Definition', '').toLowerCase();
|
|
313
|
-
throw new
|
|
209
|
+
throw new plugin_helpers_1.DetailedError(`Not all ${definitionKindName}s have an unique name: ${duplicated.join(', ')}`, `
|
|
314
210
|
Not all ${definitionKindName}s have an unique name
|
|
315
211
|
${list}
|
|
316
212
|
`);
|
|
317
213
|
}
|
|
318
214
|
});
|
|
319
215
|
}
|
|
320
|
-
|
|
321
|
-
exports.codegen = codegen;
|
|
322
|
-
exports.executePlugin = executePlugin;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.executePlugin = void 0;
|
|
4
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
async function executePlugin(options, plugin) {
|
|
7
|
+
var _a;
|
|
8
|
+
if (!plugin || !plugin.plugin || typeof plugin.plugin !== 'function') {
|
|
9
|
+
throw new plugin_helpers_1.DetailedError(`Invalid Custom Plugin "${options.name}"`, `
|
|
10
|
+
Plugin ${options.name} does not export a valid JS object with "plugin" function.
|
|
11
|
+
|
|
12
|
+
Make sure your custom plugin is written in the following form:
|
|
13
|
+
|
|
14
|
+
module.exports = {
|
|
15
|
+
plugin: (schema, documents, config) => {
|
|
16
|
+
return 'my-custom-plugin-content';
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
`);
|
|
20
|
+
}
|
|
21
|
+
const outputSchema = options.schemaAst || (0, graphql_1.buildASTSchema)(options.schema, options.config);
|
|
22
|
+
const documents = options.documents || [];
|
|
23
|
+
const pluginContext = options.pluginContext || {};
|
|
24
|
+
const profiler = (_a = options.profiler) !== null && _a !== void 0 ? _a : (0, plugin_helpers_1.createNoopProfiler)();
|
|
25
|
+
if (plugin.validate && typeof plugin.validate === 'function') {
|
|
26
|
+
try {
|
|
27
|
+
// FIXME: Sync validate signature with plugin signature
|
|
28
|
+
await profiler.run(async () => plugin.validate(outputSchema, documents, options.config, options.outputFilename, options.allPlugins, pluginContext), `Plugin ${options.name} validate`);
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
throw new plugin_helpers_1.DetailedError(`Plugin "${options.name}" validation failed:`, `
|
|
32
|
+
${e.message}
|
|
33
|
+
`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return profiler.run(() => Promise.resolve(plugin.plugin(outputSchema, documents, typeof options.config === 'object' ? { ...options.config } : options.config, {
|
|
37
|
+
outputFile: options.outputFilename,
|
|
38
|
+
allPlugins: options.allPlugins,
|
|
39
|
+
pluginContext,
|
|
40
|
+
})), `Plugin ${options.name} execution`);
|
|
41
|
+
}
|
|
42
|
+
exports.executePlugin = executePlugin;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.executePlugin = exports.codegen = void 0;
|
|
4
|
+
var codegen_js_1 = require("./codegen.js");
|
|
5
|
+
Object.defineProperty(exports, "codegen", { enumerable: true, get: function () { return codegen_js_1.codegen; } });
|
|
6
|
+
var execute_plugin_js_1 = require("./execute-plugin.js");
|
|
7
|
+
Object.defineProperty(exports, "executePlugin", { enumerable: true, get: function () { return execute_plugin_js_1.executePlugin; } });
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/cjs/utils.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractHashFromSchema = exports.hasFederationSpec = exports.getSkipDocumentsValidationOption = exports.shouldValidateDocumentsAgainstSchema = exports.shouldValidateDuplicateDocuments = exports.pickFlag = exports.prioritize = exports.isObjectMap = void 0;
|
|
4
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
function isObjectMap(obj) {
|
|
7
|
+
return obj && typeof obj === 'object' && !Array.isArray(obj);
|
|
8
|
+
}
|
|
9
|
+
exports.isObjectMap = isObjectMap;
|
|
10
|
+
function prioritize(...values) {
|
|
11
|
+
const picked = values.find(val => typeof val === 'boolean');
|
|
12
|
+
if (typeof picked !== 'boolean') {
|
|
13
|
+
return values[values.length - 1];
|
|
14
|
+
}
|
|
15
|
+
return picked;
|
|
16
|
+
}
|
|
17
|
+
exports.prioritize = prioritize;
|
|
18
|
+
function pickFlag(flag, config) {
|
|
19
|
+
return isObjectMap(config) ? config[flag] : undefined;
|
|
20
|
+
}
|
|
21
|
+
exports.pickFlag = pickFlag;
|
|
22
|
+
function shouldValidateDuplicateDocuments(skipDocumentsValidationOption) {
|
|
23
|
+
// If the value is true, skip all
|
|
24
|
+
if (skipDocumentsValidationOption === true) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
// If the value is object with the specific flag, only skip this one
|
|
28
|
+
if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipDuplicateValidation) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
// If the value is falsy or the specific flag is not set, validate
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
exports.shouldValidateDuplicateDocuments = shouldValidateDuplicateDocuments;
|
|
35
|
+
function shouldValidateDocumentsAgainstSchema(skipDocumentsValidationOption) {
|
|
36
|
+
// If the value is true, skip all
|
|
37
|
+
if (skipDocumentsValidationOption === true) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
// If the value is object with the specific flag, only skip this one
|
|
41
|
+
if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipValidationAgainstSchema) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
// If the value is falsy or the specific flag is not set, validate
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
exports.shouldValidateDocumentsAgainstSchema = shouldValidateDocumentsAgainstSchema;
|
|
48
|
+
function getSkipDocumentsValidationOption(options) {
|
|
49
|
+
// If the value is set on the root level
|
|
50
|
+
if (options.skipDocumentsValidation) {
|
|
51
|
+
return options.skipDocumentsValidation;
|
|
52
|
+
}
|
|
53
|
+
// If the value is set under `config` property
|
|
54
|
+
const flagFromConfig = pickFlag('skipDocumentsValidation', options.config);
|
|
55
|
+
if (flagFromConfig) {
|
|
56
|
+
return flagFromConfig;
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
exports.getSkipDocumentsValidationOption = getSkipDocumentsValidationOption;
|
|
61
|
+
const federationDirectives = ['key', 'requires', 'provides', 'external'];
|
|
62
|
+
function hasFederationSpec(schemaOrAST) {
|
|
63
|
+
if ((0, graphql_1.isSchema)(schemaOrAST)) {
|
|
64
|
+
return federationDirectives.some(directive => schemaOrAST.getDirective(directive));
|
|
65
|
+
}
|
|
66
|
+
if ((0, utils_1.isDocumentNode)(schemaOrAST)) {
|
|
67
|
+
return schemaOrAST.definitions.some(def => def.kind === graphql_1.Kind.DIRECTIVE_DEFINITION && federationDirectives.includes(def.name.value));
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
exports.hasFederationSpec = hasFederationSpec;
|
|
72
|
+
function extractHashFromSchema(schema) {
|
|
73
|
+
var _a;
|
|
74
|
+
if (!schema.extensions) {
|
|
75
|
+
schema.extensions = {};
|
|
76
|
+
}
|
|
77
|
+
return (_a = schema.extensions['hash']) !== null && _a !== void 0 ? _a : null;
|
|
78
|
+
}
|
|
79
|
+
exports.extractHashFromSchema = extractHashFromSchema;
|
|
@@ -1,113 +1,10 @@
|
|
|
1
|
-
import { DetailedError,
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { DetailedError, isComplexPluginOutput, federationSpec, getCachedDocumentNodeFromSchema, createNoopProfiler, } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import { visit, Kind, print, specifiedRules } from 'graphql';
|
|
3
|
+
import { executePlugin } from './execute-plugin.js';
|
|
4
|
+
import { checkValidationErrors, validateGraphQlDocuments, asArray } from '@graphql-tools/utils';
|
|
4
5
|
import { mergeSchemas } from '@graphql-tools/schema';
|
|
5
|
-
|
|
6
|
-
async function
|
|
7
|
-
var _a;
|
|
8
|
-
if (!plugin || !plugin.plugin || typeof plugin.plugin !== 'function') {
|
|
9
|
-
throw new DetailedError(`Invalid Custom Plugin "${options.name}"`, `
|
|
10
|
-
Plugin ${options.name} does not export a valid JS object with "plugin" function.
|
|
11
|
-
|
|
12
|
-
Make sure your custom plugin is written in the following form:
|
|
13
|
-
|
|
14
|
-
module.exports = {
|
|
15
|
-
plugin: (schema, documents, config) => {
|
|
16
|
-
return 'my-custom-plugin-content';
|
|
17
|
-
},
|
|
18
|
-
};
|
|
19
|
-
`);
|
|
20
|
-
}
|
|
21
|
-
const outputSchema = options.schemaAst || buildASTSchema(options.schema, options.config);
|
|
22
|
-
const documents = options.documents || [];
|
|
23
|
-
const pluginContext = options.pluginContext || {};
|
|
24
|
-
const profiler = (_a = options.profiler) !== null && _a !== void 0 ? _a : createNoopProfiler();
|
|
25
|
-
if (plugin.validate && typeof plugin.validate === 'function') {
|
|
26
|
-
try {
|
|
27
|
-
// FIXME: Sync validate signature with plugin signature
|
|
28
|
-
await profiler.run(async () => plugin.validate(outputSchema, documents, options.config, options.outputFilename, options.allPlugins, pluginContext), `Plugin ${options.name} validate`);
|
|
29
|
-
}
|
|
30
|
-
catch (e) {
|
|
31
|
-
throw new DetailedError(`Plugin "${options.name}" validation failed:`, `
|
|
32
|
-
${e.message}
|
|
33
|
-
`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return profiler.run(() => Promise.resolve(plugin.plugin(outputSchema, documents, typeof options.config === 'object' ? { ...options.config } : options.config, {
|
|
37
|
-
outputFile: options.outputFilename,
|
|
38
|
-
allPlugins: options.allPlugins,
|
|
39
|
-
pluginContext,
|
|
40
|
-
})), `Plugin ${options.name} execution`);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function isObjectMap(obj) {
|
|
44
|
-
return obj && typeof obj === 'object' && !Array.isArray(obj);
|
|
45
|
-
}
|
|
46
|
-
function prioritize(...values) {
|
|
47
|
-
const picked = values.find(val => typeof val === 'boolean');
|
|
48
|
-
if (typeof picked !== 'boolean') {
|
|
49
|
-
return values[values.length - 1];
|
|
50
|
-
}
|
|
51
|
-
return picked;
|
|
52
|
-
}
|
|
53
|
-
function pickFlag(flag, config) {
|
|
54
|
-
return isObjectMap(config) ? config[flag] : undefined;
|
|
55
|
-
}
|
|
56
|
-
function shouldValidateDuplicateDocuments(skipDocumentsValidationOption) {
|
|
57
|
-
// If the value is true, skip all
|
|
58
|
-
if (skipDocumentsValidationOption === true) {
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
// If the value is object with the specific flag, only skip this one
|
|
62
|
-
if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipDuplicateValidation) {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
// If the value is falsy or the specific flag is not set, validate
|
|
66
|
-
return true;
|
|
67
|
-
}
|
|
68
|
-
function shouldValidateDocumentsAgainstSchema(skipDocumentsValidationOption) {
|
|
69
|
-
// If the value is true, skip all
|
|
70
|
-
if (skipDocumentsValidationOption === true) {
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
// If the value is object with the specific flag, only skip this one
|
|
74
|
-
if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipValidationAgainstSchema) {
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
// If the value is falsy or the specific flag is not set, validate
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
function getSkipDocumentsValidationOption(options) {
|
|
81
|
-
// If the value is set on the root level
|
|
82
|
-
if (options.skipDocumentsValidation) {
|
|
83
|
-
return options.skipDocumentsValidation;
|
|
84
|
-
}
|
|
85
|
-
// If the value is set under `config` property
|
|
86
|
-
const flagFromConfig = pickFlag('skipDocumentsValidation', options.config);
|
|
87
|
-
if (flagFromConfig) {
|
|
88
|
-
return flagFromConfig;
|
|
89
|
-
}
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
const federationDirectives = ['key', 'requires', 'provides', 'external'];
|
|
93
|
-
function hasFederationSpec(schemaOrAST) {
|
|
94
|
-
if (isSchema(schemaOrAST)) {
|
|
95
|
-
return federationDirectives.some(directive => schemaOrAST.getDirective(directive));
|
|
96
|
-
}
|
|
97
|
-
else if (isDocumentNode(schemaOrAST)) {
|
|
98
|
-
return schemaOrAST.definitions.some(def => def.kind === Kind.DIRECTIVE_DEFINITION && federationDirectives.includes(def.name.value));
|
|
99
|
-
}
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
function extractHashFromSchema(schema) {
|
|
103
|
-
var _a;
|
|
104
|
-
if (!schema.extensions) {
|
|
105
|
-
schema.extensions = {};
|
|
106
|
-
}
|
|
107
|
-
return (_a = schema.extensions['hash']) !== null && _a !== void 0 ? _a : null;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async function codegen(options) {
|
|
6
|
+
import { extractHashFromSchema, getSkipDocumentsValidationOption, hasFederationSpec, pickFlag, prioritize, shouldValidateDocumentsAgainstSchema, shouldValidateDuplicateDocuments, } from './utils.js';
|
|
7
|
+
export async function codegen(options) {
|
|
111
8
|
var _a;
|
|
112
9
|
const documents = options.documents || [];
|
|
113
10
|
const profiler = (_a = options.profiler) !== null && _a !== void 0 ? _a : createNoopProfiler();
|
|
@@ -198,7 +95,7 @@ async function codegen(options) {
|
|
|
198
95
|
if (typeof result === 'string') {
|
|
199
96
|
return result || '';
|
|
200
97
|
}
|
|
201
|
-
|
|
98
|
+
if (isComplexPluginOutput(result)) {
|
|
202
99
|
if (result.append && result.append.length > 0) {
|
|
203
100
|
for (const item of result.append) {
|
|
204
101
|
if (item) {
|
|
@@ -225,17 +122,15 @@ function resolveCompareValue(a) {
|
|
|
225
122
|
if (a.startsWith('/*') || a.startsWith('//') || a.startsWith(' *') || a.startsWith(' */') || a.startsWith('*/')) {
|
|
226
123
|
return 0;
|
|
227
124
|
}
|
|
228
|
-
|
|
125
|
+
if (a.startsWith('package')) {
|
|
229
126
|
return 1;
|
|
230
127
|
}
|
|
231
|
-
|
|
128
|
+
if (a.startsWith('import')) {
|
|
232
129
|
return 2;
|
|
233
130
|
}
|
|
234
|
-
|
|
235
|
-
return 3;
|
|
236
|
-
}
|
|
131
|
+
return 3;
|
|
237
132
|
}
|
|
238
|
-
function sortPrependValues(values) {
|
|
133
|
+
export function sortPrependValues(values) {
|
|
239
134
|
return values.sort((a, b) => {
|
|
240
135
|
const aV = resolveCompareValue(a);
|
|
241
136
|
const bV = resolveCompareValue(b);
|
|
@@ -313,5 +208,3 @@ function validateDuplicateDocuments(files) {
|
|
|
313
208
|
}
|
|
314
209
|
});
|
|
315
210
|
}
|
|
316
|
-
|
|
317
|
-
export { codegen, executePlugin };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { DetailedError, createNoopProfiler } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import { buildASTSchema } from 'graphql';
|
|
3
|
+
export async function executePlugin(options, plugin) {
|
|
4
|
+
var _a;
|
|
5
|
+
if (!plugin || !plugin.plugin || typeof plugin.plugin !== 'function') {
|
|
6
|
+
throw new DetailedError(`Invalid Custom Plugin "${options.name}"`, `
|
|
7
|
+
Plugin ${options.name} does not export a valid JS object with "plugin" function.
|
|
8
|
+
|
|
9
|
+
Make sure your custom plugin is written in the following form:
|
|
10
|
+
|
|
11
|
+
module.exports = {
|
|
12
|
+
plugin: (schema, documents, config) => {
|
|
13
|
+
return 'my-custom-plugin-content';
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
`);
|
|
17
|
+
}
|
|
18
|
+
const outputSchema = options.schemaAst || buildASTSchema(options.schema, options.config);
|
|
19
|
+
const documents = options.documents || [];
|
|
20
|
+
const pluginContext = options.pluginContext || {};
|
|
21
|
+
const profiler = (_a = options.profiler) !== null && _a !== void 0 ? _a : createNoopProfiler();
|
|
22
|
+
if (plugin.validate && typeof plugin.validate === 'function') {
|
|
23
|
+
try {
|
|
24
|
+
// FIXME: Sync validate signature with plugin signature
|
|
25
|
+
await profiler.run(async () => plugin.validate(outputSchema, documents, options.config, options.outputFilename, options.allPlugins, pluginContext), `Plugin ${options.name} validate`);
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
throw new DetailedError(`Plugin "${options.name}" validation failed:`, `
|
|
29
|
+
${e.message}
|
|
30
|
+
`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return profiler.run(() => Promise.resolve(plugin.plugin(outputSchema, documents, typeof options.config === 'object' ? { ...options.config } : options.config, {
|
|
34
|
+
outputFile: options.outputFilename,
|
|
35
|
+
allPlugins: options.allPlugins,
|
|
36
|
+
pluginContext,
|
|
37
|
+
})), `Plugin ${options.name} execution`);
|
|
38
|
+
}
|
package/esm/index.js
ADDED
package/esm/utils.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { isDocumentNode } from '@graphql-tools/utils';
|
|
2
|
+
import { isSchema, Kind } from 'graphql';
|
|
3
|
+
export function isObjectMap(obj) {
|
|
4
|
+
return obj && typeof obj === 'object' && !Array.isArray(obj);
|
|
5
|
+
}
|
|
6
|
+
export function prioritize(...values) {
|
|
7
|
+
const picked = values.find(val => typeof val === 'boolean');
|
|
8
|
+
if (typeof picked !== 'boolean') {
|
|
9
|
+
return values[values.length - 1];
|
|
10
|
+
}
|
|
11
|
+
return picked;
|
|
12
|
+
}
|
|
13
|
+
export function pickFlag(flag, config) {
|
|
14
|
+
return isObjectMap(config) ? config[flag] : undefined;
|
|
15
|
+
}
|
|
16
|
+
export function shouldValidateDuplicateDocuments(skipDocumentsValidationOption) {
|
|
17
|
+
// If the value is true, skip all
|
|
18
|
+
if (skipDocumentsValidationOption === true) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
// If the value is object with the specific flag, only skip this one
|
|
22
|
+
if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipDuplicateValidation) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
// If the value is falsy or the specific flag is not set, validate
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
export function shouldValidateDocumentsAgainstSchema(skipDocumentsValidationOption) {
|
|
29
|
+
// If the value is true, skip all
|
|
30
|
+
if (skipDocumentsValidationOption === true) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
// If the value is object with the specific flag, only skip this one
|
|
34
|
+
if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipValidationAgainstSchema) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
// If the value is falsy or the specific flag is not set, validate
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
export function getSkipDocumentsValidationOption(options) {
|
|
41
|
+
// If the value is set on the root level
|
|
42
|
+
if (options.skipDocumentsValidation) {
|
|
43
|
+
return options.skipDocumentsValidation;
|
|
44
|
+
}
|
|
45
|
+
// If the value is set under `config` property
|
|
46
|
+
const flagFromConfig = pickFlag('skipDocumentsValidation', options.config);
|
|
47
|
+
if (flagFromConfig) {
|
|
48
|
+
return flagFromConfig;
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
const federationDirectives = ['key', 'requires', 'provides', 'external'];
|
|
53
|
+
export function hasFederationSpec(schemaOrAST) {
|
|
54
|
+
if (isSchema(schemaOrAST)) {
|
|
55
|
+
return federationDirectives.some(directive => schemaOrAST.getDirective(directive));
|
|
56
|
+
}
|
|
57
|
+
if (isDocumentNode(schemaOrAST)) {
|
|
58
|
+
return schemaOrAST.definitions.some(def => def.kind === Kind.DIRECTIVE_DEFINITION && federationDirectives.includes(def.name.value));
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
export function extractHashFromSchema(schema) {
|
|
63
|
+
var _a;
|
|
64
|
+
if (!schema.extensions) {
|
|
65
|
+
schema.extensions = {};
|
|
66
|
+
}
|
|
67
|
+
return (_a = schema.extensions['hash']) !== null && _a !== void 0 ? _a : null;
|
|
68
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/core",
|
|
3
|
-
"version": "2.6.0-alpha-
|
|
3
|
+
"version": "2.6.0-alpha-bd464a586.0",
|
|
4
4
|
"peerDependencies": {
|
|
5
5
|
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@graphql-codegen/plugin-helpers": "2.5.0-alpha-
|
|
9
|
-
"@graphql-tools/schema": "^8.
|
|
10
|
-
"@graphql-tools/utils": "^8.
|
|
11
|
-
"tslib": "~2.
|
|
8
|
+
"@graphql-codegen/plugin-helpers": "^2.5.0-alpha-bd464a586.0",
|
|
9
|
+
"@graphql-tools/schema": "^8.5.0",
|
|
10
|
+
"@graphql-tools/utils": "^8.8.0",
|
|
11
|
+
"tslib": "~2.4.0"
|
|
12
12
|
},
|
|
13
13
|
"repository": {
|
|
14
14
|
"type": "git",
|
|
@@ -35,21 +35,28 @@
|
|
|
35
35
|
],
|
|
36
36
|
"author": "Dotan Simha <dotansimha@gmail.com>",
|
|
37
37
|
"license": "MIT",
|
|
38
|
-
"main": "index.js",
|
|
39
|
-
"module": "index.
|
|
40
|
-
"typings": "index.d.ts",
|
|
38
|
+
"main": "cjs/index.js",
|
|
39
|
+
"module": "esm/index.js",
|
|
40
|
+
"typings": "typings/index.d.ts",
|
|
41
41
|
"typescript": {
|
|
42
|
-
"definition": "index.d.ts"
|
|
42
|
+
"definition": "typings/index.d.ts"
|
|
43
43
|
},
|
|
44
|
+
"type": "module",
|
|
44
45
|
"exports": {
|
|
45
|
-
"./package.json": "./package.json",
|
|
46
46
|
".": {
|
|
47
|
-
"require":
|
|
48
|
-
|
|
47
|
+
"require": {
|
|
48
|
+
"types": "./typings/index.d.ts",
|
|
49
|
+
"default": "./cjs/index.js"
|
|
50
|
+
},
|
|
51
|
+
"import": {
|
|
52
|
+
"types": "./typings/index.d.ts",
|
|
53
|
+
"default": "./esm/index.js"
|
|
54
|
+
},
|
|
55
|
+
"default": {
|
|
56
|
+
"types": "./typings/index.d.ts",
|
|
57
|
+
"default": "./esm/index.js"
|
|
58
|
+
}
|
|
49
59
|
},
|
|
50
|
-
"
|
|
51
|
-
"require": "./*.js",
|
|
52
|
-
"import": "./*.mjs"
|
|
53
|
-
}
|
|
60
|
+
"./package.json": "./package.json"
|
|
54
61
|
}
|
|
55
|
-
}
|
|
62
|
+
}
|
|
File without changes
|
|
File without changes
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { codegen } from './codegen';
|
|
2
|
-
export { executePlugin, ExecutePluginOptions } from './execute-plugin';
|
|
1
|
+
export { codegen } from './codegen.js';
|
|
2
|
+
export { executePlugin, ExecutePluginOptions } from './execute-plugin.js';
|
|
File without changes
|