@graphql-codegen/visitor-plugin-common 3.2.0-alpha-20230404104527-e530d3464 → 3.2.0-alpha-20230405093001-4d2d9b69b

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.
@@ -53,8 +53,10 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
53
53
  this._resolversParentTypes = {};
54
54
  this._hasReferencedResolversUnionTypes = false;
55
55
  this._hasReferencedResolversUnionParentTypes = false;
56
+ this._hasReferencedResolversInterfaceTypes = false;
56
57
  this._resolversUnionTypes = {};
57
58
  this._resolversUnionParentTypes = {};
59
+ this._resolversInterfaceTypes = {};
58
60
  this._rootTypeNames = new Set();
59
61
  this._globalDeclarations = new Set();
60
62
  this._hasScalars = false;
@@ -68,6 +70,7 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
68
70
  applyWrapper: type => this.applyResolverTypeWrapper(type),
69
71
  clearWrapper: type => this.clearResolverTypeWrapper(type),
70
72
  getTypeToUse: name => this.getTypeToUse(name),
73
+ currentType: 'ResolversTypes',
71
74
  referencedUnionType: 'ResolversUnionTypes',
72
75
  });
73
76
  this._resolversParentTypes = this.createResolversFields({
@@ -75,6 +78,7 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
75
78
  clearWrapper: type => type,
76
79
  getTypeToUse: name => this.getParentTypeToUse(name),
77
80
  referencedUnionType: 'ResolversUnionParentTypes',
81
+ currentType: 'ResolversParentTypes',
78
82
  shouldInclude: namedType => !(0, graphql_1.isEnumType)(namedType),
79
83
  });
