@graphql-codegen/visitor-plugin-common 3.1.0-alpha-20230329092554-66cdc8d69 → 3.1.0-alpha-20230329100531-e8e2561d7

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.
@@ -150,38 +150,71 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
150
150
  }
151
151
  let metaString = '';
152
152
  if (this._onExecutableDocumentNode && node.kind === graphql_1.Kind.OPERATION_DEFINITION) {
153
- const meta = this._onExecutableDocumentNode({
154
- kind: graphql_1.Kind.DOCUMENT,
155
- definitions,
156
- });
153
+ const meta = this._getGraphQLCodegenMetadata(node, definitions);
157
154
  if (meta) {
158
- metaString = `"__meta__":${JSON.stringify(meta)},`;
159
155
  if (this._omitDefinitions === true) {
160
- return `{${metaString.slice(0, -1)}}`;
156
+ return `{${`"__meta__":${JSON.stringify(meta)},`.slice(0, -1)}}`;
161
157
  }
158
+ metaString = `"__meta__":${JSON.stringify(meta)},`;
162
159
  }
163
160
  }
164
161
  return `{${metaString}"kind":"${graphql_1.Kind.DOCUMENT}","definitions":${jsonStringify(definitions)}}`;
165
162
  }
166
163
  if (this.config.documentMode === DocumentMode.string) {
167
- let meta;
168
164
  if (this._onExecutableDocumentNode && node.kind === graphql_1.Kind.OPERATION_DEFINITION) {
169
- meta = this._onExecutableDocumentNode({
170
- kind: graphql_1.Kind.DOCUMENT,
171
- definitions: (0, graphql_tag_1.default)([doc]).definitions,
172
- });
173
- if (meta && this._omitDefinitions === true) {
174
- return `{${`"__meta__":${JSON.stringify(meta)},`.slice(0, -1)}}`;
165
+ const meta = this._getGraphQLCodegenMetadata(node, (0, graphql_tag_1.default)([doc]).definitions);
166
+ if (meta) {
167
+ if (this._omitDefinitions === true) {
168
+ return `{${`"__meta__":${JSON.stringify(meta)},`.slice(0, -1)}}`;
169
+ }
170
+ return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify(meta)})`;
175
171
  }
176
172
  }
177
- if (meta) {
178
- return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify(meta)})`;
179
- }
180
173
  return `new TypedDocumentString(\`${doc}\`)`;
181
174
  }
182
175
  const gqlImport = this._parseImport(this.config.gqlImport || 'graphql-tag');
183
176
  return (gqlImport.propName || 'gql') + '`' + doc + '`';
184
177
  }
178
+ _getGraphQLCodegenMetadata(node, definitions) {
179
+ let meta;
180
+ meta = this._onExecutableDocumentNode({
181
+ kind: graphql_1.Kind.DOCUMENT,
182
+ definitions,
183
+ });
184
+ const deferredFields = this._findDeferredFields(node);
185
+ if (Object.keys(deferredFields).length) {
186
+ meta = {
187
+ ...meta,
188
+ deferredFields,
189
+ };
190
+ }
191
+ return meta;
192
+ }
193
+ _findDeferredFields(node) {
194
+ const deferredFields = {};
195
+ const queue = [...node.selectionSet.selections];
196
+ while (queue.length) {
197
+ const selection = queue.shift();
198
+ if (selection.kind === graphql_1.Kind.FRAGMENT_SPREAD &&
199
+ selection.directives.some((d) => d.name.value === 'defer')) {
200
+ const fragmentName = selection.name.value;
201
+ const fragment = this.fragmentsGraph.getNodeData(fragmentName);
202
+ if (fragment) {
203
+ const fields = fragment.node.selectionSet.selections.reduce((acc, selection) => {
204
+ if (selection.kind === graphql_1.Kind.FIELD) {
205
+ acc.push(selection.name.value);
206
+ }
207
+ return acc;
208
+ }, []);
209
+ deferredFields[fragmentName] = fields;
210
+ }
211
+ }
212
+ else if (selection.kind === graphql_1.Kind.FIELD && selection.selectionSet) {
213
+ queue.push(...selection.selectionSet.selections);
214
+ }
215
+ }
216
+ return deferredFields;
217
+ }
185
218
  _generateFragment(fragmentDocument) {
186
219
  const name = this.getFragmentVariableName(fragmentDocument);
187
220
  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) {
20
+ transformPrimitiveFields(_schemaType, _fields, _unsetTypes) {
21
21
  throw new Error(`Please override "transformPrimitiveFields" as part of your BaseSelectionSetProcessor implementation!`);
22
22
  }
23
- transformAliasesPrimitiveFields(_schemaType, _fields) {
23
+ transformAliasesPrimitiveFields(_schemaType, _fields, _unsetTypes) {
24
24
  throw new Error(`Please override "transformAliasesPrimitiveFields" as part of your BaseSelectionSetProcessor implementation!`);
25
25
  }
26
- transformLinkFields(_fields) {
26
+ transformLinkFields(_fields, _unsetTypes) {
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) {
16
+ transformPrimitiveFields(schemaType, fields, unsetTypes) {
17
17
  if (fields.length === 0) {
18
18
  return [];
19
19
  }
@@ -23,6 +23,13 @@ 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
+ }
26
33
  if ((0, graphql_1.isEnumType)(baseType)) {
27
34
  typeToUse =
28
35
  (this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '') +
@@ -31,7 +38,6 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
31
38
  else if (this.config.scalars[baseType.name]) {
32
39
  typeToUse = this.config.scalars[baseType.name];
33
40
  }
34
- const name = this.config.formatNamedField(field.fieldName, useInnerType ? innerType : fieldObj.type, field.isConditional);
35
41
  const wrappedType = this.config.wrapTypeWithModifiers(typeToUse, fieldObj.type);
36
42
  return {
37
43
  name,
@@ -39,7 +45,7 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
39
45
  };
40
46
  });
41
47
  }
42
- transformAliasesPrimitiveFields(schemaType, fields) {
48
+ transformAliasesPrimitiveFields(schemaType, fields, unsetTypes) {
43
49
  if (fields.length === 0) {
44
50
  return [];
45
51
  }
@@ -59,7 +65,13 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
59
65
  (this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '') +
60
66
  this.config.convertName(baseType.name, { useTypesPrefix: this.config.enumPrefix });
61
67
  }
62
- const name = this.config.formatNamedField(aliasedField.alias, fieldObj.type);
68
+ const name = this.config.formatNamedField(aliasedField.alias, fieldObj.type, undefined, unsetTypes);
69
+ if (unsetTypes) {
70
+ return {
71
+ type: 'never',
72
+ name,
73
+ };
74
+ }
63
75
  const wrappedType = this.config.wrapTypeWithModifiers(typeToUse, fieldObj.type);
64
76
  return {
65
77
  name,
@@ -67,13 +79,13 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
67
79
  };
68
80
  });
69
81
  }
