@graphql-codegen/visitor-plugin-common 3.1.0-alpha-20230320102705-ea2aff584 → 3.1.0-alpha-20230320114137-e954beaff

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.
@@ -52,9 +52,7 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
52
52
  this._resolversTypes = {};
53
53
  this._resolversParentTypes = {};
54
54
  this._hasReferencedResolversUnionTypes = false;
55
- this._hasReferencedResolversUnionParentTypes = false;
56
55
  this._resolversUnionTypes = {};
57
- this._resolversUnionParentTypes = {};
58
56
  this._rootTypeNames = new Set();
59
57
  this._globalDeclarations = new Set();
60
58
  this._hasScalars = false;
@@ -64,25 +62,9 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
64
62
  this._federation = new plugin_helpers_1.ApolloFederation({ enabled: this.config.federation, schema: this.schema });
65
63
  this._rootTypeNames = (0, utils_1.getRootTypeNames)(_schema);
66
64
  this._variablesTransformer = new variables_to_object_js_1.OperationVariablesToObject(this.scalars, this.convertName, this.config.namespacedImportName);
67
- this._resolversTypes = this.createResolversFields({
68
- applyWrapper: type => this.applyResolverTypeWrapper(type),
69
- clearWrapper: type => this.clearResolverTypeWrapper(type),
70
- getTypeToUse: name => this.getTypeToUse(name),
71
- referencedUnionType: 'ResolversUnionTypes',
72
- });
73
- this._resolversParentTypes = this.createResolversFields({
74
- applyWrapper: type => type,
75
- clearWrapper: type => type,
76
- getTypeToUse: name => this.getParentTypeToUse(name),
77
- referencedUnionType: 'ResolversUnionParentTypes',
78
- shouldInclude: namedType => !(0, graphql_1.isEnumType)(namedType),
79
- });
80
- this._resolversUnionTypes = this.createResolversUnionTypes({
81
- getTypeToUse: this.getTypeToUse,
82
- });
83
- this._resolversUnionParentTypes = this.createResolversUnionTypes({
84
- getTypeToUse: this.getParentTypeToUse,
85
- });
65
+ this._resolversTypes = this.createResolversFields(type => this.applyResolverTypeWrapper(type), type => this.clearResolverTypeWrapper(type), name => this.getTypeToUse(name));
66
+ this._resolversParentTypes = this.createResolversFields(type => type, type => type, name => this.getParentTypeToUse(name), namedType => !(0, graphql_1.isEnumType)(namedType));
67
+ this._resolversUnionTypes = this.createResolversUnionTypes();
86
68
  this._fieldContextTypeMap = this.createFieldContextTypeMap();
87
69
  this._directiveContextTypesMap = this.createDirectivedContextType();
88
70
  this._directiveResolverMappings = (_a = rawConfig.directiveResolverMappings) !== null && _a !== void 0 ? _a : {};
@@ -126,7 +108,7 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
126
108
  return `${applyNamespacedImport && this.config.namespacedImportName ? this.config.namespacedImportName + '.' : ''}${sourceType}`;
127
109
  }
128
110
  // Kamil: this one is heeeeavvyyyy
129
- createResolversFields({ applyWrapper, clearWrapper, getTypeToUse, referencedUnionType, shouldInclude, }) {
111
+ createResolversFields(applyWrapper, clearWrapper, getTypeToUse, shouldInclude) {
130
112
  const allSchemaTypes = this._schema.getTypeMap();
131
113
  const typeNames = this._federation.filterTypeNames(Object.keys(allSchemaTypes));
132
114
  // avoid checking all types recursively if we have no `mappers` defined
@@ -183,13 +165,8 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
183
165
  prev[typeName] = applyWrapper(this._getScalar(typeName));
184
166
  }
185
167
  else if ((0, graphql_1.isUnionType)(schemaType)) {
186
- if (referencedUnionType === 'ResolversUnionTypes') {
187
- this._hasReferencedResolversUnionTypes = true;
188
- }
189
- else if (referencedUnionType === 'ResolversUnionParentTypes') {
190
- this._hasReferencedResolversUnionParentTypes = true;
191
- }
192
- const resolversType = this.convertName(referencedUnionType);
168
+ this._hasReferencedResolversUnionTypes = true;
169
+ const resolversType = this.convertName('ResolversUnionTypes');
193
170
  prev[typeName] = applyWrapper(`${resolversType}['${typeName}']`);
194
171
  }
195
172
  else if ((0, graphql_1.isEnumType)(schemaType)) {
@@ -265,7 +242,7 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
265
242
  }
266
243
  return `Array<${t}>`;
267
244
  }