80
84
  this._resolversUnionTypes = this.createResolversUnionTypes({
@@ -83,6 +87,7 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
83
87
  this._resolversUnionParentTypes = this.createResolversUnionTypes({
84
88
  getTypeToUse: this.getParentTypeToUse,
85
89
  });
90
+ this._resolversInterfaceTypes = this.createResolversInterfaceTypes();
86
91
  this._fieldContextTypeMap = this.createFieldContextTypeMap();
87
92
  this._directiveContextTypesMap = this.createDirectivedContextType();
88
93
  this._directiveResolverMappings = (_a = rawConfig.directiveResolverMappings) !== null && _a !== void 0 ? _a : {};
@@ -126,7 +131,7 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
126
131
  return `${applyNamespacedImport && this.config.namespacedImportName ? this.config.namespacedImportName + '.' : ''}${sourceType}`;
127
132
  }
128
133
  // Kamil: this one is heeeeavvyyyy
129
- createResolversFields({ applyWrapper, clearWrapper, getTypeToUse, referencedUnionType, shouldInclude, }) {
134
+ createResolversFields({ applyWrapper, clearWrapper, getTypeToUse, referencedUnionType, currentType, shouldInclude, }) {
130
135
  const allSchemaTypes = this._schema.getTypeMap();
131
136
  const typeNames = this._federation.filterTypeNames(Object.keys(allSchemaTypes));
132
137
  // avoid checking all types recursively if we have no `mappers` defined
@@ -156,21 +161,6 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
156
161
  this.markMapperAsUsed(typeName);
157
162
  prev[typeName] = applyWrapper(this.config.mappers[typeName].type);
158
163
  }
159
- else if ((0, graphql_1.isInterfaceType)(schemaType)) {
160
- const allTypesMap = this._schema.getTypeMap();
161
- const implementingTypes = [];
162
- for (const graphqlType of Object.values(allTypesMap)) {
163
- if (graphqlType instanceof graphql_1.GraphQLObjectType) {
164
- const allInterfaces = graphqlType.getInterfaces();
165
- if (allInterfaces.some(int => int.name === schemaType.name)) {
166
- implementingTypes.push(graphqlType.name);
167
- }
168
- }
169
- }
170
- const possibleTypes = implementingTypes.map(name => getTypeToUse(name)).join(' | ') || 'never';
171
- prev[typeName] = possibleTypes;
172
- return prev;
173
- }
174
164
  else if ((0, graphql_1.isEnumType)(schemaType) && this.config.enumValues[typeName]) {
175
165
  prev[typeName] =
176
166
  this.config.enumValues[typeName].sourceIdentifier ||
@@ -182,6 +172,13 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
182
172
  else if (isScalar) {
183
173
  prev[typeName] = applyWrapper(this._getScalar(typeName));
184
174
  }
175
+ else if ((0, graphql_1.isInterfaceType)(schemaType)) {
176
+ this._hasReferencedResolversInterfaceTypes = true;
177
+ const type = this.convertName('ResolversInterfaceTypes');
178
+ const generic = this.convertName(currentType);
179
+ prev[typeName] = applyWrapper(`${type}<${generic}>['${typeName}']`);
180
+ return prev;
181
+ }
185
182
  else if ((0, graphql_1.isUnionType)(schemaType)) {
186
183
  if (referencedUnionType === 'ResolversUnionTypes') {
187
184
  this._hasReferencedResolversUnionTypes = true;
@@ -274,12 +271,14 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
274
271
  const unionTypes = typeNames.reduce((res, typeName) => {
275
272
  const schemaType = allSchemaTypes[typeName];
276
273
  if ((0, graphql_1.isUnionType)(schemaType)) {
277
- const referencedTypes = schemaType.getTypes().map(unionMemberType => {
274
+ const referencedTypes = schemaType
275
+ .getTypes()
276
+ .map(unionMemberType => {
278
277
  var _a;
279
278
  const isUnionMemberMapped = this.config.mappers[unionMemberType.name];
280
279
  // 1. If mapped without placehoder, just use it without doing extra checks
281
280
  if (isUnionMemberMapped && !hasPlaceholder(isUnionMemberMapped.type)) {
282
- return isUnionMemberMapped.type;
281
+ return { typename: unionMemberType.name, unionMemberValue: isUnionMemberMapped.type };
283
282
  }
284
283
  // 2. Work out value for union member type
285
284
  // 2a. By default, use the typescript type
@@ -293,26 +292,106 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
293
292
  }
294
293
  // 2c. If union member is mapped with placeholder, use the "type with maybe Omit" as {T}
295
294
  if (isUnionMemberMapped && hasPlaceholder(isUnionMemberMapped.type)) {
296
- return replacePlaceholder(isUnionMemberMapped.type, unionMemberValue);
295
+ return {
296
+ typename: unionMemberType.name,
297
+ unionMemberValue: replacePlaceholder(isUnionMemberMapped.type, unionMemberValue),
298
+ };
297
299
  }
298
300
  // 2d. If has default mapper with placeholder, use the "type with maybe Omit" as {T}
299
301
  const hasDefaultMapper = !!((_a = this.config.defaultMapper) === null || _a === void 0 ? void 0 : _a.type);
300
302
  const isScalar = this.config.scalars[typeName];
301
303
  if (hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
302
304
  const finalTypename = isScalar ? this._getScalar(typeName) : unionMemberValue;
303
- return replacePlaceholder(this.config.defaultMapper.type, finalTypename);
305
+ return {
306
+ typename: unionMemberType.name,
307
+ unionMemberValue: replacePlaceholder(this.config.defaultMapper.type, finalTypename),
308
+ };
304
309
  }
310
+ return { typename: unionMemberType.name, unionMemberValue };
311
+ })
312
+ .map(({ typename, unionMemberValue }) => {
305
313
  const nonOptionalTypenameModifier = this.config.resolversNonOptionalTypename.unionMember
306
- ? ` & { __typename: "${unionMemberType}" }`
314
+ ? ` & { __typename: '${typename}' }`
307
315
  : '';
308
- return `${unionMemberValue}${nonOptionalTypenameModifier}`;
316
+ return `( ${unionMemberValue}${nonOptionalTypenameModifier} )`; // Must wrap every union member in explicit "( )" to separate the members
309
317
  });
310
- res[typeName] = referencedTypes.map(type => `( ${type} )`).join(' | '); // Must wrap every union member in explicit "( )" to separate the members
318
+ res[typeName] = referencedTypes.join(' | ');
311
319
  }
312
320
  return res;
313
321
  }, {});
314
322
  return unionTypes;
315
323
  }
324
+ createResolversInterfaceTypes() {
325
+ if (!this._hasReferencedResolversInterfaceTypes) {
326
+ return {};
327
+ }
328
+ const allSchemaTypes = this._schema.getTypeMap();
329
+ const typeNames = this._federation.filterTypeNames(Object.keys(allSchemaTypes));
330
+ const interfaceTypes = typeNames.reduce((res, typeName) => {
331
+ const schemaType = allSchemaTypes[typeName];
332
+ if ((0, graphql_1.isInterfaceType)(schemaType)) {
333
+ const allTypesMap = this._schema.getTypeMap();
334
+ const implementingTypes = [];
335
+ for (const graphqlType of Object.values(allTypesMap)) {
336
+ if (graphqlType instanceof graphql_1.GraphQLObjectType) {
337
+ const allInterfaces = graphqlType.getInterfaces();
338
+ if (allInterfaces.some(int => int.name === schemaType.name)) {
339
+ implementingTypes.push(graphqlType);
340
+ }
341
+ }
342
+ }
343
+ const possibleTypes = implementingTypes
344
+ .map(type => {
345
+ var _a;
346
+ // TODO: eddeee888 extract this and apply to ResolversUnionTypes
347
+ const isTypeMapped = this.config.mappers[type.name];
348
+ // 1. If mapped without placehoder, just use it without doing extra checks
349
+ if (isTypeMapped && !hasPlaceholder(isTypeMapped.type)) {
350
+ return { typename: type.name, typeValue: isTypeMapped.type };
351
+ }
352
+ // 2. Work out value for type
353
+ // 2a. By default, use the typescript type
354
+ let typeValue = this.convertName(type.name, {}, true);
355
+ // 2b. Find fields to Omit if needed.
356
+ // - If no field to Omit, "type with maybe Omit" is typescript type i.e. no Omit
357
+ // - If there are fields to Omit, keep track of these "type with maybe Omit" to replace in original unionMemberValue
358
+ const fieldsToOmit = this.getRelevantFieldsToOmit({
359
+ schemaType: type,
360
+ getTypeToUse: baseType => `RefType['${baseType}']`,
361
+ });
362
+ if (fieldsToOmit.length > 0) {
363
+ typeValue = this.replaceFieldsInType(typeValue, fieldsToOmit);
364
+ }
365
+ // 2c. If type is mapped with placeholder, use the "type with maybe Omit" as {T}
366
+ if (isTypeMapped && hasPlaceholder(isTypeMapped.type)) {
367
+ return { typename: type.name, typeValue: replacePlaceholder(isTypeMapped.type, typeValue) };
368
+ }
369
+ // 2d. If has default mapper with placeholder, use the "type with maybe Omit" as {T}
370
+ const hasDefaultMapper = !!((_a = this.config.defaultMapper) === null || _a === void 0 ? void 0 : _a.type);
371
+ const isScalar = this.config.scalars[typeName];
372
+ if (hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
373
+ const finalTypename = isScalar ? this._getScalar(typeName) : typeValue;
374
+ return {
375
+ typename: type.name,
376
+ typeValue: replacePlaceholder(this.config.defaultMapper.type, finalTypename),
377
+ };
378
+ }
379
+ // ENDTODO
380
+ return { typename: type.name, typeValue };
381
+ })
382
+ .map(({ typename, typeValue }) => {
383
+ const nonOptionalTypenameModifier = this.config.resolversNonOptionalTypename.interfaceImplementingType
384
+ ? ` & { __typename: '${typename}' }`
385
+ : '';
386
+ return `( ${typeValue}${nonOptionalTypenameModifier} )`; // Must wrap every type in explicit "( )" to separate them
387
+ })
388
+ .join(' | ') || 'never';
389
+ res[typeName] = possibleTypes;
390
+ }
391
+ return res;
392
+ }, {});
393
+ return interfaceTypes;
394
+ }
316
395
  createFieldContextTypeMap() {
317
396
  return this.config.fieldContextTypes.reduce((prev, fieldContextType) => {
318
397
  const items = fieldContextType.split('#');
@@ -385,6 +464,20 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
385
464
  .map(([typeName, value]) => (0, utils_js_1.indent)(`${typeName}: ${value}${this.getPunctuation(declarationKind)}`))
386
465
  .join('\n')).string;
387
466
  }
467
+ buildResolversInterfaceTypes() {
468
+ if (Object.keys(this._resolversInterfaceTypes).length === 0) {
469
+ return '';
470
+ }
471
+ const declarationKind = 'type';
472
+ return new utils_js_1.DeclarationBlock(this._declarationBlockConfig)
473
+ .export()
474
+ .asKind(declarationKind)
475
+ .withName(this.convertName('ResolversInterfaceTypes'), `<RefType extends Record<string, unknown>>`)
476
+ .withComment('Mapping of interface types')
477
+ .withBlock(Object.entries(this._resolversInterfaceTypes)
478
+ .map(([typeName, value]) => (0, utils_js_1.indent)(`${typeName}: ${value}${this.getPunctuation(declarationKind)}`))
479
+ .join('\n')).string;
480
+ }
388
481
  get schema() {
389
482
  return this._schema;
390
483
  }
@@ -829,6 +922,7 @@ function normalizeResolversNonOptionalTypename(input) {
829
922
  if (typeof input === 'boolean') {
830
923
  return {
831
924
  unionMember: input,
925
+ interfaceImplementingType: input,
832
926
  };
833
927
  }
834
928
  return {
@@ -149,74 +149,38 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
149
149
  }
150
150
  let metaString = '';
151
151
  if (this._onExecutableDocumentNode && node.kind === graphql_1.Kind.OPERATION_DEFINITION) {
152
- const meta = this._getGraphQLCodegenMetadata(node, definitions);
152
+ const meta = this._onExecutableDocumentNode({
153
+ kind: graphql_1.Kind.DOCUMENT,
154
+ definitions,
155
+ });
153
156
  if (meta) {
157
+ metaString = `"__meta__":${JSON.stringify(meta)},`;
154
158
  if (this._omitDefinitions === true) {
155
- return `{${`"__meta__":${JSON.stringify(meta)},`.slice(0, -1)}}`;
159
+ return `{${metaString.slice(0, -1)}}`;
156
160
  }
157
- metaString = `"__meta__":${JSON.stringify(meta)},`;
158
161
  }
159
162
  }
160
163
  return `{${metaString}"kind":"${graphql_1.Kind.DOCUMENT}","definitions":${jsonStringify(definitions)}}`;
161
164
  }
162
165
  if (this.config.documentMode === DocumentMode.string) {
163
- if (node.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
164
- return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify({ fragmentName: node.name.value })})`;
165
- }
166
+ let meta;
166
167
  if (this._onExecutableDocumentNode && node.kind === graphql_1.Kind.OPERATION_DEFINITION) {
167
- const meta = this._getGraphQLCodegenMetadata(node, (0, graphql_tag_1.default)([doc]).definitions);
168
- if (meta) {
169
- if (this._omitDefinitions === true) {
170
- return `{${`"__meta__":${JSON.stringify(meta)},`.slice(0, -1)}}`;
171
- }
172
- return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify(meta)})`;
168
+ meta = this._onExecutableDocumentNode({
169
+ kind: graphql_1.Kind.DOCUMENT,
170
+ definitions: (0, graphql_tag_1.default)([doc]).definitions,
171
+ });
172
+ if (meta && this._omitDefinitions === true) {
173
+ return `{${`"__meta__":${JSON.stringify(meta)},`.slice(0, -1)}}`;
173
174
  }
174
175
  }
176
+ if (meta) {
177
+ return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify(meta)})`;
178
+ }
175
179
  return `new TypedDocumentString(\`${doc}\`)`;
176
180
  }
177
181
  const gqlImport = this._parseImport(this.config.gqlImport || 'graphql-tag');
178
182
  return (gqlImport.propName || 'gql') + '`' + doc + '`';
179
183
  }
180
- _getGraphQLCodegenMetadata(node, definitions) {
181
- let meta;
182
- meta = this._onExecutableDocumentNode({
183
- kind: graphql_1.Kind.DOCUMENT,
184
- definitions,
185
- });
186
- const deferredFields = this._findDeferredFields(node);
187
- if (Object.keys(deferredFields).length) {
188
- meta = {
189
- ...meta,
190
- deferredFields,
191
- };
192
- }
193
- return meta;
194
- }
195
- _findDeferredFields(node) {
196
- const deferredFields = {};
197
- const queue = [...node.selectionSet.selections];
198
- while (queue.length) {
199
- const selection = queue.shift();
200
- if (selection.kind === graphql_1.Kind.FRAGMENT_SPREAD &&
201
- selection.directives.some((d) => d.name.value === 'defer')) {
202
- const fragmentName = selection.name.value;
203
- const fragment = this.fragmentsGraph.getNodeData(fragmentName);
204
- if (fragment) {
205
- const fields = fragment.node.selectionSet.selections.reduce((acc, selection) => {
206
- if (selection.kind === graphql_1.Kind.FIELD) {
207
- acc.push(selection.name.value);
208
- }
209
- return acc;
210
- }, []);
211
- deferredFields[fragmentName] = fields;
212
- }
213
- }
214
- else if (selection.kind === graphql_1.Kind.FIELD && selection.selectionSet) {
215
- queue.push(...selection.selectionSet.selections);
216
- }
217
- }
218
- return deferredFields;
219
- }
220
184
  _generateFragment(fragmentDocument) {
221
185
  const name = this.getFragmentVariableName(fragmentDocument);
222
186
  const fragmentTypeSuffix = this.getFragmentSuffix(fragmentDocument);
@@ -17,13 +17,13 @@ class BaseSelectionSetProcessor {
17
17
  }
18
18
  return `(\n ${pieces.join(`\n & `)}\n)`;
19
19
  }
20
- transformPrimitiveFields(_schemaType, _fields, _unsetTypes) {
20
+ transformPrimitiveFields(_schemaType, _fields) {
21
21
  throw new Error(`Please override "transformPrimitiveFields" as part of your BaseSelectionSetProcessor implementation!`);
22
22
  }
23
- transformAliasesPrimitiveFields(_schemaType, _fields, _unsetTypes) {
23
+ transformAliasesPrimitiveFields(_schemaType, _fields) {
24
24
  throw new Error(`Please override "transformAliasesPrimitiveFields" as part of your BaseSelectionSetProcessor implementation!`);
25
25
  }
26
- transformLinkFields(_fields, _unsetTypes) {
26
+ transformLinkFields(_fields) {
27
27
  throw new Error(`Please override "transformLinkFields" as part of your BaseSelectionSetProcessor implementation!`);
28
28
  }
29
29
  transformTypenameField(_type, _name) {
@@ -13,7 +13,7 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
13
13
  },
14
14
  ];
15
15
  }
16
- transformPrimitiveFields(schemaType, fields, unsetTypes) {
16
+ transformPrimitiveFields(schemaType, fields) {
17
17
  if (fields.length === 0) {
18
18
  return [];
19
19
  }
@@ -23,13 +23,6 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
23
23
  let typeToUse = baseType.name;
24
24
  const useInnerType = field.isConditional && (0, graphql_1.isNonNullType)(fieldObj.type);
25
25
  const innerType = useInnerType ? (0, plugin_helpers_1.removeNonNullWrapper)(fieldObj.type) : undefined;
26
- const name = this.config.formatNamedField(field.fieldName, useInnerType ? innerType : fieldObj.type, field.isConditional, unsetTypes);
27
- if (unsetTypes) {
28
- return {
29
- name,
30
- type: 'never',
31
- };
32
- }
33
26
  if ((0, graphql_1.isEnumType)(baseType)) {
34
27
  typeToUse =
35
28
  (this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '') +
@@ -38,6 +31,7 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
38
31
  else if (this.config.scalars[baseType.name]) {
39
32
  typeToUse = this.config.scalars[baseType.name];
40
33
  }
34
+ const name = this.config.formatNamedField(field.fieldName, useInnerType ? innerType : fieldObj.type, field.isConditional);
41
35
  const wrappedType = this.config.wrapTypeWithModifiers(typeToUse, fieldObj.type);
42
36
  return {
43
37
  name,
@@ -45,7 +39,7 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
45
39
  };
46
40
  });
47
41
  }
48
- transformAliasesPrimitiveFields(schemaType, fields, unsetTypes) {
42
+ transformAliasesPrimitiveFields(schemaType, fields) {
49
43
  if (fields.length === 0) {
50
44
  return [];
51
45
  }
@@ -65,13 +59,7 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
65
59
  (this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '') +
66
60
  this.config.convertName(baseType.name, { useTypesPrefix: this.config.enumPrefix });
67
61
  }
68
- const name = this.config.formatNamedField(aliasedField.alias, fieldObj.type, undefined, unsetTypes);
69
- if (unsetTypes) {
70
- return {
71
- type: 'never',
72
- name,
73
- };
74
- }
62
+ const name = this.config.formatNamedField(aliasedField.alias, fieldObj.type);
75
63
  const wrappedType = this.config.wrapTypeWithModifiers(typeToUse, fieldObj.type);
76
64
  return {
77
65
  name,
@@ -79,13 +67,13 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
79
67
  };
80
68
  });
81
69
  }
82
- transformLinkFields(fields, unsetTypes) {
70
+ transformLinkFields(fields) {
83
71
  if (fields.length === 0) {
84
72
  return [];
85
73
  }
86
74
  return fields.map(field => ({
87
75
  name: field.alias || field.name,
88
- type: unsetTypes ? 'never' : field.selectionSet,
76
+ type: field.selectionSet,
89
77
  }));
90
78
  }
91
79
  }
@@ -48,17 +48,8 @@ class SelectionSetToObject {
48
48
  const { fields, inlines, spreads } = (0, utils_js_1.separateSelectionSet)(node.selectionSet.selections);
49
49
  const spreadsUsage = this.buildFragmentSpreadsUsage(spreads);
50
50
  const directives = node.directives || undefined;
51
- // When we collect the selection sets of inline fragments we need to
52
- // make sure directives on the inline fragments are stored in a way
53
- // that can be associated back to the fields in the fragment, to
54
- // support things like making those fields optional when deferring a
55
- // fragment (using @defer).
56
- const fieldsWithFragmentDirectives = fields.map(field => ({
57
- ...field,
58
- fragmentDirectives: field.fragmentDirectives || directives,
59
- }));
60
51
  if ((0, graphql_1.isObjectType)(typeOnSchema)) {
61
- this._appendToTypeMap(types, typeOnSchema.name, fieldsWithFragmentDirectives);
52
+ this._appendToTypeMap(types, typeOnSchema.name, fields);
62
53
  this._appendToTypeMap(types, typeOnSchema.name, spreadsUsage[typeOnSchema.name]);
63
54
  this._appendToTypeMap(types, typeOnSchema.name, directives);
64
55
  this._collectInlineFragments(typeOnSchema, inlines, types);
@@ -169,7 +160,6 @@ class SelectionSetToObject {
169
160
  typeName: usage,
170
161
  onType: fragmentSpreadObject.onType,
171
162
  selectionNodes: [...fragmentSpreadObject.node.selectionSet.selections],
172
- fragmentDirectives: [...spread.directives],
173
163
  });
174
164
  }
175
165
  }
@@ -231,17 +221,8 @@ class SelectionSetToObject {
231
221
  if (!(0, graphql_1.isObjectType)(schemaType)) {
232
222
  throw new TypeError(`Invalid state! Schema type ${typeName} is not a valid GraphQL object!`);
233
223
  }
234
- const allNodes = selectionNodesByTypeName.get(typeName) || [];
224
+ const selectionNodes = selectionNodesByTypeName.get(typeName) || [];
235
225
  prev[typeName] || (prev[typeName] = []);
236
- const { incrementalNodes, selectionNodes } = allNodes.reduce((acc, node) => {
237
- if ('fragmentDirectives' in node && (0, utils_js_1.hasIncrementalDeliveryDirectives)(node.fragmentDirectives)) {
238
- acc.incrementalNodes.push(node);
239
- }
240
- else {
241
- acc.selectionNodes.push(node);
242
- }
243
- return acc;
244
- }, { selectionNodes: [], incrementalNodes: [] });
245
226
  const { fields } = this.buildSelectionSet(schemaType, selectionNodes);
246
227
  const transformedSet = this.selectionSetStringFromFields(fields);
247
228
  if (transformedSet) {
@@ -250,22 +231,6 @@ class SelectionSetToObject {
250
231
  else {
251
232
  mustAddEmptyObject = true;
252
233
  }
253
- for (const incrementalNode of incrementalNodes) {
254
- if (this._config.inlineFragmentTypes === 'mask' && 'fragmentName' in incrementalNode) {
255
- const { fields: incrementalFields } = this.buildSelectionSet(schemaType, [incrementalNode], {
256
- unsetTypes: true,
257
- });
258
- prev[typeName].push(this.selectionSetStringFromFields(incrementalFields));
259
- continue;
260
- }
261
- const { fields: initialFields } = this.buildSelectionSet(schemaType, [incrementalNode]);
262
- const { fields: subsequentFields } = this.buildSelectionSet(schemaType, [incrementalNode], {
263
- unsetTypes: true,
264
- });
265
- const initialSet = this.selectionSetStringFromFields(initialFields);
266
- const subsequentSet = this.selectionSetStringFromFields(subsequentFields);
267
- prev[typeName].push({ union: [initialSet, subsequentSet] });
268
- }
269
234
  return prev;
270
235
  }, {});
271
236
  return { grouped, mustAddEmptyObject };
@@ -338,7 +303,7 @@ class SelectionSetToObject {
338
303
  const transformedSet = this._processor.buildSelectionSetFromStrings([...allStrings, mergedObjects].filter(Boolean));
339
304
  return transformedSet;
340
305
  }
341
- buildSelectionSet(parentSchemaType, selectionNodes, options) {
306
+ buildSelectionSet(parentSchemaType, selectionNodes) {
342
307
  var _a, _b, _c;
343
308
  const primitiveFields = new Map();
344
309
  const primitiveAliasFields = new Map();
@@ -429,10 +394,9 @@ class SelectionSetToObject {
429
394
  const realSelectedFieldType = (0, plugin_helpers_1.getBaseType)(selectedFieldType);
430
395
  const selectionSet = this.createNext(realSelectedFieldType, field.selectionSet);
431
396
  const isConditional = (0, utils_js_1.hasConditionalDirectives)(field) || inlineFragmentConditional;
432
- const isOptional = options === null || options === void 0 ? void 0 : options.unsetTypes;
433
397
  linkFields.push({
434
398
  alias: field.alias ? this._processor.config.formatNamedField(field.alias.value, selectedFieldType) : undefined,
435
- name: this._processor.config.formatNamedField(field.name.value, selectedFieldType, isConditional, isOptional),
399
+ name: this._processor.config.formatNamedField(field.name.value, selectedFieldType, isConditional),
436
400
  type: realSelectedFieldType.name,
437
401
  selectionSet: this._processor.config.wrapTypeWithModifiers(selectionSet.transformSelectionSet().split(`\n`).join(`\n `), selectedFieldType),
438
402
  });
@@ -448,12 +412,12 @@ class SelectionSetToObject {
448
412
  ...this._processor.transformPrimitiveFields(parentSchemaType, Array.from(primitiveFields.values()).map(field => ({
449
413
  isConditional: (0, utils_js_1.hasConditionalDirectives)(field),
450
414
  fieldName: field.name.value,
451
- })), options === null || options === void 0 ? void 0 : options.unsetTypes),
415
+ }))),
452
416
  ...this._processor.transformAliasesPrimitiveFields(parentSchemaType, Array.from(primitiveAliasFields.values()).map(field => ({
453
417
  alias: field.alias.value,
454
418
  fieldName: field.name.value,
455
- })), options === null || options === void 0 ? void 0 : options.unsetTypes),
456
- ...this._processor.transformLinkFields(linkFields, options === null || options === void 0 ? void 0 : options.unsetTypes),
419
+ }))),
420
+ ...this._processor.transformLinkFields(linkFields),
457
421
  ].filter(Boolean);