70
- transformLinkFields(fields) {
82
+ transformLinkFields(fields, unsetTypes) {
71
83
  if (fields.length === 0) {
72
84
  return [];
73
85
  }
74
86
  return fields.map(field => ({
75
87
  name: field.alias || field.name,
76
- type: field.selectionSet,
88
+ type: unsetTypes ? 'never' : field.selectionSet,
77
89
  }));
78
90
  }
79
91
  }
@@ -48,8 +48,17 @@ 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
+ }));
51
60
  if ((0, graphql_1.isObjectType)(typeOnSchema)) {
52
- this._appendToTypeMap(types, typeOnSchema.name, fields);
61
+ this._appendToTypeMap(types, typeOnSchema.name, fieldsWithFragmentDirectives);
53
62
  this._appendToTypeMap(types, typeOnSchema.name, spreadsUsage[typeOnSchema.name]);
54
63
  this._appendToTypeMap(types, typeOnSchema.name, directives);
55
64
  this._collectInlineFragments(typeOnSchema, inlines, types);
@@ -160,6 +169,7 @@ class SelectionSetToObject {
160
169
  typeName: usage,
161
170
  onType: fragmentSpreadObject.onType,
162
171
  selectionNodes: [...fragmentSpreadObject.node.selectionSet.selections],
172
+ fragmentDirectives: [...spread.directives],
163
173
  });
164
174
  }
165
175
  }
@@ -221,8 +231,17 @@ class SelectionSetToObject {
221
231
  if (!(0, graphql_1.isObjectType)(schemaType)) {
222
232
  throw new TypeError(`Invalid state! Schema type ${typeName} is not a valid GraphQL object!`);
223
233
  }
224
- const selectionNodes = selectionNodesByTypeName.get(typeName) || [];
234
+ const allNodes = selectionNodesByTypeName.get(typeName) || [];
225
235
  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: [] });
226
245
  const { fields } = this.buildSelectionSet(schemaType, selectionNodes);
227
246
  const transformedSet = this.selectionSetStringFromFields(fields);
228
247
  if (transformedSet) {
@@ -231,6 +250,22 @@ class SelectionSetToObject {
231
250
  else {
232
251
  mustAddEmptyObject = true;
233
252
  }
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
+ }
234
269
  return prev;
235
270
  }, {});
236
271
  return { grouped, mustAddEmptyObject };
@@ -303,7 +338,7 @@ class SelectionSetToObject {
303
338
  const transformedSet = this._processor.buildSelectionSetFromStrings([...allStrings, mergedObjects].filter(Boolean));
304
339
  return transformedSet;
305
340
  }
306
- buildSelectionSet(parentSchemaType, selectionNodes) {
341
+ buildSelectionSet(parentSchemaType, selectionNodes, options) {
307
342
  var _a, _b, _c;
308
343
  const primitiveFields = new Map();
309
344
  const primitiveAliasFields = new Map();
@@ -394,9 +429,10 @@ class SelectionSetToObject {
394
429
  const realSelectedFieldType = (0, plugin_helpers_1.getBaseType)(selectedFieldType);
395
430
  const selectionSet = this.createNext(realSelectedFieldType, field.selectionSet);
396
431
  const isConditional = (0, utils_js_1.hasConditionalDirectives)(field) || inlineFragmentConditional;
432
+ const isOptional = options === null || options === void 0 ? void 0 : options.unsetTypes;
397
433
  linkFields.push({
398
434
  alias: field.alias ? this._processor.config.formatNamedField(field.alias.value, selectedFieldType) : undefined,
399
- name: this._processor.config.formatNamedField(field.name.value, selectedFieldType, isConditional),
435
+ name: this._processor.config.formatNamedField(field.name.value, selectedFieldType, isConditional, isOptional),
400
436
  type: realSelectedFieldType.name,
401
437
  selectionSet: this._processor.config.wrapTypeWithModifiers(selectionSet.transformSelectionSet().split(`\n`).join(`\n `), selectedFieldType),
402
438
  });
@@ -412,12 +448,12 @@ class SelectionSetToObject {
412
448
  ...this._processor.transformPrimitiveFields(parentSchemaType, Array.from(primitiveFields.values()).map(field => ({
413
449
  isConditional: (0, utils_js_1.hasConditionalDirectives)(field),
414
450
  fieldName: field.name.value,
415
- }))),
451
+ })), options === null || options === void 0 ? void 0 : options.unsetTypes),
416
452
  ...this._processor.transformAliasesPrimitiveFields(parentSchemaType, Array.from(primitiveAliasFields.values()).map(field => ({
417
453
  alias: field.alias.value,
418
454
  fieldName: field.name.value,
419
- }))),
420
- ...this._processor.transformLinkFields(linkFields),
455
+ })), options === null || options === void 0 ? void 0 : options.unsetTypes),
456
+ ...this._processor.transformLinkFields(linkFields, options === null || options === void 0 ? void 0 : options.unsetTypes),
421
457
  ].filter(Boolean);
422
458
  const allStrings = transformed.filter(t => typeof t === 'string');
423
459
  const allObjectsMerged = transformed
@@ -433,7 +469,9 @@ class SelectionSetToObject {
433
469
  fields.push(...fragmentsSpreadUsages);
434
470
  }
435
471
  else if (this._config.inlineFragmentTypes === 'mask') {
436
- fields.push(`{ ' $fragmentRefs'?: { ${fragmentsSpreadUsages.map(name => `'${name}': ${name}`).join(`;`)} } }`);
472
+ fields.push(`{ ' $fragmentRefs'?: { ${fragmentsSpreadUsages
473
+ .map(name => `'${name}': ${(options === null || options === void 0 ? void 0 : options.unsetTypes) ? `Incremental<${name}>` : name}`)
474
+ .join(`;`)} } }`);
437
475
  }
438
476
  }
439
477
  return { typeInfo: typeInfoField, fields };
@@ -471,14 +509,19 @@ class SelectionSetToObject {
471
509
  }
