@depup/graphql-codegen__typescript-operations 5.0.9-depup.0 → 6.1.1-depup.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/LICENSE +21 -0
- package/README.md +5 -6
- package/changes.json +4 -8
- package/cjs/index.js +65 -28
- package/cjs/ts-operation-variables-to-object.js +47 -2
- package/cjs/ts-selection-set-processor.js +4 -10
- package/cjs/visitor.js +502 -31
- package/esm/index.js +48 -21
- package/esm/ts-operation-variables-to-object.js +47 -2
- package/esm/ts-selection-set-processor.js +4 -10
- package/esm/visitor.js +503 -32
- package/package.json +17 -19
- package/typings/config.d.cts +184 -89
- package/typings/config.d.ts +184 -89
- package/typings/index.d.cts +1 -2
- package/typings/index.d.ts +1 -2
- package/typings/ts-operation-variables-to-object.d.cts +16 -2
- package/typings/ts-operation-variables-to-object.d.ts +16 -2
- package/typings/ts-selection-set-processor.d.cts +1 -1
- package/typings/ts-selection-set-processor.d.ts +1 -1
- package/typings/visitor.d.cts +50 -5
- package/typings/visitor.d.ts +50 -5
package/cjs/visitor.js
CHANGED
|
@@ -2,76 +2,547 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TypeScriptDocumentsVisitor = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
6
5
|
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
7
6
|
const graphql_1 = require("graphql");
|
|
7
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
8
|
+
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
8
9
|
const ts_operation_variables_to_object_js_1 = require("./ts-operation-variables-to-object.js");
|
|
9
|
-
const ts_selection_set_processor_js_1 = require("./ts-selection-set-processor.js");
|
|
10
10
|
class TypeScriptDocumentsVisitor extends visitor_plugin_common_1.BaseDocumentsVisitor {
|
|
11
|
-
|
|
11
|
+
_usedSchemaTypes = {};
|
|
12
|
+
_needsExactUtilityType = false;
|
|
13
|
+
_outputPath;
|
|
14
|
+
constructor(schema, config, documentNode, outputPath) {
|
|
12
15
|
super(config, {
|
|
13
16
|
arrayInputCoercion: (0, visitor_plugin_common_1.getConfigValue)(config.arrayInputCoercion, true),
|
|
14
17
|
noExport: (0, visitor_plugin_common_1.getConfigValue)(config.noExport, false),
|
|
15
|
-
avoidOptionals: (0, visitor_plugin_common_1.normalizeAvoidOptionals)((0, visitor_plugin_common_1.getConfigValue)(config.avoidOptionals, false)),
|
|
16
18
|
immutableTypes: (0, visitor_plugin_common_1.getConfigValue)(config.immutableTypes, false),
|
|
17
19
|
nonOptionalTypename: (0, visitor_plugin_common_1.getConfigValue)(config.nonOptionalTypename, false),
|
|
18
|
-
preResolveTypes: (0, visitor_plugin_common_1.getConfigValue)(config.preResolveTypes, true),
|
|
19
20
|
mergeFragmentTypes: (0, visitor_plugin_common_1.getConfigValue)(config.mergeFragmentTypes, false),
|
|
20
21
|
allowUndefinedQueryVariables: (0, visitor_plugin_common_1.getConfigValue)(config.allowUndefinedQueryVariables, false),
|
|
22
|
+
enumType: (0, visitor_plugin_common_1.getConfigValue)(config.enumType, 'string-literal'),
|
|
23
|
+
ignoreEnumValuesFromSchema: (0, visitor_plugin_common_1.getConfigValue)(config.ignoreEnumValuesFromSchema, false),
|
|
24
|
+
futureProofEnums: (0, visitor_plugin_common_1.getConfigValue)(config.futureProofEnums, false),
|
|
25
|
+
maybeValue: (0, visitor_plugin_common_1.getConfigValue)(config.maybeValue, 'T | null'),
|
|
26
|
+
inputMaybeValue: (0, visitor_plugin_common_1.getConfigValue)(config.inputMaybeValue, 'T | null | undefined'),
|
|
21
27
|
}, schema);
|
|
28
|
+
this.config.enumValues = (0, visitor_plugin_common_1.parseEnumValues)({
|
|
29
|
+
schema,
|
|
30
|
+
mapOrStr: config.enumValues,
|
|
31
|
+
ignoreEnumValuesFromSchema: config.ignoreEnumValuesFromSchema,
|
|
32
|
+
naming: {
|
|
33
|
+
convert: this.config.convert,
|
|
34
|
+
options: {
|
|
35
|
+
typesPrefix: this.config.typesPrefix,
|
|
36
|
+
typesSuffix: this.config.typesSuffix,
|
|
37
|
+
useTypesPrefix: this.config.enumPrefix,
|
|
38
|
+
useTypesSuffix: this.config.enumSuffix,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
this._outputPath = outputPath;
|
|
22
43
|
(0, auto_bind_1.default)(this);
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return (this.config.immutableTypes ? `readonly ${name}` : name) + (optional ? '?' : '');
|
|
44
|
+
const allFragments = [
|
|
45
|
+
...documentNode.definitions.filter(d => d.kind === graphql_1.Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
|
46
|
+
node: fragmentDef,
|
|
47
|
+
name: fragmentDef.name.value,
|
|
48
|
+
onType: fragmentDef.typeCondition.name.value,
|
|
49
|
+
isExternal: false,
|
|
50
|
+
})),
|
|
51
|
+
...(config.externalFragments || []),
|
|
52
|
+
];
|
|
53
|
+
// Create a combined document that includes operations, internal and external fragments for enum collection
|
|
54
|
+
const documentWithAllFragments = {
|
|
55
|
+
...documentNode,
|
|
56
|
+
definitions: [
|
|
57
|
+
...documentNode.definitions.filter(d => d.kind !== graphql_1.Kind.FRAGMENT_DEFINITION),
|
|
58
|
+
...allFragments.map(f => f.node),
|
|
59
|
+
],
|
|
40
60
|
};
|
|
61
|
+
this._usedSchemaTypes = this.collectUsedSchemaTypesToGenerate({
|
|
62
|
+
schema,
|
|
63
|
+
documentNode: documentWithAllFragments,
|
|
64
|
+
});
|
|
41
65
|
const processorConfig = {
|
|
42
66
|
namespacedImportName: this.config.namespacedImportName,
|
|
43
67
|
convertName: this.convertName.bind(this),
|
|
44
68
|
enumPrefix: this.config.enumPrefix,
|
|
45
69
|
enumSuffix: this.config.enumSuffix,
|
|
46
70
|
scalars: this.scalars,
|
|
47
|
-
formatNamedField,
|
|
48
|
-
|
|
49
|
-
|
|
71
|
+
formatNamedField: ({ name, isOptional }) => {
|
|
72
|
+
return (this.config.immutableTypes ? `readonly ${name}` : name) + (isOptional ? '?' : '');
|
|
73
|
+
},
|
|
74
|
+
wrapTypeWithModifiers: (baseType, type) => {
|
|
75
|
+
return (0, visitor_plugin_common_1.wrapTypeWithModifiers)(baseType, type, {
|
|
76
|
+
wrapOptional: type => (0, visitor_plugin_common_1.printTypeScriptMaybeType)({
|
|
77
|
+
type,
|
|
78
|
+
pattern: this.config.maybeValue,
|
|
79
|
+
}),
|
|
80
|
+
wrapArray: type => {
|
|
81
|
+
const listModifier = this.config.immutableTypes ? 'ReadonlyArray' : 'Array';
|
|
82
|
+
return `${listModifier}<${type}>`;
|
|
83
|
+
},
|
|
84
|
+
});
|
|
50
85
|
},
|
|
51
|
-
avoidOptionals: this.config.avoidOptionals,
|
|
52
86
|
printFieldsOnNewLines: this.config.printFieldsOnNewLines,
|
|
53
87
|
};
|
|
54
|
-
|
|
55
|
-
this.setSelectionSetHandler(new visitor_plugin_common_1.SelectionSetToObject(processor, this.scalars, this.schema, this.convertName.bind(this), this.getFragmentSuffix.bind(this), allFragments, this.config));
|
|
88
|
+
this.setSelectionSetHandler(new visitor_plugin_common_1.SelectionSetToObject(new visitor_plugin_common_1.PreResolveTypesProcessor(processorConfig), this.scalars, this.schema, this.convertName.bind(this), this.getFragmentSuffix.bind(this), allFragments, this.config));
|
|
56
89
|
const enumsNames = Object.keys(schema.getTypeMap()).filter(typeName => (0, graphql_1.isEnumType)(schema.getType(typeName)));
|
|
57
|
-
this.setVariablesTransformer(new ts_operation_variables_to_object_js_1.TypeScriptOperationVariablesToObject(
|
|
90
|
+
this.setVariablesTransformer(new ts_operation_variables_to_object_js_1.TypeScriptOperationVariablesToObject({
|
|
91
|
+
avoidOptionals: this.config.avoidOptionals,
|
|
92
|
+
immutableTypes: this.config.immutableTypes,
|
|
93
|
+
inputMaybeValue: this.config.inputMaybeValue,
|
|
94
|
+
}, this.scalars, this.convertName.bind(this), this.config.namespacedImportName, enumsNames, this.config.enumPrefix, this.config.enumSuffix, this.config.enumValues, this.config.arrayInputCoercion));
|
|
58
95
|
this._declarationBlockConfig = {
|
|
59
96
|
ignoreExport: this.config.noExport,
|
|
97
|
+
enumNameValueSeparator: ' =',
|
|
60
98
|
};
|
|
61
99
|
}
|
|
100
|
+
EnumTypeDefinition(node) {
|
|
101
|
+
const enumName = node.name.value;
|
|
102
|
+
if (!this._usedSchemaTypes[enumName] || // If not used...
|
|
103
|
+
this.config.importSchemaTypesFrom // ... Or, is imported from a shared file
|
|
104
|
+
) {
|
|
105
|
+
return null; // ... then, don't generate in this file
|
|
106
|
+
}
|
|
107
|
+
return (0, visitor_plugin_common_1.convertSchemaEnumToDeclarationBlockString)({
|
|
108
|
+
schema: this._schema,
|
|
109
|
+
node,
|
|
110
|
+
declarationBlockConfig: this._declarationBlockConfig,
|
|
111
|
+
enumName,
|
|
112
|
+
enumValues: this.config.enumValues,
|
|
113
|
+
futureProofEnums: this.config.futureProofEnums,
|
|
114
|
+
ignoreEnumValuesFromSchema: this.config.ignoreEnumValuesFromSchema,
|
|
115
|
+
outputType: this.config.enumType,
|
|
116
|
+
naming: {
|
|
117
|
+
convert: this.config.convert,
|
|
118
|
+
options: {
|
|
119
|
+
typesPrefix: this.config.typesPrefix,
|
|
120
|
+
typesSuffix: this.config.typesSuffix,
|
|
121
|
+
useTypesPrefix: this.config.enumPrefix,
|
|
122
|
+
useTypesSuffix: this.config.enumSuffix,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
InputObjectTypeDefinition(node) {
|
|
128
|
+
const inputTypeName = node.name.value;
|
|
129
|
+
if (!this._usedSchemaTypes[inputTypeName] || // If not used...
|
|
130
|
+
this.config.importSchemaTypesFrom // ... Or, is imported from a shared file
|
|
131
|
+
) {
|
|
132
|
+
return null; // ... then, don't generate in this file
|
|
133
|
+
}
|
|
134
|
+
// Note: we usually don't need to export this type,
|
|
135
|
+
// however, it's not possible to know if another file is using this type e.g. using `importSchemaTypesFrom`,
|
|
136
|
+
// so it's better export the types.
|
|
137
|
+
if ((0, visitor_plugin_common_1.isOneOfInputObjectType)(this._schema.getType(inputTypeName))) {
|
|
138
|
+
return new visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
|
|
139
|
+
.export()
|
|
140
|
+
.asKind(this.config.declarationKind.input)
|
|
141
|
+
.withName(this.convertName(node))
|
|
142
|
+
.withComment(node.description?.value)
|
|
143
|
+
.withContent(`\n` + (node.fields || []).join('\n |')).string;
|
|
144
|
+
}
|
|
145
|
+
return new visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
|
|
146
|
+
.export()
|
|
147
|
+
.asKind(this.config.declarationKind.input)
|
|
148
|
+
.withName(this.convertName(node))
|
|
149
|
+
.withComment(node.description?.value)
|
|
150
|
+
.withBlock((node.fields || []).join('\n')).string;
|
|
151
|
+
}
|
|
152
|
+
InputValueDefinition(node, _key, _parent, _path, ancestors) {
|
|
153
|
+
const oneOfDetails = parseOneOfInputValue({
|
|
154
|
+
node,
|
|
155
|
+
schema: this._schema,
|
|
156
|
+
ancestors,
|
|
157
|
+
});
|
|
158
|
+
// 1. Flatten GraphQL type nodes to make it easier to turn into string
|
|
159
|
+
// GraphQL type nodes may have `NonNullType` type before each `ListType` or `NamedType`
|
|
160
|
+
// This make it a bit harder to know whether a `ListType` or `Namedtype` is nullable without looking at the node before it.
|
|
161
|
+
// Flattening it into an array where the nullability is in `ListType` and `NamedType` makes it easier to code,
|
|
162
|
+
//
|
|
163
|
+
// So, we recursively call `collectAndFlattenTypeNodes` to handle the following scenarios:
|
|
164
|
+
// - [Thing]
|
|
165
|
+
// - [Thing!]
|
|
166
|
+
// - [Thing]!
|
|
167
|
+
// - [Thing!]!
|
|
168
|
+
const typeNodes = [];
|
|
169
|
+
collectAndFlattenTypeNodes({
|
|
170
|
+
currentTypeNode: node.type,
|
|
171
|
+
isPreviousNodeNonNullable: oneOfDetails.isOneOfInputValue, // If the InputValue is part of @oneOf input, we treat it as non-null (even if it must be null in the schema)
|
|
172
|
+
typeNodes,
|
|
173
|
+
});
|
|
174
|
+
// 2. Generate the type of a TypeScript field declaration
|
|
175
|
+
// e.g. `field?: string`, then the `string` is the `typePart`
|
|
176
|
+
let typePart = '';
|
|
177
|
+
// We call `.reverse()` here to get the base type node first
|
|
178
|
+
for (const typeNode of typeNodes.reverse()) {
|
|
179
|
+
if (typeNode.type === 'NamedType') {
|
|
180
|
+
const usedInputType = this._usedSchemaTypes[typeNode.name];
|
|
181
|
+
if (!usedInputType) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
typePart = usedInputType.tsType; // If the schema is correct, when reversing typeNodes, the first node would be `NamedType`, which means we can safely set it as the base for typePart
|
|
185
|
+
if (!typeNode.isNonNullable) {
|
|
186
|
+
typePart = (0, visitor_plugin_common_1.printTypeScriptMaybeType)({
|
|
187
|
+
type: typePart,
|
|
188
|
+
pattern: this.config.inputMaybeValue,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (typeNode.type === 'ListType') {
|
|
194
|
+
const listModifier = this.config.immutableTypes ? 'ReadonlyArray' : 'Array';
|
|
195
|
+
typePart = `${listModifier}<${typePart}>`;
|
|
196
|
+
if (!typeNode.isNonNullable) {
|
|
197
|
+
typePart = (0, visitor_plugin_common_1.printTypeScriptMaybeType)({
|
|
198
|
+
type: typePart,
|
|
199
|
+
pattern: this.config.inputMaybeValue,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const addOptionalSign = !oneOfDetails.isOneOfInputValue &&
|
|
205
|
+
!this.config.avoidOptionals.inputValue &&
|
|
206
|
+
(node.type.kind !== graphql_1.Kind.NON_NULL_TYPE ||
|
|
207
|
+
(!this.config.avoidOptionals.defaultValue && node.defaultValue !== undefined));
|
|
208
|
+
// 3. Generate the keyPart of the TypeScript field declaration
|
|
209
|
+
// e.g. `field?: string`, then the `field?` is the `keyPart`
|
|
210
|
+
const keyPart = `${node.name.value}${addOptionalSign ? '?' : ''}`;
|
|
211
|
+
// 4. other parts of TypeScript field declaration
|
|
212
|
+
const commentPart = (0, visitor_plugin_common_1.getNodeComment)(node);
|
|
213
|
+
const readonlyPart = this.config.immutableTypes ? 'readonly ' : '';
|
|
214
|
+
const currentInputValue = commentPart + (0, visitor_plugin_common_1.indent)(`${readonlyPart}${keyPart}: ${typePart};`);
|
|
215
|
+
// 5. Check if field is part of `@oneOf` input type
|
|
216
|
+
// If yes, we must generate a union member where the current inputValue must be provieded, and the others are not
|
|
217
|
+
// e.g.
|
|
218
|
+
// ```graphql
|
|
219
|
+
// input UserInput {
|
|
220
|
+
// byId: ID
|
|
221
|
+
// byEmail: String
|
|
222
|
+
// byLegacyId: ID
|
|
223
|
+
// }
|
|
224
|
+
// ```
|
|
225
|
+
//
|
|
226
|
+
// Then, the generated type is:
|
|
227
|
+
// ```ts
|
|
228
|
+
// type UserInput =
|
|
229
|
+
// | { byId: string | number; byEmail?: never; byLegacyId?: never }
|
|
230
|
+
// | { byId?: never; byEmail: string; byLegacyId?: never }
|
|
231
|
+
// | { byId?: never; byEmail?: never; byLegacyId: string | number }
|
|
232
|
+
// ```
|
|
233
|
+
if (oneOfDetails.isOneOfInputValue) {
|
|
234
|
+
const fieldParts = [];
|
|
235
|
+
for (const fieldName of Object.keys(oneOfDetails.parentType.getFields())) {
|
|
236
|
+
if (fieldName === node.name.value) {
|
|
237
|
+
fieldParts.push(currentInputValue);
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
fieldParts.push(`${readonlyPart}${fieldName}?: never;`);
|
|
241
|
+
}
|
|
242
|
+
return (0, visitor_plugin_common_1.indent)(`{ ${fieldParts.join(' ')} }`);
|
|
243
|
+
}
|
|
244
|
+
// If field is not part of @oneOf input type, then it's a input value, just return as-is
|
|
245
|
+
return currentInputValue;
|
|
246
|
+
}
|
|
62
247
|
getImports() {
|
|
63
248
|
return !this.config.globalNamespace &&
|
|
64
249
|
(this.config.inlineFragmentTypes === 'combine' || this.config.inlineFragmentTypes === 'mask')
|
|
65
250
|
? this.config.fragmentImports.map(fragmentImport => (0, visitor_plugin_common_1.generateFragmentImportStatement)(fragmentImport, 'type'))
|
|
66
251
|
: [];
|
|
67
252
|
}
|
|
253
|
+
getExternalSchemaTypeImports() {
|
|
254
|
+
if (!this.config.importSchemaTypesFrom) {
|
|
255
|
+
return [];
|
|
256
|
+
}
|
|
257
|
+
const hasTypesToImport = Object.values(this._usedSchemaTypes).filter(value => value.type === 'GraphQLEnumType' || value.type === 'GraphQLInputObjectType').length > 0;
|
|
258
|
+
if (!hasTypesToImport) {
|
|
259
|
+
return [];
|
|
260
|
+
}
|
|
261
|
+
return [
|
|
262
|
+
(0, visitor_plugin_common_1.generateImportStatement)({
|
|
263
|
+
baseDir: process.cwd(),
|
|
264
|
+
baseOutputDir: '',
|
|
265
|
+
outputPath: this._outputPath,
|
|
266
|
+
importSource: {
|
|
267
|
+
path: this.config.importSchemaTypesFrom,
|
|
268
|
+
namespace: this.config.namespacedImportName,
|
|
269
|
+
identifiers: [],
|
|
270
|
+
},
|
|
271
|
+
typesImport: true,
|
|
272
|
+
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
|
273
|
+
importExtension: (0, plugin_helpers_1.normalizeImportExtension)({
|
|
274
|
+
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
|
275
|
+
importExtension: this.config.importExtension,
|
|
276
|
+
}),
|
|
277
|
+
}),
|
|
278
|
+
];
|
|
279
|
+
}
|
|
280
|
+
getEnumsImports() {
|
|
281
|
+
const usedEnumMap = {};
|
|
282
|
+
for (const [enumName, enumDetails] of Object.entries(this.config.enumValues)) {
|
|
283
|
+
if (this._usedSchemaTypes[enumName]) {
|
|
284
|
+
usedEnumMap[enumName] = enumDetails;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return (0, visitor_plugin_common_1.getEnumsImports)({
|
|
288
|
+
enumValues: usedEnumMap,
|
|
289
|
+
useTypeImports: this.config.useTypeImports,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
getScalarsImports() {
|
|
293
|
+
const fileType = this.config.importSchemaTypesFrom
|
|
294
|
+
? 'multi-file-operation-file'
|
|
295
|
+
: this.config.generateOperationTypes
|
|
296
|
+
? 'single-file-operation-file'
|
|
297
|
+
: 'multi-file-shared-type-file';
|
|
298
|
+
const imports = {};
|
|
299
|
+
for (const [scalarName, parsedScalar] of Object.entries(this.config.scalars)) {
|
|
300
|
+
const usedScalar = this._usedSchemaTypes[scalarName];
|
|
301
|
+
if (!usedScalar || usedScalar.type !== 'GraphQLScalarType') {
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (parsedScalar.input.isExternal &&
|
|
305
|
+
(((usedScalar.useCases.input || usedScalar.useCases.variables) &&
|
|
306
|
+
fileType === 'single-file-operation-file') ||
|
|
307
|
+
(usedScalar.useCases.input && fileType === 'multi-file-shared-type-file') ||
|
|
308
|
+
(usedScalar.useCases.variables && fileType === 'multi-file-operation-file'))) {
|
|
309
|
+
imports[parsedScalar.input.source] ||= { identifiers: {} };
|
|
310
|
+
imports[parsedScalar.input.source].identifiers[parsedScalar.input.import] = {
|
|
311
|
+
asDefault: parsedScalar.input.default,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
if (parsedScalar.output.isExternal &&
|
|
315
|
+
usedScalar.useCases.output &&
|
|
316
|
+
(fileType === 'single-file-operation-file' || fileType === 'multi-file-operation-file')) {
|
|
317
|
+
imports[parsedScalar.output.source] ||= { identifiers: {} };
|
|
318
|
+
imports[parsedScalar.output.source].identifiers[parsedScalar.output.import] = {
|
|
319
|
+
asDefault: parsedScalar.output.default,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return Object.entries(imports).reduce((res, [importSource, importParams]) => {
|
|
324
|
+
// One import statement cannot have multiple defaults.
|
|
325
|
+
// So:
|
|
326
|
+
// - split each defaults into its own statements
|
|
327
|
+
// - the named imports can all go together, tracked by `namedImports`
|
|
328
|
+
const namedImports = [];
|
|
329
|
+
for (const [identifier, identifierMetadata] of Object.entries(importParams.identifiers)) {
|
|
330
|
+
if (identifierMetadata.asDefault) {
|
|
331
|
+
res.push((0, visitor_plugin_common_1.buildTypeImport)({
|
|
332
|
+
identifier,
|
|
333
|
+
source: importSource,
|
|
334
|
+
asDefault: true,
|
|
335
|
+
useTypeImports: this.config.useTypeImports,
|
|
336
|
+
}));
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
namedImports.push(identifier);
|
|
340
|
+
}
|
|
341
|
+
if (namedImports.length > 0) {
|
|
342
|
+
res.push((0, visitor_plugin_common_1.buildTypeImport)({
|
|
343
|
+
identifier: namedImports.join(', '),
|
|
344
|
+
source: importSource,
|
|
345
|
+
asDefault: false,
|
|
346
|
+
useTypeImports: this.config.useTypeImports,
|
|
347
|
+
}));
|
|
348
|
+
}
|
|
349
|
+
return res;
|
|
350
|
+
}, []);
|
|
351
|
+
}
|
|
68
352
|
getPunctuation(_declarationKind) {
|
|
69
353
|
return ';';
|
|
70
354
|
}
|
|
71
355
|
applyVariablesWrapper(variablesBlock, operationType) {
|
|
72
|
-
const prefix = this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '';
|
|
73
356
|
const extraType = this.config.allowUndefinedQueryVariables && operationType === 'Query' ? ' | undefined' : '';
|
|
74
|
-
|
|
357
|
+
this._needsExactUtilityType = true;
|
|
358
|
+
return `Exact<${variablesBlock === '{}' ? `{ [key: string]: never; }` : variablesBlock}>${extraType}`;
|
|
359
|
+
}
|
|
360
|
+
collectInnerTypesRecursively({ node, usedSchemaTypes, location, }) {
|
|
361
|
+
if (node instanceof graphql_1.GraphQLEnumType) {
|
|
362
|
+
if (usedSchemaTypes[node.name]) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
usedSchemaTypes[node.name] = {
|
|
366
|
+
type: 'GraphQLEnumType',
|
|
367
|
+
node,
|
|
368
|
+
tsType: this.convertName(node.name),
|
|
369
|
+
};
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
if (node instanceof graphql_1.GraphQLScalarType) {
|
|
373
|
+
const scalarType = usedSchemaTypes[node.name] || {
|
|
374
|
+
type: 'GraphQLScalarType',
|
|
375
|
+
node,
|
|
376
|
+
tsType: this.config.scalars?.[node.name]?.input.type ??
|
|
377
|
+
visitor_plugin_common_1.DEFAULT_INPUT_SCALARS[node.name]?.input ??
|
|
378
|
+
'unknown',
|
|
379
|
+
useCases: {
|
|
380
|
+
variables: location === 'variables',
|
|
381
|
+
input: location === 'input',
|
|
382
|
+
output: false,
|
|
383
|
+
},
|
|
384
|
+
};
|
|
385
|
+
if (scalarType.type !== 'GraphQLScalarType') {
|
|
386
|
+
throw new Error(`${node.name} has been incorrectly parsed as Scalar. This should not happen.`);
|
|
387
|
+
}
|
|
388
|
+
// ensure scalar's useCases is updated to have `useCases.input:true` or `useCases.variables:true`, depending on the use case
|
|
389
|
+
// this is required because if the scalar has been parsed previously, it may only have `useCases.output:true`, and not `useCases.input:true` or `useCases.variables:true`
|
|
390
|
+
if (location === 'input') {
|
|
391
|
+
scalarType.useCases.input = true;
|
|
392
|
+
}
|
|
393
|
+
if (location === 'variables') {
|
|
394
|
+
scalarType.useCases.variables = true;
|
|
395
|
+
}
|
|
396
|
+
usedSchemaTypes[node.name] = scalarType;
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
// GraphQLInputObjectType
|
|
400
|
+
if (usedSchemaTypes[node.name]) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
usedSchemaTypes[node.name] = {
|
|
404
|
+
type: 'GraphQLInputObjectType',
|
|
405
|
+
node,
|
|
406
|
+
tsType: this.convertName(node.name),
|
|
407
|
+
};
|
|
408
|
+
const fields = node.getFields();
|
|
409
|
+
for (const field of Object.values(fields)) {
|
|
410
|
+
const fieldType = (0, graphql_1.getNamedType)(field.type);
|
|
411
|
+
this.collectInnerTypesRecursively({
|
|
412
|
+
node: fieldType,
|
|
413
|
+
usedSchemaTypes,
|
|
414
|
+
location: 'input',
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* @description collects schema types used in operations:
|
|
420
|
+
* - used Enums for Variables
|
|
421
|
+
* - used Scalars for Variables
|
|
422
|
+
* - used Input for Variables (recursively)
|
|
423
|
+
*
|
|
424
|
+
* - used Enums for Result
|
|
425
|
+
* - used Scalars for Result
|
|
426
|
+
*/
|
|
427
|
+
collectUsedSchemaTypesToGenerate({ schema, documentNode, }) {
|
|
428
|
+
const schemaTypes = schema.getTypeMap();
|
|
429
|
+
const usedSchemaTypes = {};
|
|
430
|
+
// Collect input enums and input types
|
|
431
|
+
(0, graphql_1.visit)(documentNode, {
|
|
432
|
+
VariableDefinition: variableDefinitionNode => {
|
|
433
|
+
(0, graphql_1.visit)(variableDefinitionNode, {
|
|
434
|
+
NamedType: namedTypeNode => {
|
|
435
|
+
const foundInputType = schemaTypes[namedTypeNode.name.value];
|
|
436
|
+
if (foundInputType &&
|
|
437
|
+
(foundInputType instanceof graphql_1.GraphQLInputObjectType ||
|
|
438
|
+
foundInputType instanceof graphql_1.GraphQLScalarType ||
|
|
439
|
+
foundInputType instanceof graphql_1.GraphQLEnumType) &&
|
|
440
|
+
!(0, visitor_plugin_common_1.isNativeNamedType)(foundInputType)) {
|
|
441
|
+
this.collectInnerTypesRecursively({
|
|
442
|
+
node: foundInputType,
|
|
443
|
+
usedSchemaTypes,
|
|
444
|
+
location: 'variables',
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
},
|
|
448
|
+
});
|
|
449
|
+
},
|
|
450
|
+
});
|
|
451
|
+
// Collect output enums
|
|
452
|
+
const typeInfo = new graphql_1.TypeInfo(schema);
|
|
453
|
+
(0, graphql_1.visit)(documentNode,
|
|
454
|
+
// AST doesn’t include field types (they are defined in the schema) - only names.
|
|
455
|
+
// TypeInfo is a stateful helper that tracks typing context while walking the AST
|
|
456
|
+
// visitWithTypeInfo wires that context into a visitor.
|
|
457
|
+
(0, graphql_1.visitWithTypeInfo)(typeInfo, {
|
|
458
|
+
Field: () => {
|
|
459
|
+
const fieldType = typeInfo.getType();
|
|
460
|
+
if (fieldType) {
|
|
461
|
+
const namedType = (0, graphql_1.getNamedType)(fieldType);
|
|
462
|
+
if (namedType instanceof graphql_1.GraphQLEnumType) {
|
|
463
|
+
usedSchemaTypes[namedType.name] = {
|
|
464
|
+
type: 'GraphQLEnumType',
|
|
465
|
+
node: namedType,
|
|
466
|
+
tsType: this.convertName(namedType.name),
|
|
467
|
+
};
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
if (namedType instanceof graphql_1.GraphQLScalarType) {
|
|
471
|
+
const scalarType = usedSchemaTypes[namedType.name] || {
|
|
472
|
+
type: 'GraphQLScalarType',
|
|
473
|
+
node: namedType,
|
|
474
|
+
tsType: this.convertName(namedType.name),
|
|
475
|
+
useCases: { variables: false, input: false, output: true },
|
|
476
|
+
};
|
|
477
|
+
if (scalarType.type !== 'GraphQLScalarType') {
|
|
478
|
+
throw new Error(`${namedType.name} has been incorrectly parsed as Scalar. This should not happen.`);
|
|
479
|
+
}
|
|
480
|
+
// ensure scalar's useCases is updated to have `useCases.output:true`
|
|
481
|
+
// this is required because if the scalar has been parsed previously, it may only have `useCases.input:true` or `useCases.variables:true`, not `useCases.output:true`
|
|
482
|
+
scalarType.useCases.output = true;
|
|
483
|
+
usedSchemaTypes[namedType.name] = scalarType;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
},
|
|
487
|
+
}));
|
|
488
|
+
return usedSchemaTypes;
|
|
489
|
+
}
|
|
490
|
+
getExactUtilityType() {
|
|
491
|
+
if (!this.config.generateOperationTypes || // 1. If we don't generate operation types, definitely do not need `Exact`
|
|
492
|
+
!this._needsExactUtilityType // 2. Even if we generate operation types, we may not need `Exact` if there's no operations in the documents i.e. only fragments found
|
|
493
|
+
) {
|
|
494
|
+
return null;
|
|
495
|
+
}
|
|
496
|
+
return `${internalUtilityTypeWarning}type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };`;
|
|
497
|
+
}
|
|
498
|
+
getIncrementalUtilityType() {
|
|
499
|
+
if (!this.config.generateOperationTypes) {
|
|
500
|
+
return null;
|
|
501
|
+
}
|
|
502
|
+
// Note: `export` here is important for 2 reasons
|
|
503
|
+
// 1. It is not always used in the rest of the file, so this is a safe way to avoid lint rules (in tsconfig or eslint) complaining it's not used in the current file.
|
|
504
|
+
// 2. In Client Preset, it is used by fragment-masking.ts, so it needs `export`
|
|
505
|
+
return `${internalUtilityTypeWarning}export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };`;
|
|
75
506
|
}
|
|
76
507
|
}
|
|
77
508
|
exports.TypeScriptDocumentsVisitor = TypeScriptDocumentsVisitor;
|
|
509
|
+
const internalUtilityTypeWarning = '/** Internal type. DO NOT USE DIRECTLY. */\n';
|
|
510
|
+
function parseOneOfInputValue({ node, schema, ancestors, }) {
|
|
511
|
+
const realParentDef = ancestors?.[ancestors.length - 1];
|
|
512
|
+
if (realParentDef) {
|
|
513
|
+
const parentType = schema.getType(realParentDef.name.value);
|
|
514
|
+
if ((0, visitor_plugin_common_1.isOneOfInputObjectType)(parentType)) {
|
|
515
|
+
if (node.type.kind === graphql_1.Kind.NON_NULL_TYPE) {
|
|
516
|
+
throw new Error('Fields on an input object type can not be non-nullable. It seems like the schema was not validated.');
|
|
517
|
+
}
|
|
518
|
+
return { isOneOfInputValue: true, realParentDef, parentType };
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return { isOneOfInputValue: false };
|
|
522
|
+
}
|
|
523
|
+
function collectAndFlattenTypeNodes({ currentTypeNode, isPreviousNodeNonNullable, typeNodes, }) {
|
|
524
|
+
if (currentTypeNode.kind === graphql_1.Kind.NON_NULL_TYPE) {
|
|
525
|
+
const nextTypeNode = currentTypeNode.type;
|
|
526
|
+
collectAndFlattenTypeNodes({
|
|
527
|
+
currentTypeNode: nextTypeNode,
|
|
528
|
+
isPreviousNodeNonNullable: true,
|
|
529
|
+
typeNodes,
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
else if (currentTypeNode.kind === graphql_1.Kind.LIST_TYPE) {
|
|
533
|
+
typeNodes.push({ type: 'ListType', isNonNullable: isPreviousNodeNonNullable });
|
|
534
|
+
const nextTypeNode = currentTypeNode.type;
|
|
535
|
+
collectAndFlattenTypeNodes({
|
|
536
|
+
currentTypeNode: nextTypeNode,
|
|
537
|
+
isPreviousNodeNonNullable: false,
|
|
538
|
+
typeNodes,
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
else if (currentTypeNode.kind === graphql_1.Kind.NAMED_TYPE) {
|
|
542
|
+
typeNodes.push({
|
|
543
|
+
type: 'NamedType',
|
|
544
|
+
isNonNullable: isPreviousNodeNonNullable,
|
|
545
|
+
name: currentTypeNode.name.value,
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
}
|