@graphql-codegen/visitor-plugin-common 3.1.0 → 3.1.1-alpha-20230405162832-a31e8a154

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.
@@ -274,12 +274,14 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
274
274
  const unionTypes = typeNames.reduce((res, typeName) => {
275
275
  const schemaType = allSchemaTypes[typeName];
276
276
  if ((0, graphql_1.isUnionType)(schemaType)) {
277
- const referencedTypes = schemaType.getTypes().map(unionMemberType => {
277
+ const referencedTypes = schemaType
278
+ .getTypes()
279
+ .map(unionMemberType => {
278
280
  var _a;
279
281
  const isUnionMemberMapped = this.config.mappers[unionMemberType.name];
280
282
  // 1. If mapped without placehoder, just use it without doing extra checks
281
283
  if (isUnionMemberMapped && !hasPlaceholder(isUnionMemberMapped.type)) {
282
- return isUnionMemberMapped.type;
284
+ return { typename: unionMemberType.name, unionMemberValue: isUnionMemberMapped.type };
283
285
  }
284
286
  // 2. Work out value for union member type
285
287
  // 2a. By default, use the typescript type
@@ -293,21 +295,30 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
293
295
  }
294
296
  // 2c. If union member is mapped with placeholder, use the "type with maybe Omit" as {T}
295
297
  if (isUnionMemberMapped && hasPlaceholder(isUnionMemberMapped.type)) {
296
- return replacePlaceholder(isUnionMemberMapped.type, unionMemberValue);
298
+ return {
299
+ typename: unionMemberType.name,
300
+ unionMemberValue: replacePlaceholder(isUnionMemberMapped.type, unionMemberValue),
301
+ };
297
302
  }
298
303
  // 2d. If has default mapper with placeholder, use the "type with maybe Omit" as {T}
299
304
  const hasDefaultMapper = !!((_a = this.config.defaultMapper) === null || _a === void 0 ? void 0 : _a.type);
300
305
  const isScalar = this.config.scalars[typeName];
301
306
  if (hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
302
307
  const finalTypename = isScalar ? this._getScalar(typeName) : unionMemberValue;
303
- return replacePlaceholder(this.config.defaultMapper.type, finalTypename);
308
+ return {
309
+ typename: unionMemberType.name,
310
+ unionMemberValue: replacePlaceholder(this.config.defaultMapper.type, finalTypename),
311
+ };
304
312
  }
313
+ return { typename: unionMemberType.name, unionMemberValue };
314
+ })
315
+ .map(({ typename, unionMemberValue }) => {
305
316
  const nonOptionalTypenameModifier = this.config.resolversNonOptionalTypename.unionMember
306
- ? ` & { __typename: "${unionMemberType}" }`
317
+ ? ` & { __typename: '${typename}' }`
307
318
  : '';
308
- return `${unionMemberValue}${nonOptionalTypenameModifier}`;
319
+ return `( ${unionMemberValue}${nonOptionalTypenameModifier} )`; // Must wrap every union member in explicit "( )" to separate the members
309
320
  });
310
- res[typeName] = referencedTypes.map(type => `( ${type} )`).join(' | '); // Must wrap every union member in explicit "( )" to separate the members
321
+ res[typeName] = referencedTypes.join(' | ');
311
322
  }
312
323
  return res;
313
324
  }, {});
@@ -270,12 +270,14 @@ export class BaseResolversVisitor extends BaseVisitor {
270
270
  const unionTypes = typeNames.reduce((res, typeName) => {
271
271
  const schemaType = allSchemaTypes[typeName];
272
272
  if (isUnionType(schemaType)) {
273
- const referencedTypes = schemaType.getTypes().map(unionMemberType => {
273
+ const referencedTypes = schemaType
274
+ .getTypes()
275
+ .map(unionMemberType => {
274
276
  var _a;
275
277
  const isUnionMemberMapped = this.config.mappers[unionMemberType.name];
276
278
  // 1. If mapped without placehoder, just use it without doing extra checks
277
279
  if (isUnionMemberMapped && !hasPlaceholder(isUnionMemberMapped.type)) {
278
- return isUnionMemberMapped.type;
280
+ return { typename: unionMemberType.name, unionMemberValue: isUnionMemberMapped.type };
279
281
  }
280
282
  // 2. Work out value for union member type
281
283
  // 2a. By default, use the typescript type
@@ -289,21 +291,30 @@ export class BaseResolversVisitor extends BaseVisitor {
289
291
  }
290
292
  // 2c. If union member is mapped with placeholder, use the "type with maybe Omit" as {T}
291
293
  if (isUnionMemberMapped && hasPlaceholder(isUnionMemberMapped.type)) {
292
- return replacePlaceholder(isUnionMemberMapped.type, unionMemberValue);
294
+ return {
295
+ typename: unionMemberType.name,
296
+ unionMemberValue: replacePlaceholder(isUnionMemberMapped.type, unionMemberValue),
297
+ };
293
298
  }
294
299
  // 2d. If has default mapper with placeholder, use the "type with maybe Omit" as {T}
295
300
  const hasDefaultMapper = !!((_a = this.config.defaultMapper) === null || _a === void 0 ? void 0 : _a.type);
296
301
  const isScalar = this.config.scalars[typeName];
297
302
  if (hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
298
303
  const finalTypename = isScalar ? this._getScalar(typeName) : unionMemberValue;
299
- return replacePlaceholder(this.config.defaultMapper.type, finalTypename);
304
+ return {
305
+ typename: unionMemberType.name,
306
+ unionMemberValue: replacePlaceholder(this.config.defaultMapper.type, finalTypename),
307
+ };
300
308
  }
309
+ return { typename: unionMemberType.name, unionMemberValue };
310
+ })
311
+ .map(({ typename, unionMemberValue }) => {
301
312
  const nonOptionalTypenameModifier = this.config.resolversNonOptionalTypename.unionMember
302
- ? ` & { __typename: "${unionMemberType}" }`
313
+ ? ` & { __typename: '${typename}' }`
303
314
  : '';
304
- return `${unionMemberValue}${nonOptionalTypenameModifier}`;
315
+ return `( ${unionMemberValue}${nonOptionalTypenameModifier} )`; // Must wrap every union member in explicit "( )" to separate the members
305
316
  });
306
- res[typeName] = referencedTypes.map(type => `( ${type} )`).join(' | '); // Must wrap every union member in explicit "( )" to separate the members
317
+ res[typeName] = referencedTypes.join(' | ');
307
318
  }
308
319
  return res;
309
320
  }, {});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/visitor-plugin-common",
3
- "version": "3.1.0",
3
+ "version": "3.1.1-alpha-20230405162832-a31e8a154",
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
  },