@graphql-codegen/visitor-plugin-common 3.0.2-alpha-20230306101700-e8822ec25 → 3.0.2-alpha-20230306130010-da1dc7695
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.
|
@@ -50,6 +50,8 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
50
50
|
this._usedMappers = {};
|
|
51
51
|
this._resolversTypes = {};
|
|
52
52
|
this._resolversParentTypes = {};
|
|
53
|
+
this._hasReferencedResolversUnionTypes = false;
|
|
54
|
+
this._resolversUnionTypes = {};
|
|
53
55
|
this._rootTypeNames = new Set();
|
|
54
56
|
this._globalDeclarations = new Set();
|
|
55
57
|
this._hasScalars = false;
|
|
@@ -61,6 +63,7 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
61
63
|
this._variablesTransformer = new variables_to_object_js_1.OperationVariablesToObject(this.scalars, this.convertName, this.config.namespacedImportName);
|
|
62
64
|
this._resolversTypes = this.createResolversFields(type => this.applyResolverTypeWrapper(type), type => this.clearResolverTypeWrapper(type), name => this.getTypeToUse(name));
|
|
63
65
|
this._resolversParentTypes = this.createResolversFields(type => type, type => type, name => this.getParentTypeToUse(name), namedType => !(0, graphql_1.isEnumType)(namedType));
|
|
66
|
+
this._resolversUnionTypes = this.createResolversUnionTypes();
|
|
64
67
|
this._fieldContextTypeMap = this.createFieldContextTypeMap();
|
|
65
68
|
this._directiveContextTypesMap = this.createDirectivedContextType();
|
|
66
69
|
this._directiveResolverMappings = (_a = rawConfig.directiveResolverMappings) !== null && _a !== void 0 ? _a : {};
|
|
@@ -122,7 +125,6 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
122
125
|
if (typeName.startsWith('__') || (shouldInclude && !shouldInclude(schemaType))) {
|
|
123
126
|
return prev;
|
|
124
127
|
}
|
|
125
|
-
let shouldApplyOmit = false;
|
|
126
128
|
const isRootType = this._rootTypeNames.has(typeName);
|
|
127
129
|
const isMapped = this.config.mappers[typeName];
|
|
128
130
|
const isScalar = this.config.scalars[typeName];
|
|
@@ -131,7 +133,7 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
131
133
|
prev[typeName] = applyWrapper(this.config.rootValueType.type);
|
|
132
134
|
return prev;
|
|
133
135
|
}
|
|
134
|
-
if (isMapped && this.config.mappers[typeName].type) {
|
|
136
|
+
if (isMapped && this.config.mappers[typeName].type && !hasPlaceholder(this.config.mappers[typeName].type)) {
|
|
135
137
|
this.markMapperAsUsed(typeName);
|
|
136
138
|
prev[typeName] = applyWrapper(this.config.mappers[typeName].type);
|
|
137
139
|
}
|
|
@@ -162,71 +164,45 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
162
164
|
prev[typeName] = applyWrapper(this._getScalar(typeName));
|
|
163
165
|
}
|
|
164
166
|
else if ((0, graphql_1.isUnionType)(schemaType)) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
.join(' | ');
|
|
167
|
+
this._hasReferencedResolversUnionTypes = true;
|
|
168
|
+
const resolversType = this.convertName('ResolversUnionTypes');
|
|
169
|
+
prev[typeName] = applyWrapper(`${resolversType}['${typeName}']`);
|
|
169
170
|
}
|
|
170
171
|
else if ((0, graphql_1.isEnumType)(schemaType)) {
|
|
171
172
|
prev[typeName] = this.convertName(typeName, { useTypesPrefix: this.config.enumPrefix }, true);
|
|
172
173
|
}
|
|
173
174
|
else {
|
|
174
|
-
shouldApplyOmit = true;
|
|
175
175
|
prev[typeName] = this.convertName(typeName, {}, true);
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const baseType = (0, plugin_helpers_1.getBaseType)(field.type);
|
|
193
|
-
const isUnion = (0, graphql_1.isUnionType)(baseType);
|
|
194
|
-
if (!this.config.mappers[baseType.name] && !isUnion && !this._shouldMapType[baseType.name]) {
|
|
195
|
-
return null;
|
|
176
|
+
if (prev[typeName] !== 'any' && (0, graphql_1.isObjectType)(schemaType)) {
|
|
177
|
+
const relevantFields = this.getRelevantFieldsToOmit({
|
|
178
|
+
schemaType,
|
|
179
|
+
getTypeToUse,
|
|
180
|
+
shouldInclude,
|
|
181
|
+
});
|
|
182
|
+
// If relevantFields, puts ResolverTypeWrapper on top of an entire type
|
|
183
|
+
let internalType = relevantFields.length > 0 ? this.replaceFieldsInType(prev[typeName], relevantFields) : prev[typeName];
|
|
184
|
+
if (isMapped) {
|
|
185
|
+
// replace the placeholder with the actual type
|
|
186
|
+
if (hasPlaceholder(internalType)) {
|
|
187
|
+
internalType = replacePlaceholder(internalType, typeName);
|
|
188
|
+
}
|
|
189
|
+
if (this.config.mappers[typeName].type && hasPlaceholder(this.config.mappers[typeName].type)) {
|
|
190
|
+
internalType = replacePlaceholder(this.config.mappers[typeName].type, internalType);
|
|
191
|
+
}
|
|
196
192
|
}
|
|
197
|
-
|
|
198
|
-
return {
|
|
199
|
-
addOptionalSign,
|
|
200
|
-
fieldName,
|
|
201
|
-
replaceWithType: (0, utils_js_1.wrapTypeWithModifiers)(getTypeToUse(baseType.name), field.type, {
|
|
202
|
-
wrapOptional: this.applyMaybe,
|
|
203
|
-
wrapArray: this.wrapWithArray,
|
|
204
|
-
}),
|
|
205
|
-
};
|
|
206
|
-
})
|
|
207
|
-
.filter(a => a);
|
|
208
|
-
if (relevantFields.length > 0) {
|
|
209
|
-
// Puts ResolverTypeWrapper on top of an entire type
|
|
210
|
-
prev[typeName] = applyWrapper(this.replaceFieldsInType(prev[typeName], relevantFields));
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
// We still want to use ResolverTypeWrapper, even if we don't touch any fields
|
|
214
|
-
prev[typeName] = applyWrapper(prev[typeName]);
|
|
193
|
+
prev[typeName] = applyWrapper(internalType);
|
|
215
194
|
}
|
|
216
195
|
}
|
|
217
|
-
if (isMapped && hasPlaceholder(prev[typeName])) {
|
|
218
|
-
prev[typeName] = replacePlaceholder(prev[typeName], typeName);
|
|
219
|
-
}
|
|
220
196
|
if (!isMapped && hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
|
|
221
|
-
|
|
222
|
-
const name = clearWrapper(isScalar ? this._getScalar(typeName) : prev[typeName]);
|
|
223
|
-
const replaced = replacePlaceholder(this.config.defaultMapper.type, name);
|
|
224
|
-
// Don't wrap Union with ResolverTypeWrapper, each inner type already has it
|
|
197
|
+
const originalTypeName = isScalar ? this._getScalar(typeName) : prev[typeName];
|
|
225
198
|
if ((0, graphql_1.isUnionType)(schemaType)) {
|
|
226
|
-
|
|
199
|
+
// Don't clear ResolverTypeWrapper from Unions
|
|
200
|
+
prev[typeName] = replacePlaceholder(this.config.defaultMapper.type, originalTypeName);
|
|
227
201
|
}
|
|
228
202
|
else {
|
|
229
|
-
|
|
203
|
+
const name = clearWrapper(originalTypeName);
|
|
204
|
+
const replaced = replacePlaceholder(this.config.defaultMapper.type, name);
|
|
205
|
+
prev[typeName] = applyWrapper(replaced);
|
|
230
206
|
}
|
|
231
207
|
}
|
|
232
208
|
return prev;
|
|
@@ -265,6 +241,54 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
265
241
|
}
|
|
266
242
|
return `Array<${t}>`;
|
|
267
243
|
}
|
|
244
|
+
createResolversUnionTypes() {
|
|
245
|
+
if (!this._hasReferencedResolversUnionTypes) {
|
|
246
|
+
return {};
|
|
247
|
+
}
|
|
248
|
+
const allSchemaTypes = this._schema.getTypeMap();
|
|
249
|
+
const typeNames = this._federation.filterTypeNames(Object.keys(allSchemaTypes));
|
|
250
|
+
const unionTypes = typeNames.reduce((res, typeName) => {
|
|
251
|
+
const schemaType = allSchemaTypes[typeName];
|
|
252
|
+
if ((0, graphql_1.isUnionType)(schemaType)) {
|
|
253
|
+
const referencedTypes = schemaType.getTypes().map(unionMemberType => {
|
|
254
|
+
var _a;
|
|
255
|
+
const isUnionMemberMapped = this.config.mappers[unionMemberType.name];
|
|
256
|
+
// 1. If mapped without placehoder, just use it without doing extra checks
|
|
257
|
+
if (isUnionMemberMapped && !hasPlaceholder(isUnionMemberMapped.type)) {
|
|
258
|
+
return isUnionMemberMapped.type;
|
|
259
|
+
}
|
|
260
|
+
// 2. Work out value for union member type
|
|
261
|
+
// 2a. By default, use the typescript type
|
|
262
|
+
let unionMemberValue = this.convertName(unionMemberType.name, {}, true);
|
|
263
|
+
// 2b. Find fields to Omit if needed.
|
|
264
|
+
// - If no field to Omit, "type with maybe Omit" is typescript type i.e. no Omit
|
|
265
|
+
// - If there are fields to Omit, "type with maybe Omit"
|
|
266
|
+
const fieldsToOmit = this.getRelevantFieldsToOmit({
|
|
267
|
+
schemaType: unionMemberType,
|
|
268
|
+
getTypeToUse: this.getTypeToUse,
|
|
269
|
+
});
|
|
270
|
+
if (fieldsToOmit.length > 0) {
|
|
271
|
+
unionMemberValue = this.replaceFieldsInType(unionMemberValue, fieldsToOmit);
|
|
272
|
+
}
|
|
273
|
+
// 2c. If union member is mapped with placeholder, use the "type with maybe Omit" as {T}
|
|
274
|
+
if (isUnionMemberMapped && hasPlaceholder(isUnionMemberMapped.type)) {
|
|
275
|
+
return replacePlaceholder(isUnionMemberMapped.type, unionMemberValue);
|
|
276
|
+
}
|
|
277
|
+
// 2d. If has default mapper with placeholder, use the "type with maybe Omit" as {T}
|
|
278
|
+
const hasDefaultMapper = !!((_a = this.config.defaultMapper) === null || _a === void 0 ? void 0 : _a.type);
|
|
279
|
+
const isScalar = this.config.scalars[typeName];
|
|
280
|
+
if (hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
|
|
281
|
+
const finalTypename = isScalar ? this._getScalar(typeName) : unionMemberValue;
|
|
282
|
+
return replacePlaceholder(this.config.defaultMapper.type, finalTypename);
|
|
283
|
+
}
|
|
284
|
+
return unionMemberValue;
|
|
285
|
+
});
|
|
286
|
+
res[typeName] = referencedTypes.map(type => `( ${type} )`).join(' | '); // Must wrap every union member in explicit "( )" to separate the members
|
|
287
|
+
}
|
|
288
|
+
return res;
|
|
289
|
+
}, {});
|
|
290
|
+
return unionTypes;
|
|
291
|
+
}
|
|
268
292
|
createFieldContextTypeMap() {
|
|
269
293
|
return this.config.fieldContextTypes.reduce((prev, fieldContextType) => {
|
|
270
294
|
const items = fieldContextType.split('#');
|
|
@@ -309,6 +333,20 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
309
333
|
.map(typeName => (0, utils_js_1.indent)(`${typeName}: ${this._resolversParentTypes[typeName]}${this.getPunctuation(declarationKind)}`))
|
|
310
334
|
.join('\n')).string;
|
|
311
335
|
}
|
|
336
|
+
buildResolversUnionTypes() {
|
|
337
|
+
if (Object.keys(this._resolversUnionTypes).length === 0) {
|
|
338
|
+
return '';
|
|
339
|
+
}
|
|
340
|
+
const declarationKind = 'type';
|
|
341
|
+
return new utils_js_1.DeclarationBlock(this._declarationBlockConfig)
|
|
342
|
+
.export()
|
|
343
|
+
.asKind(declarationKind)
|
|
344
|
+
.withName(this.convertName('ResolversUnionTypes'))
|
|
345
|
+
.withComment('Mapping of union types')
|
|
346
|
+
.withBlock(Object.entries(this._resolversUnionTypes)
|
|
347
|
+
.map(([typeName, value]) => (0, utils_js_1.indent)(`${typeName}: ${value}${this.getPunctuation(declarationKind)}`))
|
|
348
|
+
.join('\n')).string;
|
|
349
|
+
}
|
|
312
350
|
get schema() {
|
|
313
351
|
return this._schema;
|
|
314
352
|
}
|
|
@@ -707,6 +745,38 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
707
745
|
SchemaDefinition() {
|
|
708
746
|
return null;
|
|
709
747
|
}
|
|
748
|
+
getRelevantFieldsToOmit({ schemaType, shouldInclude, getTypeToUse, }) {
|
|
749
|
+
const fields = schemaType.getFields();
|
|
750
|
+
return this._federation
|
|
751
|
+
.filterFieldNames(Object.keys(fields))
|
|
752
|
+
.filter(fieldName => {
|
|
753
|
+
const field = fields[fieldName];
|
|
754
|
+
const baseType = (0, plugin_helpers_1.getBaseType)(field.type);
|
|
755
|
+
// Filter out fields of types that are not included
|
|
756
|
+
if (shouldInclude && !shouldInclude(baseType)) {
|
|
757
|
+
return false;
|
|
758
|
+
}
|
|
759
|
+
return true;
|
|
760
|
+
})
|
|
761
|
+
.map(fieldName => {
|
|
762
|
+
const field = fields[fieldName];
|
|
763
|
+
const baseType = (0, plugin_helpers_1.getBaseType)(field.type);
|
|
764
|
+
const isUnion = (0, graphql_1.isUnionType)(baseType);
|
|
765
|
+
if (!this.config.mappers[baseType.name] && !isUnion && !this._shouldMapType[baseType.name]) {
|
|
766
|
+
return null;
|
|
767
|
+
}
|
|
768
|
+
const addOptionalSign = !this.config.avoidOptionals && !(0, graphql_1.isNonNullType)(field.type);
|
|
769
|
+
return {
|
|
770
|
+
addOptionalSign,
|
|
771
|
+
fieldName,
|
|
772
|
+
replaceWithType: (0, utils_js_1.wrapTypeWithModifiers)(getTypeToUse(baseType.name), field.type, {
|
|
773
|
+
wrapOptional: this.applyMaybe,
|
|
774
|
+
wrapArray: this.wrapWithArray,
|
|
775
|
+
}),
|
|
776
|
+
};
|
|
777
|
+
})
|
|
778
|
+
.filter(a => a);
|
|
779
|
+
}
|
|
710
780
|
}
|
|
711
781
|
exports.BaseResolversVisitor = BaseResolversVisitor;
|
|
712
782
|
function replacePlaceholder(pattern, typename) {
|
|
@@ -46,6 +46,8 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
46
46
|
this._usedMappers = {};
|
|
47
47
|
this._resolversTypes = {};
|
|
48
48
|
this._resolversParentTypes = {};
|
|
49
|
+
this._hasReferencedResolversUnionTypes = false;
|
|
50
|
+
this._resolversUnionTypes = {};
|
|
49
51
|
this._rootTypeNames = new Set();
|
|
50
52
|
this._globalDeclarations = new Set();
|
|
51
53
|
this._hasScalars = false;
|
|
@@ -57,6 +59,7 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
57
59
|
this._variablesTransformer = new OperationVariablesToObject(this.scalars, this.convertName, this.config.namespacedImportName);
|
|
58
60
|
this._resolversTypes = this.createResolversFields(type => this.applyResolverTypeWrapper(type), type => this.clearResolverTypeWrapper(type), name => this.getTypeToUse(name));
|
|
59
61
|
this._resolversParentTypes = this.createResolversFields(type => type, type => type, name => this.getParentTypeToUse(name), namedType => !isEnumType(namedType));
|
|
62
|
+
this._resolversUnionTypes = this.createResolversUnionTypes();
|
|
60
63
|
this._fieldContextTypeMap = this.createFieldContextTypeMap();
|
|
61
64
|
this._directiveContextTypesMap = this.createDirectivedContextType();
|
|
62
65
|
this._directiveResolverMappings = (_a = rawConfig.directiveResolverMappings) !== null && _a !== void 0 ? _a : {};
|
|
@@ -118,7 +121,6 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
118
121
|
if (typeName.startsWith('__') || (shouldInclude && !shouldInclude(schemaType))) {
|
|
119
122
|
return prev;
|
|
120
123
|
}
|
|
121
|
-
let shouldApplyOmit = false;
|
|
122
124
|
const isRootType = this._rootTypeNames.has(typeName);
|
|
123
125
|
const isMapped = this.config.mappers[typeName];
|
|
124
126
|
const isScalar = this.config.scalars[typeName];
|
|
@@ -127,7 +129,7 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
127
129
|
prev[typeName] = applyWrapper(this.config.rootValueType.type);
|
|
128
130
|
return prev;
|
|
129
131
|
}
|
|
130
|
-
if (isMapped && this.config.mappers[typeName].type) {
|
|
132
|
+
if (isMapped && this.config.mappers[typeName].type && !hasPlaceholder(this.config.mappers[typeName].type)) {
|
|
131
133
|
this.markMapperAsUsed(typeName);
|
|
132
134
|
prev[typeName] = applyWrapper(this.config.mappers[typeName].type);
|
|
133
135
|
}
|
|
@@ -158,71 +160,45 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
158
160
|
prev[typeName] = applyWrapper(this._getScalar(typeName));
|
|
159
161
|
}
|
|
160
162
|
else if (isUnionType(schemaType)) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
.join(' | ');
|
|
163
|
+
this._hasReferencedResolversUnionTypes = true;
|
|
164
|
+
const resolversType = this.convertName('ResolversUnionTypes');
|
|
165
|
+
prev[typeName] = applyWrapper(`${resolversType}['${typeName}']`);
|
|
165
166
|
}
|
|
166
167
|
else if (isEnumType(schemaType)) {
|
|
167
168
|
prev[typeName] = this.convertName(typeName, { useTypesPrefix: this.config.enumPrefix }, true);
|
|
168
169
|
}
|
|
169
170
|
else {
|
|
170
|
-
shouldApplyOmit = true;
|
|
171
171
|
prev[typeName] = this.convertName(typeName, {}, true);
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const baseType = getBaseType(field.type);
|
|
189
|
-
const isUnion = isUnionType(baseType);
|
|
190
|
-
if (!this.config.mappers[baseType.name] && !isUnion && !this._shouldMapType[baseType.name]) {
|
|
191
|
-
return null;
|
|
172
|
+
if (prev[typeName] !== 'any' && isObjectType(schemaType)) {
|
|
173
|
+
const relevantFields = this.getRelevantFieldsToOmit({
|
|
174
|
+
schemaType,
|
|
175
|
+
getTypeToUse,
|
|
176
|
+
shouldInclude,
|
|
177
|
+
});
|
|
178
|
+
// If relevantFields, puts ResolverTypeWrapper on top of an entire type
|
|
179
|
+
let internalType = relevantFields.length > 0 ? this.replaceFieldsInType(prev[typeName], relevantFields) : prev[typeName];
|
|
180
|
+
if (isMapped) {
|
|
181
|
+
// replace the placeholder with the actual type
|
|
182
|
+
if (hasPlaceholder(internalType)) {
|
|
183
|
+
internalType = replacePlaceholder(internalType, typeName);
|
|
184
|
+
}
|
|
185
|
+
if (this.config.mappers[typeName].type && hasPlaceholder(this.config.mappers[typeName].type)) {
|
|
186
|
+
internalType = replacePlaceholder(this.config.mappers[typeName].type, internalType);
|
|
187
|
+
}
|
|
192
188
|
}
|
|
193
|
-
|
|
194
|
-
return {
|
|
195
|
-
addOptionalSign,
|
|
196
|
-
fieldName,
|
|
197
|
-
replaceWithType: wrapTypeWithModifiers(getTypeToUse(baseType.name), field.type, {
|
|
198
|
-
wrapOptional: this.applyMaybe,
|
|
199
|
-
wrapArray: this.wrapWithArray,
|
|
200
|
-
}),
|
|
201
|
-
};
|
|
202
|
-
})
|
|
203
|
-
.filter(a => a);
|
|
204
|
-
if (relevantFields.length > 0) {
|
|
205
|
-
// Puts ResolverTypeWrapper on top of an entire type
|
|
206
|
-
prev[typeName] = applyWrapper(this.replaceFieldsInType(prev[typeName], relevantFields));
|
|
207
|
-
}
|
|
208
|
-
else {
|
|
209
|
-
// We still want to use ResolverTypeWrapper, even if we don't touch any fields
|
|
210
|
-
prev[typeName] = applyWrapper(prev[typeName]);
|
|
189
|
+
prev[typeName] = applyWrapper(internalType);
|
|
211
190
|
}
|
|
212
191
|
}
|
|
213
|
-
if (isMapped && hasPlaceholder(prev[typeName])) {
|
|
214
|
-
prev[typeName] = replacePlaceholder(prev[typeName], typeName);
|
|
215
|
-
}
|
|
216
192
|
if (!isMapped && hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
|
|
217
|
-
|
|
218
|
-
const name = clearWrapper(isScalar ? this._getScalar(typeName) : prev[typeName]);
|
|
219
|
-
const replaced = replacePlaceholder(this.config.defaultMapper.type, name);
|
|
220
|
-
// Don't wrap Union with ResolverTypeWrapper, each inner type already has it
|
|
193
|
+
const originalTypeName = isScalar ? this._getScalar(typeName) : prev[typeName];
|
|
221
194
|
if (isUnionType(schemaType)) {
|
|
222
|
-
|
|
195
|
+
// Don't clear ResolverTypeWrapper from Unions
|
|
196
|
+
prev[typeName] = replacePlaceholder(this.config.defaultMapper.type, originalTypeName);
|
|
223
197
|
}
|
|
224
198
|
else {
|
|
225
|
-
|
|
199
|
+
const name = clearWrapper(originalTypeName);
|
|
200
|
+
const replaced = replacePlaceholder(this.config.defaultMapper.type, name);
|
|
201
|
+
prev[typeName] = applyWrapper(replaced);
|
|
226
202
|
}
|
|
227
203
|
}
|
|
228
204
|
return prev;
|
|
@@ -261,6 +237,54 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
261
237
|
}
|
|
262
238
|
return `Array<${t}>`;
|
|
263
239
|
}
|
|
240
|
+
createResolversUnionTypes() {
|
|
241
|
+
if (!this._hasReferencedResolversUnionTypes) {
|
|
242
|
+
return {};
|
|
243
|
+
}
|
|
244
|
+
const allSchemaTypes = this._schema.getTypeMap();
|
|
245
|
+
const typeNames = this._federation.filterTypeNames(Object.keys(allSchemaTypes));
|
|
246
|
+
const unionTypes = typeNames.reduce((res, typeName) => {
|
|
247
|
+
const schemaType = allSchemaTypes[typeName];
|
|
248
|
+
if (isUnionType(schemaType)) {
|
|
249
|
+
const referencedTypes = schemaType.getTypes().map(unionMemberType => {
|
|
250
|
+
var _a;
|
|
251
|
+
const isUnionMemberMapped = this.config.mappers[unionMemberType.name];
|
|
252
|
+
// 1. If mapped without placehoder, just use it without doing extra checks
|
|
253
|
+
if (isUnionMemberMapped && !hasPlaceholder(isUnionMemberMapped.type)) {
|
|
254
|
+
return isUnionMemberMapped.type;
|
|
255
|
+
}
|
|
256
|
+
// 2. Work out value for union member type
|
|
257
|
+
// 2a. By default, use the typescript type
|
|
258
|
+
let unionMemberValue = this.convertName(unionMemberType.name, {}, true);
|
|
259
|
+
// 2b. Find fields to Omit if needed.
|
|
260
|
+
// - If no field to Omit, "type with maybe Omit" is typescript type i.e. no Omit
|
|
261
|
+
// - If there are fields to Omit, "type with maybe Omit"
|
|
262
|
+
const fieldsToOmit = this.getRelevantFieldsToOmit({
|
|
263
|
+
schemaType: unionMemberType,
|
|
264
|
+
getTypeToUse: this.getTypeToUse,
|
|
265
|
+
});
|
|
266
|
+
if (fieldsToOmit.length > 0) {
|
|
267
|
+
unionMemberValue = this.replaceFieldsInType(unionMemberValue, fieldsToOmit);
|
|
268
|
+
}
|
|
269
|
+
// 2c. If union member is mapped with placeholder, use the "type with maybe Omit" as {T}
|
|
270
|
+
if (isUnionMemberMapped && hasPlaceholder(isUnionMemberMapped.type)) {
|
|
271
|
+
return replacePlaceholder(isUnionMemberMapped.type, unionMemberValue);
|
|
272
|
+
}
|
|
273
|
+
// 2d. If has default mapper with placeholder, use the "type with maybe Omit" as {T}
|
|
274
|
+
const hasDefaultMapper = !!((_a = this.config.defaultMapper) === null || _a === void 0 ? void 0 : _a.type);
|
|
275
|
+
const isScalar = this.config.scalars[typeName];
|
|
276
|
+
if (hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
|
|
277
|
+
const finalTypename = isScalar ? this._getScalar(typeName) : unionMemberValue;
|
|
278
|
+
return replacePlaceholder(this.config.defaultMapper.type, finalTypename);
|
|
279
|
+
}
|
|
280
|
+
return unionMemberValue;
|
|
281
|
+
});
|
|
282
|
+
res[typeName] = referencedTypes.map(type => `( ${type} )`).join(' | '); // Must wrap every union member in explicit "( )" to separate the members
|
|
283
|
+
}
|
|
284
|
+
return res;
|
|
285
|
+
}, {});
|
|
286
|
+
return unionTypes;
|
|
287
|
+
}
|
|
264
288
|
createFieldContextTypeMap() {
|
|
265
289
|
return this.config.fieldContextTypes.reduce((prev, fieldContextType) => {
|
|
266
290
|
const items = fieldContextType.split('#');
|
|
@@ -305,6 +329,20 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
305
329
|
.map(typeName => indent(`${typeName}: ${this._resolversParentTypes[typeName]}${this.getPunctuation(declarationKind)}`))
|
|
306
330
|
.join('\n')).string;
|
|
307
331
|
}
|
|
332
|
+
buildResolversUnionTypes() {
|
|
333
|
+
if (Object.keys(this._resolversUnionTypes).length === 0) {
|
|
334
|
+
return '';
|
|
335
|
+
}
|
|
336
|
+
const declarationKind = 'type';
|
|
337
|
+
return new DeclarationBlock(this._declarationBlockConfig)
|
|
338
|
+
.export()
|
|
339
|
+
.asKind(declarationKind)
|
|
340
|
+
.withName(this.convertName('ResolversUnionTypes'))
|
|
341
|
+
.withComment('Mapping of union types')
|
|
342
|
+
.withBlock(Object.entries(this._resolversUnionTypes)
|
|
343
|
+
.map(([typeName, value]) => indent(`${typeName}: ${value}${this.getPunctuation(declarationKind)}`))
|
|
344
|
+
.join('\n')).string;
|
|
345
|
+
}
|
|
308
346
|
get schema() {
|
|
309
347
|
return this._schema;
|
|
310
348
|
}
|
|
@@ -703,6 +741,38 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
703
741
|
SchemaDefinition() {
|
|
704
742
|
return null;
|
|
705
743
|
}
|
|
744
|
+
getRelevantFieldsToOmit({ schemaType, shouldInclude, getTypeToUse, }) {
|
|
745
|
+
const fields = schemaType.getFields();
|
|
746
|
+
return this._federation
|
|
747
|
+
.filterFieldNames(Object.keys(fields))
|
|
748
|
+
.filter(fieldName => {
|
|
749
|
+
const field = fields[fieldName];
|
|
750
|
+
const baseType = getBaseType(field.type);
|
|
751
|
+
// Filter out fields of types that are not included
|
|
752
|
+
if (shouldInclude && !shouldInclude(baseType)) {
|
|
753
|
+
return false;
|
|
754
|
+
}
|
|
755
|
+
return true;
|
|
756
|
+
})
|
|
757
|
+
.map(fieldName => {
|
|
758
|
+
const field = fields[fieldName];
|
|
759
|
+
const baseType = getBaseType(field.type);
|
|
760
|
+
const isUnion = isUnionType(baseType);
|
|
761
|
+
if (!this.config.mappers[baseType.name] && !isUnion && !this._shouldMapType[baseType.name]) {
|
|
762
|
+
return null;
|
|
763
|
+
}
|
|
764
|
+
const addOptionalSign = !this.config.avoidOptionals && !isNonNullType(field.type);
|
|
765
|
+
return {
|
|
766
|
+
addOptionalSign,
|
|
767
|
+
fieldName,
|
|
768
|
+
replaceWithType: wrapTypeWithModifiers(getTypeToUse(baseType.name), field.type, {
|
|
769
|
+
wrapOptional: this.applyMaybe,
|
|
770
|
+
wrapArray: this.wrapWithArray,
|
|
771
|
+
}),
|
|
772
|
+
};
|
|
773
|
+
})
|
|
774
|
+
.filter(a => a);
|
|
775
|
+
}
|
|
706
776
|
}
|
|
707
777
|
function replacePlaceholder(pattern, typename) {
|
|
708
778
|
return pattern.replace(/\{T\}/g, typename);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/visitor-plugin-common",
|
|
3
|
-
"version": "3.0.2-alpha-
|
|
3
|
+
"version": "3.0.2-alpha-20230306130010-da1dc7695",
|
|
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
|
},
|
|
@@ -525,6 +525,8 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
|
|
|
525
525
|
};
|
|
526
526
|
protected _resolversTypes: ResolverTypes;
|
|
527
527
|
protected _resolversParentTypes: ResolverParentTypes;
|
|
528
|
+
protected _hasReferencedResolversUnionTypes: boolean;
|
|
529
|
+
protected _resolversUnionTypes: Record<string, string>;
|
|
528
530
|
protected _rootTypeNames: Set<string>;
|
|
529
531
|
protected _globalDeclarations: Set<string>;
|
|
530
532
|
protected _federation: ApolloFederation;
|
|
@@ -539,20 +541,18 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
|
|
|
539
541
|
protected shouldMapType(type: GraphQLNamedType, duringCheck?: string[]): boolean;
|
|
540
542
|
convertName(node: ASTNode | string, options?: BaseVisitorConvertOptions & ConvertOptions, applyNamespacedImport?: boolean): string;
|
|
541
543
|
protected createResolversFields(applyWrapper: (str: string) => string, clearWrapper: (str: string) => string, getTypeToUse: (str: string) => string, shouldInclude?: (type: GraphQLNamedType) => boolean): ResolverTypes;
|
|
542
|
-
protected replaceFieldsInType(typeName: string, relevantFields:
|
|
543
|
-
addOptionalSign: boolean;
|
|
544
|
-
fieldName: string;
|
|
545
|
-
replaceWithType: string;
|
|
546
|
-
}[]): string;
|
|
544
|
+
protected replaceFieldsInType(typeName: string, relevantFields: ReturnType<typeof this.getRelevantFieldsToOmit>): string;
|
|
547
545
|
protected applyMaybe(str: string): string;
|
|
548
546
|
protected applyResolverTypeWrapper(str: string): string;
|
|
549
547
|
protected clearMaybe(str: string): string;
|
|
550
548
|
protected clearResolverTypeWrapper(str: string): string;
|
|
551
549
|
protected wrapWithArray(t: string): string;
|
|
550
|
+
protected createResolversUnionTypes(): Record<string, string>;
|
|
552
551
|
protected createFieldContextTypeMap(): FieldContextTypeMap;
|
|
553
552
|
protected createDirectivedContextType(): FieldContextTypeMap;
|
|
554
553
|
buildResolversTypes(): string;
|
|
555
554
|
buildResolversParentTypes(): string;
|
|
555
|
+
buildResolversUnionTypes(): string;
|
|
556
556
|
get schema(): GraphQLSchema;
|
|
557
557
|
get defaultMapperType(): string;
|
|
558
558
|
get unusedMappers(): string[];
|
|
@@ -592,5 +592,6 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
|
|
|
592
592
|
EnumTypeDefinition(node: EnumTypeDefinitionNode): string;
|
|
593
593
|
InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode): string;
|
|
594
594
|
SchemaDefinition(): any;
|
|
595
|
+
private getRelevantFieldsToOmit;
|
|
595
596
|
}
|
|
596
597
|
export {};
|
|
@@ -525,6 +525,8 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
|
|
|
525
525
|
};
|
|
526
526
|
protected _resolversTypes: ResolverTypes;
|
|
527
527
|
protected _resolversParentTypes: ResolverParentTypes;
|
|
528
|
+
protected _hasReferencedResolversUnionTypes: boolean;
|
|
529
|
+
protected _resolversUnionTypes: Record<string, string>;
|
|
528
530
|
protected _rootTypeNames: Set<string>;
|
|
529
531
|
protected _globalDeclarations: Set<string>;
|
|
530
532
|
protected _federation: ApolloFederation;
|
|
@@ -539,20 +541,18 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
|
|
|
539
541
|
protected shouldMapType(type: GraphQLNamedType, duringCheck?: string[]): boolean;
|
|
540
542
|
convertName(node: ASTNode | string, options?: BaseVisitorConvertOptions & ConvertOptions, applyNamespacedImport?: boolean): string;
|
|
541
543
|
protected createResolversFields(applyWrapper: (str: string) => string, clearWrapper: (str: string) => string, getTypeToUse: (str: string) => string, shouldInclude?: (type: GraphQLNamedType) => boolean): ResolverTypes;
|
|
542
|
-
protected replaceFieldsInType(typeName: string, relevantFields:
|
|
543
|
-
addOptionalSign: boolean;
|
|
544
|
-
fieldName: string;
|
|
545
|
-
replaceWithType: string;
|
|
546
|
-
}[]): string;
|
|
544
|
+
protected replaceFieldsInType(typeName: string, relevantFields: ReturnType<typeof this.getRelevantFieldsToOmit>): string;
|
|
547
545
|
protected applyMaybe(str: string): string;
|
|
548
546
|
protected applyResolverTypeWrapper(str: string): string;
|
|
549
547
|
protected clearMaybe(str: string): string;
|
|
550
548
|
protected clearResolverTypeWrapper(str: string): string;
|
|
551
549
|
protected wrapWithArray(t: string): string;
|
|
550
|
+
protected createResolversUnionTypes(): Record<string, string>;
|
|
552
551
|
protected createFieldContextTypeMap(): FieldContextTypeMap;
|
|
553
552
|
protected createDirectivedContextType(): FieldContextTypeMap;
|
|
554
553
|
buildResolversTypes(): string;
|
|
555
554
|
buildResolversParentTypes(): string;
|
|
555
|
+
buildResolversUnionTypes(): string;
|
|
556
556
|
get schema(): GraphQLSchema;
|
|
557
557
|
get defaultMapperType(): string;
|
|
558
558
|
get unusedMappers(): string[];
|
|
@@ -592,5 +592,6 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
|
|
|
592
592
|
EnumTypeDefinition(node: EnumTypeDefinitionNode): string;
|
|
593
593
|
InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode): string;
|
|
594
594
|
SchemaDefinition(): any;
|
|
595
|
+
private getRelevantFieldsToOmit;
|
|
595
596
|
}
|
|
596
597
|
export {};
|