472
510
  return (Object.keys(grouped)
473
511
  .map(typeName => {
512
+ const hasUnions = grouped[typeName].filter(s => typeof s !== 'string' && s.union).length > 0;
474
513
  const relevant = grouped[typeName].filter(Boolean);
475
514
  if (relevant.length === 0) {
476
515
  return null;
477
516
  }
478
- if (relevant.length === 1) {
479
- return relevant[0];
480
- }
481
- return `( ${relevant.join(' & ')} )`;
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;
482
525
  })
483
526
  .filter(Boolean)
484
527
  .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.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.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;
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,6 +342,11 @@ 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;
345
350
  function wrapTypeWithModifiers(baseType, type, options) {
346
351
  let currentType = type;
347
352
  const modifiers = [];
@@ -146,38 +146,71 @@ export class ClientSideBaseVisitor extends BaseVisitor {
146
146
  }
147
147
  let metaString = '';
148
148
  if (this._onExecutableDocumentNode && node.kind === Kind.OPERATION_DEFINITION) {
149
- const meta = this._onExecutableDocumentNode({
150
- kind: Kind.DOCUMENT,
151
- definitions,
152
- });
149
+ const meta = this._getGraphQLCodegenMetadata(node, definitions);
153
150
  if (meta) {
154
- metaString = `"__meta__":${JSON.stringify(meta)},`;
155
151
  if (this._omitDefinitions === true) {
156
- return `{${metaString.slice(0, -1)}}`;
152
+ return `{${`"__meta__":${JSON.stringify(meta)},`.slice(0, -1)}}`;
157
153
  }
154
+ metaString = `"__meta__":${JSON.stringify(meta)},`;
158
155
  }
159
156
  }
160
157
  return `{${metaString}"kind":"${Kind.DOCUMENT}","definitions":${jsonStringify(definitions)}}`;
161
158
  }
162
159
  if (this.config.documentMode === DocumentMode.string) {
163
- let meta;
164
160
  if (this._onExecutableDocumentNode && node.kind === Kind.OPERATION_DEFINITION) {
165
- meta = this._onExecutableDocumentNode({
166
- kind: Kind.DOCUMENT,
167
- definitions: gqlTag([doc]).definitions,
168
- });
169
- if (meta && this._omitDefinitions === true) {
170
- return `{${`"__meta__":${JSON.stringify(meta)},`.slice(0, -1)}}`;
161
+ const meta = this._getGraphQLCodegenMetadata(node, gqlTag([doc]).definitions);
162
+ if (meta) {
163
+ if (this._omitDefinitions === true) {
164
+ return `{${`"__meta__":${JSON.stringify(meta)},`.slice(0, -1)}}`;
165
+ }
166
+ return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify(meta)})`;
171
167
  }
172
168
  }
173
- if (meta) {
174
- return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify(meta)})`;
175
- }
176
169
  return `new TypedDocumentString(\`${doc}\`)`;
177
170
  }
178
171
  const gqlImport = this._parseImport(this.config.gqlImport || 'graphql-tag');
179
172
  return (gqlImport.propName || 'gql') + '`' + doc + '`';
180
173
  }
174
+ _getGraphQLCodegenMetadata(node, definitions) {
175
+ let meta;
176
+ meta = this._onExecutableDocumentNode({
177
+ kind: Kind.DOCUMENT,
178
+ definitions,
179
+ });
180
+ const deferredFields = this._findDeferredFields(node);
181
+ if (Object.keys(deferredFields).length) {
182
+ meta = {
183
+ ...meta,
184
+ deferredFields,
185
+ };
186
+ }
187
+ return meta;
188
+ }
189
+ _findDeferredFields(node) {
190
+ const deferredFields = {};
191
+ const queue = [...node.selectionSet.selections];
192
+ while (queue.length) {
193
+ const selection = queue.shift();
194
+ if (selection.kind === Kind.FRAGMENT_SPREAD &&
195
+ selection.directives.some((d) => d.name.value === 'defer')) {
196
+ const fragmentName = selection.name.value;
197
+ const fragment = this.fragmentsGraph.getNodeData(fragmentName);
198
+ if (fragment) {
199
+ const fields = fragment.node.selectionSet.selections.reduce((acc, selection) => {
200
+ if (selection.kind === Kind.FIELD) {
201
+ acc.push(selection.name.value);
202
+ }
203
+ return acc;
204
+ }, []);
205
+ deferredFields[fragmentName] = fields;
206
+ }
207
+ }
208
+ else if (selection.kind === Kind.FIELD && selection.selectionSet) {
209
+ queue.push(...selection.selectionSet.selections);
210
+ }
211
+ }
212
+ return deferredFields;
213
+ }
181
214
  _generateFragment(fragmentDocument) {
182
215
  const name = this.getFragmentVariableName(fragmentDocument);
183
216
  const fragmentTypeSuffix = this.getFragmentSuffix(fragmentDocument);
@@ -14,13 +14,13 @@ export class BaseSelectionSetProcessor {
14
14
  }
15
15
  return `(\n ${pieces.join(`\n & `)}\n)`;
16
16
  }
