@graphql-codegen/visitor-plugin-common 3.0.2-alpha-20230306073054-e69d84f1c → 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 : {};
|
|
@@ -161,48 +164,21 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
161
164
|
prev[typeName] = applyWrapper(this._getScalar(typeName));
|
|
162
165
|
}
|
|
163
166
|
else if ((0, graphql_1.isUnionType)(schemaType)) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
.join(' | ');
|
|
167
|
+
this._hasReferencedResolversUnionTypes = true;
|
|
168
|
+
const resolversType = this.convertName('ResolversUnionTypes');
|
|
169
|
+
prev[typeName] = applyWrapper(`${resolversType}['${typeName}']`);
|
|
168
170
|
}
|
|
169
171
|
else if ((0, graphql_1.isEnumType)(schemaType)) {
|
|
170
172
|
prev[typeName] = this.convertName(typeName, { useTypesPrefix: this.config.enumPrefix }, true);
|
|
171
173
|
}
|
|
172
174
|
else {
|
|
173
175
|
prev[typeName] = this.convertName(typeName, {}, true);
|
|
174
|
-
// apply omit
|
|
175
176
|
if (prev[typeName] !== 'any' && (0, graphql_1.isObjectType)(schemaType)) {
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const baseType = (0, plugin_helpers_1.getBaseType)(field.type);
|
|
182
|
-
// Filter out fields of types that are not included
|
|
183
|
-
if (shouldInclude && !shouldInclude(baseType)) {
|
|
184
|
-
return false;
|
|
185
|
-
}
|
|
186
|
-
return true;
|
|
187
|
-
})
|
|
188
|
-
.map(fieldName => {
|
|
189
|
-
const field = fields[fieldName];
|
|
190
|
-
const baseType = (0, plugin_helpers_1.getBaseType)(field.type);
|
|
191
|
-
const isUnion = (0, graphql_1.isUnionType)(baseType);
|
|
192
|
-
if (!this.config.mappers[baseType.name] && !isUnion && !this._shouldMapType[baseType.name]) {
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
const addOptionalSign = !this.config.avoidOptionals && !(0, graphql_1.isNonNullType)(field.type);
|
|
196
|
-
return {
|
|
197
|
-
addOptionalSign,
|
|
198
|
-
fieldName,
|
|
199
|
-
replaceWithType: (0, utils_js_1.wrapTypeWithModifiers)(getTypeToUse(baseType.name), field.type, {
|
|
200
|
-
wrapOptional: this.applyMaybe,
|
|
201
|
-
wrapArray: this.wrapWithArray,
|
|
202
|
-
}),
|
|
203
|
-
};
|
|
204
|
-
})
|
|
205
|
-
.filter(a => a);
|
|
177
|
+
const relevantFields = this.getRelevantFieldsToOmit({
|
|
178
|
+
schemaType,
|
|
179
|
+
getTypeToUse,
|
|
180
|
+
shouldInclude,
|
|
181
|
+
});
|
|
206
182
|
// If relevantFields, puts ResolverTypeWrapper on top of an entire type
|
|
207
183
|
let internalType = relevantFields.length > 0 ? this.replaceFieldsInType(prev[typeName], relevantFields) : prev[typeName];
|
|
208
184
|
if (isMapped) {
|
|
@@ -218,15 +194,15 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
218
194
|
}
|
|
219
195
|
}
|
|
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 : {};
|
|
@@ -157,48 +160,21 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
157
160
|
prev[typeName] = applyWrapper(this._getScalar(typeName));
|
|
158
161
|
}
|
|
159
162
|
else if (isUnionType(schemaType)) {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
.join(' | ');
|
|
163
|
+
this._hasReferencedResolversUnionTypes = true;
|
|
164
|
+
const resolversType = this.convertName('ResolversUnionTypes');
|
|
165
|
+
prev[typeName] = applyWrapper(`${resolversType}['${typeName}']`);
|
|
164
166
|
}
|
|
165
167
|
else if (isEnumType(schemaType)) {
|
|
166
168
|
prev[typeName] = this.convertName(typeName, { useTypesPrefix: this.config.enumPrefix }, true);
|
|
167
169
|
}
|
|
168
170
|
else {
|
|
169
171
|
prev[typeName] = this.convertName(typeName, {}, true);
|
|
170
|
-
// apply omit
|
|
171
172
|
if (prev[typeName] !== 'any' && isObjectType(schemaType)) {
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const baseType = getBaseType(field.type);
|
|
178
|
-
// Filter out fields of types that are not included
|
|
179
|
-
if (shouldInclude && !shouldInclude(baseType)) {
|
|
180
|
-
return false;
|
|
181
|
-
}
|
|
182
|
-
return true;
|
|
183
|
-
})
|
|
184
|
-
.map(fieldName => {
|
|
185
|
-
const field = fields[fieldName];
|
|
186
|
-
const baseType = getBaseType(field.type);
|
|
187
|
-
const isUnion = isUnionType(baseType);
|
|
188
|
-
if (!this.config.mappers[baseType.name] && !isUnion && !this._shouldMapType[baseType.name]) {
|
|
189
|
-
return null;
|
|
190
|
-
}
|
|
191
|
-
const addOptionalSign = !this.config.avoidOptionals && !isNonNullType(field.type);
|
|
192
|
-
return {
|
|
193
|
-
addOptionalSign,
|
|
194
|
-
fieldName,
|
|
195
|
-
replaceWithType: wrapTypeWithModifiers(getTypeToUse(baseType.name), field.type, {
|
|
196
|
-
wrapOptional: this.applyMaybe,
|
|
197
|
-
wrapArray: this.wrapWithArray,
|
|
198
|
-
}),
|
|
199
|
-
};
|
|
200
|
-
})
|
|
201
|
-
.filter(a => a);
|
|
173
|
+
const relevantFields = this.getRelevantFieldsToOmit({
|
|
174
|
+
schemaType,
|
|
175
|
+
getTypeToUse,
|
|
176
|
+
shouldInclude,
|
|
177
|
+
});
|
|
202
178
|
// If relevantFields, puts ResolverTypeWrapper on top of an entire type
|
|
203
179
|
let internalType = relevantFields.length > 0 ? this.replaceFieldsInType(prev[typeName], relevantFields) : prev[typeName];
|
|
204
180
|
if (isMapped) {
|
|
@@ -214,15 +190,15 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
214
190
|
}
|
|
215
191
|
}
|
|
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 {};
|