458
422
  const allStrings = transformed.filter(t => typeof t === 'string');
459
423
  const allObjectsMerged = transformed
@@ -469,9 +433,7 @@ class SelectionSetToObject {
469
433
  fields.push(...fragmentsSpreadUsages);
470
434
  }
471
435
  else if (this._config.inlineFragmentTypes === 'mask') {
472
- fields.push(`{ ' $fragmentRefs'?: { ${fragmentsSpreadUsages
473
- .map(name => `'${name}': ${(options === null || options === void 0 ? void 0 : options.unsetTypes) ? `Incremental<${name}>` : name}`)
474
- .join(`;`)} } }`);
436
+ fields.push(`{ ' $fragmentRefs'?: { ${fragmentsSpreadUsages.map(name => `'${name}': ${name}`).join(`;`)} } }`);
475
437
  }
476
438
  }
477
439
  return { typeInfo: typeInfoField, fields };
@@ -509,19 +471,14 @@ class SelectionSetToObject {
509
471
  }
510
472
  return (Object.keys(grouped)
511
473
  .map(typeName => {
512
- const hasUnions = grouped[typeName].filter(s => typeof s !== 'string' && s.union).length > 0;
513
474
  const relevant = grouped[typeName].filter(Boolean);
514
475
  if (relevant.length === 0) {
515
476
  return null;
516
477
  }
517
- const res = relevant
518
- .map(selectionObject => {
519
- if (typeof selectionObject === 'string')
520
- return selectionObject;
521
- return '(' + selectionObject.union.join(' | ') + ')';
522
- })
523
- .join(' & ');
524
- return relevant.length > 1 && !hasUnions ? `( ${res} )` : res;
478
+ if (relevant.length === 1) {
479
+ return relevant[0];
480
+ }
481
+ return `( ${relevant.join(' & ')} )`;
525
482
  })
526
483
  .filter(Boolean)
527
484
  .join(' | ') + this.getEmptyObjectTypeString(mustAddEmptyObject));