17
- transformPrimitiveFields(_schemaType, _fields) {
17
+ transformPrimitiveFields(_schemaType, _fields, _unsetTypes) {
18
18
  throw new Error(`Please override "transformPrimitiveFields" as part of your BaseSelectionSetProcessor implementation!`);
19
19
  }
20
- transformAliasesPrimitiveFields(_schemaType, _fields) {
20
+ transformAliasesPrimitiveFields(_schemaType, _fields, _unsetTypes) {
21
21
  throw new Error(`Please override "transformAliasesPrimitiveFields" as part of your BaseSelectionSetProcessor implementation!`);
22
22
  }
23
- transformLinkFields(_fields) {
23
+ transformLinkFields(_fields, _unsetTypes) {
24
24
  throw new Error(`Please override "transformLinkFields" as part of your BaseSelectionSetProcessor implementation!`);
25
25
  }
26
26
  transformTypenameField(_type, _name) {
@@ -10,7 +10,7 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor {
10
10
  },
11
11
  ];
12
12
  }
13
- transformPrimitiveFields(schemaType, fields) {
13
+ transformPrimitiveFields(schemaType, fields, unsetTypes) {
14
14
  if (fields.length === 0) {
15
15
  return [];
16
16
  }
@@ -20,6 +20,13 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor {
20
20
  let typeToUse = baseType.name;
21
21
  const useInnerType = field.isConditional && isNonNullType(fieldObj.type);
22
22
  const innerType = useInnerType ? removeNonNullWrapper(fieldObj.type) : undefined;
23
+ const name = this.config.formatNamedField(field.fieldName, useInnerType ? innerType : fieldObj.type, field.isConditional, unsetTypes);
24
+ if (unsetTypes) {
25
+ return {
26
+ name,
27
+ type: 'never',
28
+ };
29
+ }
23
30
  if (isEnumType(baseType)) {
24
31
  typeToUse =
25
32
  (this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '') +
@@ -28,7 +35,6 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor {
28
35
  else if (this.config.scalars[baseType.name]) {
29
36
  typeToUse = this.config.scalars[baseType.name];
30
37
  }
31
- const name = this.config.formatNamedField(field.fieldName, useInnerType ? innerType : fieldObj.type, field.isConditional);
32
38
  const wrappedType = this.config.wrapTypeWithModifiers(typeToUse, fieldObj.type);
33
39
  return {
34
40
  name,
@@ -36,7 +42,7 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor {
36
42
  };
37
43
  });
38
44
  }
39
- transformAliasesPrimitiveFields(schemaType, fields) {
45
+ transformAliasesPrimitiveFields(schemaType, fields, unsetTypes) {
40
46
  if (fields.length === 0) {
41
47
  return [];
42
48
  }
@@ -56,7 +62,13 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor {
56
62
  (this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '') +
57
63
  this.config.convertName(baseType.name, { useTypesPrefix: this.config.enumPrefix });
58
64
  }
59
- const name = this.config.formatNamedField(aliasedField.alias, fieldObj.type);
65
+ const name = this.config.formatNamedField(aliasedField.alias, fieldObj.type, undefined, unsetTypes);
66
+ if (unsetTypes) {
67
+ return {
68
+ type: 'never',
69
+ name,
70
+ };
71
+ }
60
72
  const wrappedType = this.config.wrapTypeWithModifiers(typeToUse, fieldObj.type);
61
73
  return {
62
74
  name,
@@ -64,13 +76,13 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor {
64
76
  };
65
77
  });
66
78
  }
67
- transformLinkFields(fields) {
79
+ transformLinkFields(fields, unsetTypes) {
68
80
  if (fields.length === 0) {
69
81
  return [];
70
82
  }
71
83
  return fields.map(field => ({
72
84
  name: field.alias || field.name,
73
- type: field.selectionSet,
85
+ type: unsetTypes ? 'never' : field.selectionSet,
74
86
  }));
75
87
  }
76
88
  }
@@ -3,7 +3,7 @@ import { getBaseType } from '@graphql-codegen/plugin-helpers';
3
3
  import { getRootTypes } from '@graphql-tools/utils';
4
4
  import autoBind from 'auto-bind';
5
5
  import { isInterfaceType, isListType, isNonNullType, isObjectType, isTypeSubTypeOf, isUnionType, Kind, SchemaMetaFieldDef, TypeMetaFieldDef, } from 'graphql';
6
- import { DeclarationBlock, getFieldNodeNameValue, getPossibleTypes, hasConditionalDirectives, mergeSelectionSets, separateSelectionSet, } from './utils.js';
6
+ import { DeclarationBlock, getFieldNodeNameValue, getPossibleTypes, hasConditionalDirectives, hasIncrementalDeliveryDirectives, mergeSelectionSets, separateSelectionSet, } from './utils.js';
7
7
  function isMetadataFieldName(name) {
8
8
  return ['__schema', '__type'].includes(name);
9
9
  }
@@ -44,8 +44,17 @@ export class SelectionSetToObject {
44
44
  const { fields, inlines, spreads } = separateSelectionSet(node.selectionSet.selections);
45
45
  const spreadsUsage = this.buildFragmentSpreadsUsage(spreads);
46
46
  const directives = node.directives || undefined;
47
+ // When we collect the selection sets of inline fragments we need to
48
+ // make sure directives on the inline fragments are stored in a way
49
+ // that can be associated back to the fields in the fragment, to
50
+ // support things like making those fields optional when deferring a
51
+ // fragment (using @defer).
52
+ const fieldsWithFragmentDirectives = fields.map(field => ({
53
+ ...field,
54
+ fragmentDirectives: field.fragmentDirectives || directives,
55
+ }));
47
56
  if (isObjectType(typeOnSchema)) {
48
- this._appendToTypeMap(types, typeOnSchema.name, fields);
57
+ this._appendToTypeMap(types, typeOnSchema.name, fieldsWithFragmentDirectives);
49
58
  this._appendToTypeMap(types, typeOnSchema.name, spreadsUsage[typeOnSchema.name]);
50
59
  this._appendToTypeMap(types, typeOnSchema.name, directives);
51
60
  this._collectInlineFragments(typeOnSchema, inlines, types);
@@ -156,6 +165,7 @@ export class SelectionSetToObject {
156
165
  typeName: usage,
157
166
  onType: fragmentSpreadObject.onType,
158
167
  selectionNodes: [...fragmentSpreadObject.node.selectionSet.selections],
168
+ fragmentDirectives: [...spread.directives],
159
169
  });
160
170
  }
161
171
  }
@@ -217,8 +227,17 @@ export class SelectionSetToObject {
217
227
  if (!isObjectType(schemaType)) {
218
228
  throw new TypeError(`Invalid state! Schema type ${typeName} is not a valid GraphQL object!`);
219
229
  }
220
- const selectionNodes = selectionNodesByTypeName.get(typeName) || [];
230
+ const allNodes = selectionNodesByTypeName.get(typeName) || [];
221
231
  prev[typeName] || (prev[typeName] = []);
232
+ const { incrementalNodes, selectionNodes } = allNodes.reduce((acc, node) => {
233
+ if ('fragmentDirectives' in node && hasIncrementalDeliveryDirectives(node.fragmentDirectives)) {
234
+ acc.incrementalNodes.push(node);
235
+ }
236
+ else {
237
+ acc.selectionNodes.push(node);
238
+ }
239
+ return acc;
240
+ }, { selectionNodes: [], incrementalNodes: [] });
222
241
  const { fields } = this.buildSelectionSet(schemaType, selectionNodes);
223
242
  const transformedSet = this.selectionSetStringFromFields(fields);
224
243
  if (transformedSet) {
@@ -227,6 +246,22 @@ export class SelectionSetToObject {
227
246
  else {
228
247
  mustAddEmptyObject = true;
229
248
  }
249
+ for (const incrementalNode of incrementalNodes) {
250
+ if (this._config.inlineFragmentTypes === 'mask' && 'fragmentName' in incrementalNode) {
251
+ const { fields: incrementalFields } = this.buildSelectionSet(schemaType, [incrementalNode], {
252
+ unsetTypes: true,
253
+ });
254
+ prev[typeName].push(this.selectionSetStringFromFields(incrementalFields));
255
+ continue;
256
+ }
257
+ const { fields: initialFields } = this.buildSelectionSet(schemaType, [incrementalNode]);
258
+ const { fields: subsequentFields } = this.buildSelectionSet(schemaType, [incrementalNode], {
259
+ unsetTypes: true,
260
+ });
261
+ const initialSet = this.selectionSetStringFromFields(initialFields);
262
+ const subsequentSet = this.selectionSetStringFromFields(subsequentFields);
263
+ prev[typeName].push({ union: [initialSet, subsequentSet] });
264
+ }
230
265
  return prev;
231
266
  }, {});
232
267
  return { grouped, mustAddEmptyObject };
@@ -299,7 +334,7 @@ export class SelectionSetToObject {
299
334
  const transformedSet = this._processor.buildSelectionSetFromStrings([...allStrings, mergedObjects].filter(Boolean));
300
335
  return transformedSet;
301
336
  }
302
- buildSelectionSet(parentSchemaType, selectionNodes) {
337
+ buildSelectionSet(parentSchemaType, selectionNodes, options) {
303
338
  var _a, _b, _c;
304
339
  const primitiveFields = new Map();
305
340
  const primitiveAliasFields = new Map();
@@ -390,9 +425,10 @@ export class SelectionSetToObject {
390
425
  const realSelectedFieldType = getBaseType(selectedFieldType);
391
426
  const selectionSet = this.createNext(realSelectedFieldType, field.selectionSet);
392
427
  const isConditional = hasConditionalDirectives(field) || inlineFragmentConditional;
428
+ const isOptional = options === null || options === void 0 ? void 0 : options.unsetTypes;
393
429
  linkFields.push({
394
430
  alias: field.alias ? this._processor.config.formatNamedField(field.alias.value, selectedFieldType) : undefined,
395
- name: this._processor.config.formatNamedField(field.name.value, selectedFieldType, isConditional),
431
+ name: this._processor.config.formatNamedField(field.name.value, selectedFieldType, isConditional, isOptional),
396
432
  type: realSelectedFieldType.name,
397
433
  selectionSet: this._processor.config.wrapTypeWithModifiers(selectionSet.transformSelectionSet().split(`\n`).join(`\n `), selectedFieldType),
398
434
  });
@@ -408,12 +444,12 @@ export class SelectionSetToObject {
408
444
  ...this._processor.transformPrimitiveFields(parentSchemaType, Array.from(primitiveFields.values()).map(field => ({
409
445
  isConditional: hasConditionalDirectives(field),
410
446
  fieldName: field.name.value,
411
- }))),
447
+ })), options === null || options === void 0 ? void 0 : options.unsetTypes),
412
448
  ...this._processor.transformAliasesPrimitiveFields(parentSchemaType, Array.from(primitiveAliasFields.values()).map(field => ({
413
449
  alias: field.alias.value,
414
450
  fieldName: field.name.value,
415
- }))),
416
- ...this._processor.transformLinkFields(linkFields),
451
+ })), options === null || options === void 0 ? void 0 : options.unsetTypes),
452
+ ...this._processor.transformLinkFields(linkFields, options === null || options === void 0 ? void 0 : options.unsetTypes),
417
453
  ].filter(Boolean);