268
- createResolversUnionTypes({ getTypeToUse, }) {
245
+ createResolversUnionTypes() {
269
246
  if (!this._hasReferencedResolversUnionTypes) {
270
247
  return {};
271
248
  }
@@ -286,8 +263,11 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
286
263
  let unionMemberValue = this.convertName(unionMemberType.name, {}, true);
287
264
  // 2b. Find fields to Omit if needed.
288
265
  // - If no field to Omit, "type with maybe Omit" is typescript type i.e. no Omit
289
- // - If there are fields to Omit, keep track of these "type with maybe Omit" to replace in original unionMemberValue
290
- const fieldsToOmit = this.getRelevantFieldsToOmit({ schemaType: unionMemberType, getTypeToUse });
266
+ // - If there are fields to Omit, "type with maybe Omit"
267
+ const fieldsToOmit = this.getRelevantFieldsToOmit({
268
+ schemaType: unionMemberType,
269
+ getTypeToUse: this.getTypeToUse,
270
+ });
291
271
  if (fieldsToOmit.length > 0) {
292
272
  unionMemberValue = this.replaceFieldsInType(unionMemberValue, fieldsToOmit);
293
273
  }
@@ -371,20 +351,6 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
371
351
  .map(([typeName, value]) => (0, utils_js_1.indent)(`${typeName}: ${value}${this.getPunctuation(declarationKind)}`))
372
352
  .join('\n')).string;
373
353
  }
374
- buildResolversUnionParentTypes() {
375
- if (Object.keys(this._resolversUnionParentTypes).length === 0) {
376
- return '';
377
- }
378
- const declarationKind = 'type';
379
- return new utils_js_1.DeclarationBlock(this._declarationBlockConfig)
380
- .export()
381
- .asKind(declarationKind)
382
- .withName(this.convertName('ResolversUnionParentTypes'))
383
- .withComment('Mapping of union parent types')
384
- .withBlock(Object.entries(this._resolversUnionParentTypes)
385
- .map(([typeName, value]) => (0, utils_js_1.indent)(`${typeName}: ${value}${this.getPunctuation(declarationKind)}`))
386
- .join('\n')).string;
387
- }
388
354
  get schema() {
389
355
  return this._schema;
390
356
  }
@@ -91,7 +91,7 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
91
91
  }
92
92
  _includeFragments(fragments, nodeKind) {
93
93
  if (fragments && fragments.length > 0) {
94
- if (this.config.documentMode === DocumentMode.documentNode) {
94
+ if (this.config.documentMode === DocumentMode.documentNode || this.config.documentMode === DocumentMode.string) {
95
95
  return Array.from(this._fragments.values())
96
96
  .filter(f => fragments.includes(this.getFragmentVariableName(f.name)))
97
97
  .map(fragment => (0, graphql_1.print)(fragment.node))
@@ -164,7 +164,20 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
164
164
  return `{${metaString}"kind":"${graphql_1.Kind.DOCUMENT}","definitions":${jsonStringify(definitions)}}`;
165
165
  }
166
166
  if (this.config.documentMode === DocumentMode.string) {
167
- return '`' + doc + '`';
167
+ let meta;
168
+ 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)}}`;
175
+ }
176
+ }
177
+ if (meta) {
178
+ return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify(meta)})`;
179
+ }
180
+ return `new TypedDocumentString(\`${doc}\`)`;
168
181
  }
169
182
  const gqlImport = this._parseImport(this.config.gqlImport || 'graphql-tag');
170
183
  return (gqlImport.propName || 'gql') + '`' + doc + '`';