package/cjs/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isOneOfInputObjectType = exports.wrapTypeNodeWithModifiers = exports.removeDescription = exports.wrapTypeWithModifiers = exports.hasIncrementalDeliveryDirectives = exports.hasConditionalDirectives = exports.getPossibleTypes = exports.separateSelectionSet = exports.getFieldNodeNameValue = exports.mergeSelectionSets = exports.REQUIRE_FIELDS_TYPE = exports.OMIT_TYPE = exports.stripMapperTypeInterpolation = exports.getRootTypeNames = exports.buildScalars = exports.buildScalarsFromConfig = exports.convertNameParts = exports.getBaseTypeNode = exports.DeclarationBlock = exports.transformComment = exports.indentMultiline = exports.indent = exports.breakLine = exports.wrapWithSingleQuotes = exports.block = exports.quoteIfNeeded = exports.getConfigValue = void 0;
3
+ exports.isOneOfInputObjectType = exports.wrapTypeNodeWithModifiers = exports.removeDescription = exports.wrapTypeWithModifiers = exports.hasConditionalDirectives = exports.getPossibleTypes = exports.separateSelectionSet = exports.getFieldNodeNameValue = exports.mergeSelectionSets = exports.REQUIRE_FIELDS_TYPE = exports.OMIT_TYPE = exports.stripMapperTypeInterpolation = exports.getRootTypeNames = exports.buildScalars = exports.buildScalarsFromConfig = exports.convertNameParts = exports.getBaseTypeNode = exports.DeclarationBlock = exports.transformComment = exports.indentMultiline = exports.indent = exports.breakLine = exports.wrapWithSingleQuotes = exports.block = exports.quoteIfNeeded = exports.getConfigValue = void 0;
4
4
  const graphql_1 = require("graphql");
5
5
  const mappers_js_1 = require("./mappers.js");
6
6
  const scalars_js_1 = require("./scalars.js");
@@ -342,11 +342,6 @@ function hasConditionalDirectives(field) {
342
342
  return (_a = field.directives) === null || _a === void 0 ? void 0 : _a.some(directive => CONDITIONAL_DIRECTIVES.includes(directive.name.value));
343
343
  }
344
344
  exports.hasConditionalDirectives = hasConditionalDirectives;
345
- function hasIncrementalDeliveryDirectives(directives) {
346
- const INCREMENTAL_DELIVERY_DIRECTIVES = ['defer'];
347
- return directives === null || directives === void 0 ? void 0 : directives.some(directive => INCREMENTAL_DELIVERY_DIRECTIVES.includes(directive.name.value));
348
- }
349
- exports.hasIncrementalDeliveryDirectives = hasIncrementalDeliveryDirectives;
350
345
  function wrapTypeWithModifiers(baseType, type, options) {
351
346
  let currentType = type;
352
347
  const modifiers = [];