418
454
  const allStrings = transformed.filter(t => typeof t === 'string');
419
455
  const allObjectsMerged = transformed
@@ -429,7 +465,9 @@ export class SelectionSetToObject {
429
465
  fields.push(...fragmentsSpreadUsages);
430
466
  }
431
467
  else if (this._config.inlineFragmentTypes === 'mask') {
432
- fields.push(`{ ' $fragmentRefs'?: { ${fragmentsSpreadUsages.map(name => `'${name}': ${name}`).join(`;`)} } }`);
468
+ fields.push(`{ ' $fragmentRefs'?: { ${fragmentsSpreadUsages
469
+ .map(name => `'${name}': ${(options === null || options === void 0 ? void 0 : options.unsetTypes) ? `Incremental<${name}>` : name}`)
470
+ .join(`;`)} } }`);
433
471
  }
434
472
  }
435
473
  return { typeInfo: typeInfoField, fields };
@@ -467,14 +505,19 @@ export class SelectionSetToObject {
467
505
  }
468
506
  return (Object.keys(grouped)
469
507
  .map(typeName => {
508
+ const hasUnions = grouped[typeName].filter(s => typeof s !== 'string' && s.union).length > 0;
470
509
  const relevant = grouped[typeName].filter(Boolean);
471
510
  if (relevant.length === 0) {
472
511
  return null;
473
512
  }
474
- if (relevant.length === 1) {
475
- return relevant[0];
476
- }
477
- return `( ${relevant.join(' & ')} )`;
513
+ const res = relevant
514
+ .map(selectionObject => {
515
+ if (typeof selectionObject === 'string')
516
+ return selectionObject;
517
+ return '(' + selectionObject.union.join(' | ') + ')';
518
+ })
519
+ .join(' & ');
520
+ return relevant.length > 1 && !hasUnions ? `( ${res} )` : res;
478
521
  })
479
522
  .filter(Boolean)
480
523
  .join(' | ') + this.getEmptyObjectTypeString(mustAddEmptyObject));
package/esm/utils.js CHANGED
@@ -319,6 +319,10 @@ export function hasConditionalDirectives(field) {
319
319
  const CONDITIONAL_DIRECTIVES = ['skip', 'include'];
320
320
  return (_a = field.directives) === null || _a === void 0 ? void 0 : _a.some(directive => CONDITIONAL_DIRECTIVES.includes(directive.name.value));
321
321
  }
322
+ export function hasIncrementalDeliveryDirectives(directives) {
323
+ const INCREMENTAL_DELIVERY_DIRECTIVES = ['defer'];
324
+ return directives === null || directives === void 0 ? void 0 : directives.some(directive => INCREMENTAL_DELIVERY_DIRECTIVES.includes(directive.name.value));
325
+ }
322
326
  export function wrapTypeWithModifiers(baseType, type, options) {
323
327
  let currentType = type;
324
328
  const modifiers = [];
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@graphql-codegen/visitor-plugin-common",
3
- "version": "3.1.0-alpha-20230329092554-66cdc8d69",
3
+ "version": "3.1.0-alpha-20230329100531-e8e2561d7",
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
  },