@@ -48,9 +48,7 @@ export class BaseResolversVisitor extends BaseVisitor {
48
48
  this._resolversTypes = {};
49
49
  this._resolversParentTypes = {};
50
50
  this._hasReferencedResolversUnionTypes = false;
51
- this._hasReferencedResolversUnionParentTypes = false;
52
51
  this._resolversUnionTypes = {};
53
- this._resolversUnionParentTypes = {};
54
52
  this._rootTypeNames = new Set();
55
53
  this._globalDeclarations = new Set();
56
54
  this._hasScalars = false;
@@ -60,25 +58,9 @@ export class BaseResolversVisitor extends BaseVisitor {
60
58
  this._federation = new ApolloFederation({ enabled: this.config.federation, schema: this.schema });
61
59
  this._rootTypeNames = getRootTypeNames(_schema);
62
60
  this._variablesTransformer = new OperationVariablesToObject(this.scalars, this.convertName, this.config.namespacedImportName);
63
- this._resolversTypes = this.createResolversFields({
64
- applyWrapper: type => this.applyResolverTypeWrapper(type),
65
- clearWrapper: type => this.clearResolverTypeWrapper(type),
66
- getTypeToUse: name => this.getTypeToUse(name),
67
- referencedUnionType: 'ResolversUnionTypes',
68
- });
69
- this._resolversParentTypes = this.createResolversFields({
70
- applyWrapper: type => type,
71
- clearWrapper: type => type,
72
- getTypeToUse: name => this.getParentTypeToUse(name),
73
- referencedUnionType: 'ResolversUnionParentTypes',
74
- shouldInclude: namedType => !isEnumType(namedType),
75
- });
76
- this._resolversUnionTypes = this.createResolversUnionTypes({
77
- getTypeToUse: this.getTypeToUse,
78
- });
79
- this._resolversUnionParentTypes = this.createResolversUnionTypes({
80
- getTypeToUse: this.getParentTypeToUse,
81
- });
61
+ this._resolversTypes = this.createResolversFields(type => this.applyResolverTypeWrapper(type), type => this.clearResolverTypeWrapper(type), name => this.getTypeToUse(name));
62
+ this._resolversParentTypes = this.createResolversFields(type => type, type => type, name => this.getParentTypeToUse(name), namedType => !isEnumType(namedType));
63
+ this._resolversUnionTypes = this.createResolversUnionTypes();
82
64
  this._fieldContextTypeMap = this.createFieldContextTypeMap();
83
65
  this._directiveContextTypesMap = this.createDirectivedContextType();
84
66
  this._directiveResolverMappings = (_a = rawConfig.directiveResolverMappings) !== null && _a !== void 0 ? _a : {};
@@ -122,7 +104,7 @@ export class BaseResolversVisitor extends BaseVisitor {
122
104
  return `${applyNamespacedImport && this.config.namespacedImportName ? this.config.namespacedImportName + '.' : ''}${sourceType}`;
123
105
  }
124
106
  // Kamil: this one is heeeeavvyyyy
125
- createResolversFields({ applyWrapper, clearWrapper, getTypeToUse, referencedUnionType, shouldInclude, }) {
107
+ createResolversFields(applyWrapper, clearWrapper, getTypeToUse, shouldInclude) {
126
108
  const allSchemaTypes = this._schema.getTypeMap();
127
109
  const typeNames = this._federation.filterTypeNames(Object.keys(allSchemaTypes));
128
110
  // avoid checking all types recursively if we have no `mappers` defined
@@ -179,13 +161,8 @@ export class BaseResolversVisitor extends BaseVisitor {
179
161
  prev[typeName] = applyWrapper(this._getScalar(typeName));
180
162
  }
181
163
  else if (isUnionType(schemaType)) {
182
- if (referencedUnionType === 'ResolversUnionTypes') {
183
- this._hasReferencedResolversUnionTypes = true;
184
- }
185
- else if (referencedUnionType === 'ResolversUnionParentTypes') {
186
- this._hasReferencedResolversUnionParentTypes = true;
187
- }
188
- const resolversType = this.convertName(referencedUnionType);
164
+ this._hasReferencedResolversUnionTypes = true;
165
+ const resolversType = this.convertName('ResolversUnionTypes');
189
166
  prev[typeName] = applyWrapper(`${resolversType}['${typeName}']`);
190
167
  }
191
168
  else if (isEnumType(schemaType)) {
@@ -261,7 +238,7 @@ export class BaseResolversVisitor extends BaseVisitor {
261
238
  }
262
239
  return `Array<${t}>`;
263
240
  }
264
- createResolversUnionTypes({ getTypeToUse, }) {
241
+ createResolversUnionTypes() {
265
242
  if (!this._hasReferencedResolversUnionTypes) {
266
243
  return {};
267
244
  }
@@ -282,8 +259,11 @@ export class BaseResolversVisitor extends BaseVisitor {
282
259
  let unionMemberValue = this.convertName(unionMemberType.name, {}, true);
283
260
  // 2b. Find fields to Omit if needed.
284
261
  // - If no field to Omit, "type with maybe Omit" is typescript type i.e. no Omit
285
- // - If there are fields to Omit, keep track of these "type with maybe Omit" to replace in original unionMemberValue
286
- const fieldsToOmit = this.getRelevantFieldsToOmit({ schemaType: unionMemberType, getTypeToUse });
262
+ // - If there are fields to Omit, "type with maybe Omit"
263
+ const fieldsToOmit = this.getRelevantFieldsToOmit({
264
+ schemaType: unionMemberType,
265
+ getTypeToUse: this.getTypeToUse,
266
+ });
287
267
  if (fieldsToOmit.length > 0) {
288
268
  unionMemberValue = this.replaceFieldsInType(unionMemberValue, fieldsToOmit);
289
269
  }
@@ -367,20 +347,6 @@ export class BaseResolversVisitor extends BaseVisitor {
367
347
  .map(([typeName, value]) => indent(`${typeName}: ${value}${this.getPunctuation(declarationKind)}`))
368
348
  .join('\n')).string;
369
349
  }
370
- buildResolversUnionParentTypes() {
371
- if (Object.keys(this._resolversUnionParentTypes).length === 0) {
372
- return '';
373
- }
374
- const declarationKind = 'type';
375
- return new DeclarationBlock(this._declarationBlockConfig)
376
- .export()
377
- .asKind(declarationKind)
378
- .withName(this.convertName('ResolversUnionParentTypes'))
379
- .withComment('Mapping of union parent types')
380
- .withBlock(Object.entries(this._resolversUnionParentTypes)
381
- .map(([typeName, value]) => indent(`${typeName}: ${value}${this.getPunctuation(declarationKind)}`))
382
- .join('\n')).string;
383
- }
384
350
  get schema() {
385
351
  return this._schema;
386
352
  }
@@ -87,7 +87,7 @@ export class ClientSideBaseVisitor extends BaseVisitor {
87
87
  }
88
88
  _includeFragments(fragments, nodeKind) {
89
89
  if (fragments && fragments.length > 0) {
90
- if (this.config.documentMode === DocumentMode.documentNode) {
90
+ if (this.config.documentMode === DocumentMode.documentNode || this.config.documentMode === DocumentMode.string) {
91
91
  return Array.from(this._fragments.values())
92
92
  .filter(f => fragments.includes(this.getFragmentVariableName(f.name)))
93
93
  .map(fragment => print(fragment.node))
@@ -160,7 +160,20 @@ export class ClientSideBaseVisitor extends BaseVisitor {
160
160
  return `{${metaString}"kind":"${Kind.DOCUMENT}","definitions":${jsonStringify(definitions)}}`;
161
161
  }
162
162
  if (this.config.documentMode === DocumentMode.string) {
163
- return '`' + doc + '`';
163
+ let meta;
164
+ 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)}}`;
171
+ }
172
+ }
173
+ if (meta) {
174
+ return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify(meta)})`;
175
+ }
176
+ return `new TypedDocumentString(\`${doc}\`)`;
164
177
  }
165
178
  const gqlImport = this._parseImport(this.config.gqlImport || 'graphql-tag');
166
179
  return (gqlImport.propName || 'gql') + '`' + doc + '`';
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@graphql-codegen/visitor-plugin-common",
3
- "version": "3.1.0-alpha-20230320102705-ea2aff584",
3
+ "version": "3.1.0-alpha-20230320114137-e954beaff",
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-20230320102705-ea2aff584",
9
+ "@graphql-codegen/plugin-helpers": "4.2.0-alpha-20230320114137-e954beaff",
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",
@@ -532,9 +532,7 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
532
532
  protected _resolversTypes: ResolverTypes;
533
533
  protected _resolversParentTypes: ResolverParentTypes;
534
534
  protected _hasReferencedResolversUnionTypes: boolean;
535
- protected _hasReferencedResolversUnionParentTypes: boolean;
536
535
  protected _resolversUnionTypes: Record<string, string>;
537
- protected _resolversUnionParentTypes: Record<string, string>;
538
536
  protected _rootTypeNames: Set<string>;
539
537
  protected _globalDeclarations: Set<string>;
540
538
  protected _federation: ApolloFederation;
@@ -548,28 +546,19 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
548
546
  getResolverTypeWrapperSignature(): string;
549
547
  protected shouldMapType(type: GraphQLNamedType, duringCheck?: string[]): boolean;
550
548
  convertName(node: ASTNode | string, options?: BaseVisitorConvertOptions & ConvertOptions, applyNamespacedImport?: boolean): string;
551
- protected createResolversFields({ applyWrapper, clearWrapper, getTypeToUse, referencedUnionType, shouldInclude, }: {
552
- applyWrapper: (str: string) => string;
553
- clearWrapper: (str: string) => string;
554
- getTypeToUse: (str: string) => string;
555
- referencedUnionType: 'ResolversUnionTypes' | 'ResolversUnionParentTypes';
556
- shouldInclude?: (type: GraphQLNamedType) => boolean;
557
- }): ResolverTypes;
549
+ protected createResolversFields(applyWrapper: (str: string) => string, clearWrapper: (str: string) => string, getTypeToUse: (str: string) => string, shouldInclude?: (type: GraphQLNamedType) => boolean): ResolverTypes;
558
550
  protected replaceFieldsInType(typeName: string, relevantFields: ReturnType<typeof this.getRelevantFieldsToOmit>): string;
559
551
  protected applyMaybe(str: string): string;
560
552
  protected applyResolverTypeWrapper(str: string): string;
561
553
  protected clearMaybe(str: string): string;
562
554
  protected clearResolverTypeWrapper(str: string): string;
563
555
  protected wrapWithArray(t: string): string;
564
- protected createResolversUnionTypes({ getTypeToUse, }: {
565
- getTypeToUse: (name: string) => string;
566
- }): Record<string, string>;
556
+ protected createResolversUnionTypes(): Record<string, string>;
567
557
  protected createFieldContextTypeMap(): FieldContextTypeMap;
568
558
  protected createDirectivedContextType(): FieldContextTypeMap;
569
559
  buildResolversTypes(): string;
570
560
  buildResolversParentTypes(): string;
571
561
  buildResolversUnionTypes(): string;
572
- buildResolversUnionParentTypes(): string;
573
562
  get schema(): GraphQLSchema;
574
563
  get defaultMapperType(): string;
575
564
  get unusedMappers(): string[];
@@ -532,9 +532,7 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
532
532
  protected _resolversTypes: ResolverTypes;
533
533
  protected _resolversParentTypes: ResolverParentTypes;
534
534
  protected _hasReferencedResolversUnionTypes: boolean;
535
- protected _hasReferencedResolversUnionParentTypes: boolean;
536
535
  protected _resolversUnionTypes: Record<string, string>;
537
- protected _resolversUnionParentTypes: Record<string, string>;
538
536
  protected _rootTypeNames: Set<string>;
539
537
  protected _globalDeclarations: Set<string>;
540
538
  protected _federation: ApolloFederation;
@@ -548,28 +546,19 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
548
546
  getResolverTypeWrapperSignature(): string;
549
547
  protected shouldMapType(type: GraphQLNamedType, duringCheck?: string[]): boolean;
550
548
  convertName(node: ASTNode | string, options?: BaseVisitorConvertOptions & ConvertOptions, applyNamespacedImport?: boolean): string;
551
- protected createResolversFields({ applyWrapper, clearWrapper, getTypeToUse, referencedUnionType, shouldInclude, }: {
552
- applyWrapper: (str: string) => string;
553
- clearWrapper: (str: string) => string;
554
- getTypeToUse: (str: string) => string;
555
- referencedUnionType: 'ResolversUnionTypes' | 'ResolversUnionParentTypes';
556
- shouldInclude?: (type: GraphQLNamedType) => boolean;
557
- }): ResolverTypes;
549
+ protected createResolversFields(applyWrapper: (str: string) => string, clearWrapper: (str: string) => string, getTypeToUse: (str: string) => string, shouldInclude?: (type: GraphQLNamedType) => boolean): ResolverTypes;
558
550
  protected replaceFieldsInType(typeName: string, relevantFields: ReturnType<typeof this.getRelevantFieldsToOmit>): string;
559
551
  protected applyMaybe(str: string): string;
560
552
  protected applyResolverTypeWrapper(str: string): string;
561
553
  protected clearMaybe(str: string): string;
562
554
  protected clearResolverTypeWrapper(str: string): string;
563
555
  protected wrapWithArray(t: string): string;
564
- protected createResolversUnionTypes({ getTypeToUse, }: {
565
- getTypeToUse: (name: string) => string;
566
- }): Record<string, string>;
556
+ protected createResolversUnionTypes(): Record<string, string>;
567
557
  protected createFieldContextTypeMap(): FieldContextTypeMap;
568
558
  protected createDirectivedContextType(): FieldContextTypeMap;
569
559
  buildResolversTypes(): string;
570
560
  buildResolversParentTypes(): string;
571
561
  buildResolversUnionTypes(): string;
572
- buildResolversUnionParentTypes(): string;
573
562
  get schema(): GraphQLSchema;
574
563
  get defaultMapperType(): string;
575
564
  get unusedMappers(): string[];