7
7
  "dependencies": {
8
8
  "@graphql-tools/optimize": "^1.3.0",
9
- "@graphql-codegen/plugin-helpers": "4.2.0-alpha-20230329092554-66cdc8d69",
9
+ "@graphql-codegen/plugin-helpers": "4.2.0-alpha-20230329100531-e8e2561d7",
10
10
  "@graphql-tools/relay-operation-optimizer": "^6.5.0",
11
11
  "@graphql-tools/utils": "^9.0.0",
12
12
  "auto-bind": "~4.0.0",
@@ -1,5 +1,5 @@
1
1
  import { Types } from '@graphql-codegen/plugin-helpers';
2
- import { DocumentNode, FragmentDefinitionNode, GraphQLSchema, OperationDefinitionNode } from 'graphql';
2
+ import { DefinitionNode, DocumentNode, FragmentDefinitionNode, GraphQLSchema, OperationDefinitionNode } from 'graphql';
3
3
  import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor.cjs';
4
4
  import { LoadedFragment, ParsedImport } from './types.cjs';
5
5
  export declare enum DocumentMode {
@@ -226,6 +226,10 @@ export declare class ClientSideBaseVisitor<TRawConfig extends RawClientSideBaseP
226
226
  protected _includeFragments(fragments: string[], nodeKind: 'FragmentDefinition' | 'OperationDefinition'): string;
227
227
  protected _prepareDocument(documentStr: string): string;
228
228
  protected _gql(node: FragmentDefinitionNode | OperationDefinitionNode): string;
229
+ protected _getGraphQLCodegenMetadata(node: OperationDefinitionNode, definitions?: ReadonlyArray<DefinitionNode>): Record<string, any> | void | undefined;
230
+ protected _findDeferredFields(node: OperationDefinitionNode): {
231
+ [fargmentName: string]: string[];
232
+ };
229
233
  protected _generateFragment(fragmentDocument: FragmentDefinitionNode): string | void;
230
234
  private get fragmentsGraph();
231
235
  get fragments(): string;
@@ -1,5 +1,5 @@
1
1
  import { Types } from '@graphql-codegen/plugin-helpers';
2
- import { DocumentNode, FragmentDefinitionNode, GraphQLSchema, OperationDefinitionNode } from 'graphql';
2
+ import { DefinitionNode, DocumentNode, FragmentDefinitionNode, GraphQLSchema, OperationDefinitionNode } from 'graphql';
3
3
  import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor.js';
4
4
  import { LoadedFragment, ParsedImport } from './types.js';
5
5
  export declare enum DocumentMode {
@@ -226,6 +226,10 @@ export declare class ClientSideBaseVisitor<TRawConfig extends RawClientSideBaseP
226
226
  protected _includeFragments(fragments: string[], nodeKind: 'FragmentDefinition' | 'OperationDefinition'): string;
227
227
  protected _prepareDocument(documentStr: string): string;
228
228
  protected _gql(node: FragmentDefinitionNode | OperationDefinitionNode): string;
229
+ protected _getGraphQLCodegenMetadata(node: OperationDefinitionNode, definitions?: ReadonlyArray<DefinitionNode>): Record<string, any> | void | undefined;
230
+ protected _findDeferredFields(node: OperationDefinitionNode): {
231
+ [fargmentName: string]: string[];
232
+ };
229
233
  protected _generateFragment(fragmentDocument: FragmentDefinitionNode): string | void;
230
234
  private get fragmentsGraph();
231
235
  get fragments(): string;
@@ -24,7 +24,7 @@ export type SelectionSetProcessorConfig = {
24
24
  convertName: ConvertNameFn<any>;
25
25
  enumPrefix: boolean | null;
26
26
  scalars: ScalarsMap;
27
- formatNamedField(name: string, type?: GraphQLOutputType | GraphQLNamedType | null, isConditional?: boolean): string;
27
+ formatNamedField(name: string, type?: GraphQLOutputType | GraphQLNamedType | null, isConditional?: boolean, isOptional?: boolean): string;
28
28
  wrapTypeWithModifiers(baseType: string, type: GraphQLOutputType | GraphQLNamedType): string;
29
29
  avoidOptionals?: AvoidOptionalsConfig | boolean;
30
30
  };
@@ -33,8 +33,8 @@ export declare class BaseSelectionSetProcessor<Config extends SelectionSetProces
33
33
  constructor(config: Config);
34
34
  buildFieldsIntoObject(allObjectsMerged: string[]): string;
35
35
  buildSelectionSetFromStrings(pieces: string[]): string;
36
- transformPrimitiveFields(_schemaType: GraphQLObjectType | GraphQLInterfaceType, _fields: PrimitiveField[]): ProcessResult;
37
- transformAliasesPrimitiveFields(_schemaType: GraphQLObjectType | GraphQLInterfaceType, _fields: PrimitiveAliasedFields[]): ProcessResult;
38
- transformLinkFields(_fields: LinkField[]): ProcessResult;
36
+ transformPrimitiveFields(_schemaType: GraphQLObjectType | GraphQLInterfaceType, _fields: PrimitiveField[], _unsetTypes?: boolean): ProcessResult;
37
+ transformAliasesPrimitiveFields(_schemaType: GraphQLObjectType | GraphQLInterfaceType, _fields: PrimitiveAliasedFields[], _unsetTypes?: boolean): ProcessResult;
38
+ transformLinkFields(_fields: LinkField[], _unsetTypes?: boolean): ProcessResult;
39
39
  transformTypenameField(_type: string, _name: string): ProcessResult;
40
40
  }
@@ -24,7 +24,7 @@ export type SelectionSetProcessorConfig = {
24
24
  convertName: ConvertNameFn<any>;
25
25
  enumPrefix: boolean | null;
26
26
  scalars: ScalarsMap;
27
- formatNamedField(name: string, type?: GraphQLOutputType | GraphQLNamedType | null, isConditional?: boolean): string;
27
+ formatNamedField(name: string, type?: GraphQLOutputType | GraphQLNamedType | null, isConditional?: boolean, isOptional?: boolean): string;
28
28
  wrapTypeWithModifiers(baseType: string, type: GraphQLOutputType | GraphQLNamedType): string;
29
29
  avoidOptionals?: AvoidOptionalsConfig | boolean;
30
30
  };
@@ -33,8 +33,8 @@ export declare class BaseSelectionSetProcessor<Config extends SelectionSetProces
33
33
  constructor(config: Config);
34
34
  buildFieldsIntoObject(allObjectsMerged: string[]): string;
35
35
  buildSelectionSetFromStrings(pieces: string[]): string;
36
- transformPrimitiveFields(_schemaType: GraphQLObjectType | GraphQLInterfaceType, _fields: PrimitiveField[]): ProcessResult;
37
- transformAliasesPrimitiveFields(_schemaType: GraphQLObjectType | GraphQLInterfaceType, _fields: PrimitiveAliasedFields[]): ProcessResult;
38
- transformLinkFields(_fields: LinkField[]): ProcessResult;
36
+ transformPrimitiveFields(_schemaType: GraphQLObjectType | GraphQLInterfaceType, _fields: PrimitiveField[], _unsetTypes?: boolean): ProcessResult;
37
+ transformAliasesPrimitiveFields(_schemaType: GraphQLObjectType | GraphQLInterfaceType, _fields: PrimitiveAliasedFields[], _unsetTypes?: boolean): ProcessResult;
38
+ transformLinkFields(_fields: LinkField[], _unsetTypes?: boolean): ProcessResult;
39
39
  transformTypenameField(_type: string, _name: string): ProcessResult;
40
40
  }
@@ -2,7 +2,7 @@ import { GraphQLInterfaceType, GraphQLObjectType } from 'graphql';
2
2
  import { BaseSelectionSetProcessor, LinkField, PrimitiveAliasedFields, PrimitiveField, ProcessResult, SelectionSetProcessorConfig } from './base.cjs';
3
3
  export declare class PreResolveTypesProcessor extends BaseSelectionSetProcessor<SelectionSetProcessorConfig> {
4
4
  transformTypenameField(type: string, name: string): ProcessResult;
5
- transformPrimitiveFields(schemaType: GraphQLObjectType | GraphQLInterfaceType, fields: PrimitiveField[]): ProcessResult;
6
- transformAliasesPrimitiveFields(schemaType: GraphQLObjectType | GraphQLInterfaceType, fields: PrimitiveAliasedFields[]): ProcessResult;
7
- transformLinkFields(fields: LinkField[]): ProcessResult;
5
+ transformPrimitiveFields(schemaType: GraphQLObjectType | GraphQLInterfaceType, fields: PrimitiveField[], unsetTypes?: boolean): ProcessResult;
6
+ transformAliasesPrimitiveFields(schemaType: GraphQLObjectType | GraphQLInterfaceType, fields: PrimitiveAliasedFields[], unsetTypes?: boolean): ProcessResult;
7
+ transformLinkFields(fields: LinkField[], unsetTypes?: boolean): ProcessResult;
8
8
  }
@@ -2,7 +2,7 @@ import { GraphQLInterfaceType, GraphQLObjectType } from 'graphql';
2
2
  import { BaseSelectionSetProcessor, LinkField, PrimitiveAliasedFields, PrimitiveField, ProcessResult, SelectionSetProcessorConfig } from './base.js';
3
3
  export declare class PreResolveTypesProcessor extends BaseSelectionSetProcessor<SelectionSetProcessorConfig> {
4
4
  transformTypenameField(type: string, name: string): ProcessResult;
5
- transformPrimitiveFields(schemaType: GraphQLObjectType | GraphQLInterfaceType, fields: PrimitiveField[]): ProcessResult;
6
- transformAliasesPrimitiveFields(schemaType: GraphQLObjectType | GraphQLInterfaceType, fields: PrimitiveAliasedFields[]): ProcessResult;
7
- transformLinkFields(fields: LinkField[]): ProcessResult;
5
+ transformPrimitiveFields(schemaType: GraphQLObjectType | GraphQLInterfaceType, fields: PrimitiveField[], unsetTypes?: boolean): ProcessResult;
6
+ transformAliasesPrimitiveFields(schemaType: GraphQLObjectType | GraphQLInterfaceType, fields: PrimitiveAliasedFields[], unsetTypes?: boolean): ProcessResult;
7
+ transformLinkFields(fields: LinkField[], unsetTypes?: boolean): ProcessResult;
8
8
  }
@@ -2,13 +2,18 @@ import { DirectiveNode, FieldNode, FragmentSpreadNode, GraphQLNamedType, GraphQL
2
2
  import { ParsedDocumentsConfig } from './base-documents-visitor.cjs';
3
3
  import { BaseVisitorConvertOptions } from './base-visitor.cjs';
4
4
  import { BaseSelectionSetProcessor, LinkField, NameAndType, PrimitiveAliasedFields, PrimitiveField } from './selection-set-processor/base.cjs';
5
- import { ConvertNameFn, GetFragmentSuffixFn, LoadedFragment, NormalizedScalarsMap } from './types.cjs';
5
+ import { ConvertNameFn, FragmentDirectives, GetFragmentSuffixFn, LoadedFragment, NormalizedScalarsMap } from './types.cjs';
6
6
  type FragmentSpreadUsage = {
7
7
  fragmentName: string;
8
8
  typeName: string;
9
9
  onType: string;
10
10
  selectionNodes: Array<SelectionNode>;
11
+ fragmentDirectives?: DirectiveNode[];
11
12
  };
13
+ type CollectedFragmentNode = (SelectionNode | FragmentSpreadUsage | DirectiveNode) & FragmentDirectives;
14
+ type GroupedStringifiedTypes = Record<string, Array<string | {
15
+ union: string[];
16
+ }>>;
12
17
  export declare class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedDocumentsConfig> {
13
18
  protected _processor: BaseSelectionSetProcessor<any>;
14
19
  protected _scalars: NormalizedScalarsMap;
@@ -28,7 +33,7 @@ export declare class SelectionSetToObject<Config extends ParsedDocumentsConfig =
28
33
  /**
29
34
  * traverse the inline fragment nodes recursively for collecting the selectionSets on each type
30
35
  */
31
- _collectInlineFragments(parentType: GraphQLNamedType, nodes: InlineFragmentNode[], types: Map<string, Array<SelectionNode | FragmentSpreadUsage | DirectiveNode>>): any;
36
+ _collectInlineFragments(parentType: GraphQLNamedType, nodes: Array<InlineFragmentNode & FragmentDirectives>, types: Map<string, Array<CollectedFragmentNode>>): any;
32
37
  protected _createInlineFragmentForFieldNodes(parentType: GraphQLNamedType, fieldNodes: FieldNode[]): InlineFragmentNode;
33
38
  protected buildFragmentSpreadsUsage(spreads: FragmentSpreadNode[]): Record<string, FragmentSpreadUsage[]>;
34
39
  protected flattenSelectionSet(selections: ReadonlyArray<SelectionNode>, parentSchemaType?: GraphQLObjectType<any, any>): Map<string, Array<SelectionNode | FragmentSpreadUsage>>;
@@ -37,11 +42,13 @@ export declare class SelectionSetToObject<Config extends ParsedDocumentsConfig =
37
42
  * mustAddEmptyObject indicates that not all possible types on a union or interface field are covered.
38
43
  */
39
44
  protected _buildGroupedSelections(): {
40
- grouped: Record<string, string[]>;
45
+ grouped: GroupedStringifiedTypes;
41
46
  mustAddEmptyObject: boolean;
42
47
  };
43
48
  protected selectionSetStringFromFields(fields: (string | NameAndType)[]): string | null;
44
- protected buildSelectionSet(parentSchemaType: GraphQLObjectType, selectionNodes: Array<SelectionNode | FragmentSpreadUsage | DirectiveNode>): {
49
+ protected buildSelectionSet(parentSchemaType: GraphQLObjectType, selectionNodes: Array<SelectionNode | FragmentSpreadUsage | DirectiveNode>, options?: {
50
+ unsetTypes: boolean;
51
+ }): {
45
52
  typeInfo: {
46
53
  name: string;
47
54
  type: string;
@@ -2,13 +2,18 @@ import { DirectiveNode, FieldNode, FragmentSpreadNode, GraphQLNamedType, GraphQL
2
2
  import { ParsedDocumentsConfig } from './base-documents-visitor.js';
3
3
  import { BaseVisitorConvertOptions } from './base-visitor.js';
4
4
  import { BaseSelectionSetProcessor, LinkField, NameAndType, PrimitiveAliasedFields, PrimitiveField } from './selection-set-processor/base.js';
5
- import { ConvertNameFn, GetFragmentSuffixFn, LoadedFragment, NormalizedScalarsMap } from './types.js';
5
+ import { ConvertNameFn, FragmentDirectives, GetFragmentSuffixFn, LoadedFragment, NormalizedScalarsMap } from './types.js';
6
6
  type FragmentSpreadUsage = {
7
7
  fragmentName: string;
8
8
  typeName: string;
9
9
  onType: string;
10
10
  selectionNodes: Array<SelectionNode>;
11
+ fragmentDirectives?: DirectiveNode[];
11
12
  };
13
+ type CollectedFragmentNode = (SelectionNode | FragmentSpreadUsage | DirectiveNode) & FragmentDirectives;
14
+ type GroupedStringifiedTypes = Record<string, Array<string | {
15
+ union: string[];
16
+ }>>;
12
17
  export declare class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedDocumentsConfig> {
13
18
  protected _processor: BaseSelectionSetProcessor<any>;
14
19
  protected _scalars: NormalizedScalarsMap;
@@ -28,7 +33,7 @@ export declare class SelectionSetToObject<Config extends ParsedDocumentsConfig =
28
33
  /**
29
34
  * traverse the inline fragment nodes recursively for collecting the selectionSets on each type
30
35
  */
31
- _collectInlineFragments(parentType: GraphQLNamedType, nodes: InlineFragmentNode[], types: Map<string, Array<SelectionNode | FragmentSpreadUsage | DirectiveNode>>): any;
36
+ _collectInlineFragments(parentType: GraphQLNamedType, nodes: Array<InlineFragmentNode & FragmentDirectives>, types: Map<string, Array<CollectedFragmentNode>>): any;
32
37
  protected _createInlineFragmentForFieldNodes(parentType: GraphQLNamedType, fieldNodes: FieldNode[]): InlineFragmentNode;
33
38
  protected buildFragmentSpreadsUsage(spreads: FragmentSpreadNode[]): Record<string, FragmentSpreadUsage[]>;
34
39
  protected flattenSelectionSet(selections: ReadonlyArray<SelectionNode>, parentSchemaType?: GraphQLObjectType<any, any>): Map<string, Array<SelectionNode | FragmentSpreadUsage>>;
@@ -37,11 +42,13 @@ export declare class SelectionSetToObject<Config extends ParsedDocumentsConfig =
37
42
  * mustAddEmptyObject indicates that not all possible types on a union or interface field are covered.
38
43
  */
39
44
  protected _buildGroupedSelections(): {
40
- grouped: Record<string, string[]>;
45
+ grouped: GroupedStringifiedTypes;
41
46
  mustAddEmptyObject: boolean;
42
47
  };
43
48
  protected selectionSetStringFromFields(fields: (string | NameAndType)[]): string | null;
44
- protected buildSelectionSet(parentSchemaType: GraphQLObjectType, selectionNodes: Array<SelectionNode | FragmentSpreadUsage | DirectiveNode>): {
49
+ protected buildSelectionSet(parentSchemaType: GraphQLObjectType, selectionNodes: Array<SelectionNode | FragmentSpreadUsage | DirectiveNode>, options?: {
50
+ unsetTypes: boolean;
51
+ }): {
45
52
  typeInfo: {
46
53
  name: string;
47
54
  type: string;
@@ -1,4 +1,4 @@
1
- import { ASTNode, FragmentDefinitionNode } from 'graphql';
1
+ import { ASTNode, FragmentDefinitionNode, DirectiveNode } from 'graphql';
2
2
  import { ParsedMapper } from './mappers.cjs';
3
3
  /**
4
4
  * A map between the GraphQL directive name and the identifier that should be used
@@ -99,6 +99,9 @@ export interface ParsedImport {
99
99
  moduleName: string | null;
100
100
  propName: string;
101
101
  }
102
+ export type FragmentDirectives = {
103
+ fragmentDirectives?: Array<DirectiveNode>;
104
+ };
102
105
  export interface ResolversNonOptionalTypenameConfig {
103
106
  unionMember?: boolean;
104
107
  }
@@ -1,4 +1,4 @@
1
- import { ASTNode, FragmentDefinitionNode } from 'graphql';
1
+ import { ASTNode, FragmentDefinitionNode, DirectiveNode } from 'graphql';
2
2
  import { ParsedMapper } from './mappers.js';
3
3
  /**
4
4
  * A map between the GraphQL directive name and the identifier that should be used
@@ -99,6 +99,9 @@ export interface ParsedImport {
99
99
  moduleName: string | null;
100
100
  propName: string;
101
101
  }
102
+ export type FragmentDirectives = {
103
+ fragmentDirectives?: Array<DirectiveNode>;
104
+ };
102
105
  export interface ResolversNonOptionalTypenameConfig {
103
106
  unionMember?: boolean;
104
107
  }
@@ -1,6 +1,6 @@
1
- import { FieldNode, FragmentSpreadNode, GraphQLInputObjectType, GraphQLNamedType, GraphQLObjectType, GraphQLOutputType, GraphQLSchema, InlineFragmentNode, NamedTypeNode, NameNode, SelectionNode, SelectionSetNode, StringValueNode, TypeNode } from 'graphql';
1
+ import { FieldNode, FragmentSpreadNode, GraphQLInputObjectType, GraphQLNamedType, GraphQLObjectType, GraphQLOutputType, GraphQLSchema, InlineFragmentNode, NamedTypeNode, NameNode, SelectionNode, SelectionSetNode, StringValueNode, TypeNode, DirectiveNode } from 'graphql';
2
2
  import { RawConfig } from './base-visitor.cjs';
3
- import { NormalizedScalarsMap, ParsedScalarsMap, ScalarsMap } from './types.cjs';
3
+ import { NormalizedScalarsMap, ParsedScalarsMap, ScalarsMap, FragmentDirectives } from './types.cjs';
4
4
  export declare const getConfigValue: <T = any>(value: T, defaultValue: T) => T;
5
5
  export declare function quoteIfNeeded(array: string[], joinWith?: string): string;
6
6
  export declare function block(array: any): string;
@@ -52,12 +52,13 @@ export declare const REQUIRE_FIELDS_TYPE = "export type RequireFields<T, K exten
52
52
  export declare function mergeSelectionSets(selectionSet1: SelectionSetNode, selectionSet2: SelectionSetNode): SelectionSetNode;
53
53
  export declare const getFieldNodeNameValue: (node: FieldNode) => string;
54
54
  export declare function separateSelectionSet(selections: ReadonlyArray<SelectionNode>): {
55
- fields: FieldNode[];
55
+ fields: (FieldNode & FragmentDirectives)[];
56
56
  spreads: FragmentSpreadNode[];
57
57
  inlines: InlineFragmentNode[];
58
58
  };
59
59
  export declare function getPossibleTypes(schema: GraphQLSchema, type: GraphQLNamedType): GraphQLObjectType[];
60
60
  export declare function hasConditionalDirectives(field: FieldNode): boolean;
61
+ export declare function hasIncrementalDeliveryDirectives(directives: DirectiveNode[]): boolean;
61
62
  type WrapModifiersOptions = {
62
63
  wrapOptional(type: string): string;
63
64
  wrapArray(type: string): string;
@@ -1,6 +1,6 @@
1
- import { FieldNode, FragmentSpreadNode, GraphQLInputObjectType, GraphQLNamedType, GraphQLObjectType, GraphQLOutputType, GraphQLSchema, InlineFragmentNode, NamedTypeNode, NameNode, SelectionNode, SelectionSetNode, StringValueNode, TypeNode } from 'graphql';
1
+ import { FieldNode, FragmentSpreadNode, GraphQLInputObjectType, GraphQLNamedType, GraphQLObjectType, GraphQLOutputType, GraphQLSchema, InlineFragmentNode, NamedTypeNode, NameNode, SelectionNode, SelectionSetNode, StringValueNode, TypeNode, DirectiveNode } from 'graphql';
2
2
  import { RawConfig } from './base-visitor.js';
3
- import { NormalizedScalarsMap, ParsedScalarsMap, ScalarsMap } from './types.js';
3
+ import { NormalizedScalarsMap, ParsedScalarsMap, ScalarsMap, FragmentDirectives } from './types.js';
4
4
  export declare const getConfigValue: <T = any>(value: T, defaultValue: T) => T;
5
5
  export declare function quoteIfNeeded(array: string[], joinWith?: string): string;
6
6
  export declare function block(array: any): string;
@@ -52,12 +52,13 @@ export declare const REQUIRE_FIELDS_TYPE = "export type RequireFields<T, K exten
52
52
  export declare function mergeSelectionSets(selectionSet1: SelectionSetNode, selectionSet2: SelectionSetNode): SelectionSetNode;
53
53
  export declare const getFieldNodeNameValue: (node: FieldNode) => string;
54
54
  export declare function separateSelectionSet(selections: ReadonlyArray<SelectionNode>): {
55
- fields: FieldNode[];
55
+ fields: (FieldNode & FragmentDirectives)[];
56
56
  spreads: FragmentSpreadNode[];
57
57
  inlines: InlineFragmentNode[];
58
58
  };
59
59
  export declare function getPossibleTypes(schema: GraphQLSchema, type: GraphQLNamedType): GraphQLObjectType[];
60
60
  export declare function hasConditionalDirectives(field: FieldNode): boolean;
61
+ export declare function hasIncrementalDeliveryDirectives(directives: DirectiveNode[]): boolean;
61
62
  type WrapModifiersOptions = {
62
63
  wrapOptional(type: string): string;
63
64
  wrapArray(type: string): string;