@graphql-codegen/plugin-helpers 4.1.0 → 4.1.1-alpha-20230308024648-c2ff4e4a2

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.
package/cjs/federation.js CHANGED
@@ -132,11 +132,11 @@ class ApolloFederation {
132
132
  if (keys.length) {
133
133
  const outputs = [`{ __typename: '${parentType.name}' } &`];
134
134
  // Look for @requires and see what the service needs and gets
135
- const requires = getDirectivesByName('requires', fieldNode).map(this.extractKeyOrRequiresFieldSet);
135
+ const requires = getDirectivesByName('requires', fieldNode).map(this.extractFieldSet);
136
136
  const requiredFields = this.translateFieldSet((0, merge_js_1.default)({}, ...requires), parentTypeSignature);
137
137
  // @key() @key() - "primary keys" in Federation
138
138
  const primaryKeys = keys.map(def => {
139
- const fields = this.extractKeyOrRequiresFieldSet(def);
139
+ const fields = this.extractFieldSet(def);
140
140
  return this.translateFieldSet(fields, parentTypeSignature);
141
141
  });
142
142
  const [open, close] = primaryKeys.length > 1 ? ['(', ')'] : ['', ''];
@@ -166,7 +166,7 @@ class ApolloFederation {
166
166
  translateFieldSet(fields, parentTypeRef) {
167
167
  return `GraphQLRecursivePick<${parentTypeRef}, ${JSON.stringify(fields)}>`;
168
168
  }
169
- extractKeyOrRequiresFieldSet(directive) {
169
+ extractFieldSet(directive) {
170
170
  const arg = directive.arguments.find(arg => arg.name.value === 'fields');
171
171
  const { value } = arg.value;
172
172
  return (0, index_js_1.oldVisit)((0, graphql_1.parse)(`{${value}}`), {
@@ -189,14 +189,6 @@ class ApolloFederation {
189
189
  },
190
190
  });
191
191
  }
192
- extractProvidesFieldSet(directive) {
193
- const arg = directive.arguments.find(arg => arg.name.value === 'fields');
194
- const { value } = arg.value;
195
- if (/[{}]/gi.test(value)) {
196
- throw new Error('Nested fields in _FieldSet is not supported in the @provides directive');
197
- }
198
- return value.split(/\s+/g);
199
- }
200
192
  createMapOfProvides() {
201
193
  const providesMap = {};
202
194
  Object.keys(this.schema.getTypeMap()).forEach(typename => {
@@ -205,8 +197,8 @@ class ApolloFederation {
205
197
  Object.values(objectType.getFields()).forEach(field => {
206
198
  var _a;
207
199
  const provides = getDirectivesByName('provides', field.astNode)
208
- .map(this.extractProvidesFieldSet)
209
- .reduce((prev, curr) => [...prev, ...curr], []);
200
+ .map(this.extractFieldSet)
201
+ .reduce((prev, curr) => [...prev, ...Object.keys(curr)], []);
210
202
  const ofType = (0, utils_js_1.getBaseType)(field.type);
211
203
  providesMap[_a = ofType.name] || (providesMap[_a] = []);
212
204
  providesMap[ofType.name].push(...provides);
package/esm/federation.js CHANGED
@@ -126,11 +126,11 @@ export class ApolloFederation {
126
126
  if (keys.length) {
127
127
  const outputs = [`{ __typename: '${parentType.name}' } &`];
128
128
  // Look for @requires and see what the service needs and gets
129
- const requires = getDirectivesByName('requires', fieldNode).map(this.extractKeyOrRequiresFieldSet);
129
+ const requires = getDirectivesByName('requires', fieldNode).map(this.extractFieldSet);
130
130
  const requiredFields = this.translateFieldSet(merge({}, ...requires), parentTypeSignature);
131
131
  // @key() @key() - "primary keys" in Federation
132
132
  const primaryKeys = keys.map(def => {
133
- const fields = this.extractKeyOrRequiresFieldSet(def);
133
+ const fields = this.extractFieldSet(def);
134
134
  return this.translateFieldSet(fields, parentTypeSignature);
135
135
  });
136
136
  const [open, close] = primaryKeys.length > 1 ? ['(', ')'] : ['', ''];
@@ -160,7 +160,7 @@ export class ApolloFederation {
160
160
  translateFieldSet(fields, parentTypeRef) {
161
161
  return `GraphQLRecursivePick<${parentTypeRef}, ${JSON.stringify(fields)}>`;
162
162
  }
163
- extractKeyOrRequiresFieldSet(directive) {
163
+ extractFieldSet(directive) {
164
164
  const arg = directive.arguments.find(arg => arg.name.value === 'fields');
165
165
  const { value } = arg.value;
166
166
  return oldVisit(parse(`{${value}}`), {
@@ -183,14 +183,6 @@ export class ApolloFederation {
183
183
  },
184
184
  });
185
185
  }
186
- extractProvidesFieldSet(directive) {
187
- const arg = directive.arguments.find(arg => arg.name.value === 'fields');
188
- const { value } = arg.value;
189
- if (/[{}]/gi.test(value)) {
190
- throw new Error('Nested fields in _FieldSet is not supported in the @provides directive');
191
- }
192
- return value.split(/\s+/g);
193
- }
194
186
  createMapOfProvides() {
195
187
  const providesMap = {};
196
188
  Object.keys(this.schema.getTypeMap()).forEach(typename => {
@@ -199,8 +191,8 @@ export class ApolloFederation {
199
191
  Object.values(objectType.getFields()).forEach(field => {
200
192
  var _a;
201
193
  const provides = getDirectivesByName('provides', field.astNode)
202
- .map(this.extractProvidesFieldSet)
203
- .reduce((prev, curr) => [...prev, ...curr], []);
194
+ .map(this.extractFieldSet)
195
+ .reduce((prev, curr) => [...prev, ...Object.keys(curr)], []);
204
196
  const ofType = getBaseType(field.type);
205
197
  providesMap[_a = ofType.name] || (providesMap[_a] = []);
206
198
  providesMap[ofType.name].push(...provides);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/plugin-helpers",
3
- "version": "4.1.0",
3
+ "version": "4.1.1-alpha-20230308024648-c2ff4e4a2",
4
4
  "description": "GraphQL Code Generator common utils and types",
5
5
  "peerDependencies": {
6
6
  "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"
@@ -64,7 +64,6 @@ export declare class ApolloFederation {
64
64
  private isExternal;
65
65
  private hasProvides;
66
66
  private translateFieldSet;
67
- private extractKeyOrRequiresFieldSet;
68
- private extractProvidesFieldSet;
67
+ private extractFieldSet;
69
68
  private createMapOfProvides;
70
69
  }
@@ -64,7 +64,6 @@ export declare class ApolloFederation {
64
64
  private isExternal;
65
65
  private hasProvides;
66
66
  private translateFieldSet;
67
- private extractKeyOrRequiresFieldSet;
68
- private extractProvidesFieldSet;
67
+ private extractFieldSet;
69
68
  private createMapOfProvides;
70
69
  }
@@ -1,7 +1,7 @@
1
1
  import { ASTNode, visit } from 'graphql';
2
- declare type VisitFn = typeof visit;
3
- declare type NewVisitor = Partial<Parameters<VisitFn>[1]>;
4
- declare type OldVisitor = {
2
+ type VisitFn = typeof visit;
3
+ type NewVisitor = Partial<Parameters<VisitFn>[1]>;
4
+ type OldVisitor = {
5
5
  enter?: Partial<Record<keyof NewVisitor, NonNullable<NewVisitor[keyof NewVisitor]>['enter']>>;
6
6
  leave?: Partial<Record<keyof NewVisitor, NonNullable<NewVisitor[keyof NewVisitor]>['leave']>>;
7
7
  } & NewVisitor;
@@ -1,7 +1,7 @@
1
1
  import { ASTNode, visit } from 'graphql';
2
- declare type VisitFn = typeof visit;
3
- declare type NewVisitor = Partial<Parameters<VisitFn>[1]>;
4
- declare type OldVisitor = {
2
+ type VisitFn = typeof visit;
3
+ type NewVisitor = Partial<Parameters<VisitFn>[1]>;
4
+ type OldVisitor = {
5
5
  enter?: Partial<Record<keyof NewVisitor, NonNullable<NewVisitor[keyof NewVisitor]>['enter']>>;
6
6
  leave?: Partial<Record<keyof NewVisitor, NonNullable<NewVisitor[keyof NewVisitor]>['leave']>>;
7
7
  } & NewVisitor;
@@ -21,6 +21,7 @@ export declare namespace Types {
21
21
  };
22
22
  profiler?: Profiler;
23
23
  cache?<T>(namespace: string, key: string, factory: () => Promise<T>): Promise<T>;
24
+ documentTransforms?: ConfiguredDocumentTransform[];
24
25
  }
25
26
  type FileOutput = {
26
27
  filename: string;
@@ -287,6 +288,10 @@ export declare namespace Types {
287
288
  * For more details: https://graphql-code-generator.com/docs/config-reference/lifecycle-hooks
288
289
  */
289
290
  hooks?: Partial<LifecycleHooksDefinition>;
291
+ /**
292
+ * @description DocumentTransform changes documents before executing plugins.
293
+ */
294
+ documentTransforms?: OutputDocumentTransform[];
290
295
  }
291
296
  type PresetFnArgs<Config = any, PluginConfig = {
292
297
  [key: string]: any;
@@ -306,6 +311,7 @@ export declare namespace Types {
306
311
  };
307
312
  profiler?: Profiler;
308
313
  cache?<T>(namespace: string, key: string, factory: () => Promise<T>): Promise<T>;
314
+ documentTransforms?: ConfiguredDocumentTransform[];
309
315
  };
310
316
  type OutputPreset<TPresetConfig = any> = {
311
317
  buildGeneratesSection: (options: PresetFnArgs<TPresetConfig>) => Promisable<GenerateOptions[]>;
@@ -398,6 +404,8 @@ export declare namespace Types {
398
404
  */
399
405
  watch?: boolean | string | string[];
400
406
  /**
407
+ * @deprecated this is not necessary since we are using `@parcel/watcher` instead of `chockidar`.
408
+ *
401
409
  * @description Allows overriding the behavior of watch to use stat polling over native file watching support.
402
410
  *
403
411
  * Config fields have the same defaults and sematics as the identically named ones for chokidar.
@@ -563,9 +571,31 @@ export declare namespace Types {
563
571
  */
564
572
  skipValidationAgainstSchema?: boolean;
565
573
  } | boolean;
574
+ type DocumentTransformFunction<Config = object> = (options: {
575
+ documents: Types.DocumentFile[];
576
+ schema: DocumentNode;
577
+ config: Config;
578
+ pluginContext?: {
579
+ [key: string]: any;
580
+ };
581
+ }) => Types.Promisable<Types.DocumentFile[]>;
582
+ type DocumentTransformObject<T = object> = {
583
+ transform: DocumentTransformFunction<T>;
584
+ };
585
+ type DocumentTransformFileName = string;
586
+ type DocumentTransformFileConfig<T = object> = {
587
+ [name: DocumentTransformFileName]: T;
588
+ };
589
+ type DocumentTransformFile<T> = DocumentTransformFileName | DocumentTransformFileConfig<T>;
590
+ type OutputDocumentTransform<T = object> = DocumentTransformObject<T> | DocumentTransformFile<T>;
591
+ type ConfiguredDocumentTransform<T = object> = {
592
+ name: string;
593
+ transformObject: DocumentTransformObject<T>;
594
+ config?: T;
595
+ };
566
596
  }
567
597
  export declare function isComplexPluginOutput(obj: Types.PluginOutput): obj is Types.ComplexPluginOutput;
568
- export declare type PluginFunction<T = any, TOutput extends Types.PluginOutput = Types.PluginOutput> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, info?: {
598
+ export type PluginFunction<T = any, TOutput extends Types.PluginOutput = Types.PluginOutput> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, info?: {
569
599
  outputFile?: string;
570
600
  allPlugins?: Types.ConfiguredPlugin[];
571
601
  pluginContext?: {
@@ -573,10 +603,10 @@ export declare type PluginFunction<T = any, TOutput extends Types.PluginOutput =
573
603
  };
574
604
  [key: string]: any;
575
605
  }) => Types.Promisable<TOutput>;
576
- export declare type PluginValidateFn<T = any> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, outputFile: string, allPlugins: Types.ConfiguredPlugin[], pluginContext?: {
606
+ export type PluginValidateFn<T = any> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, outputFile: string, allPlugins: Types.ConfiguredPlugin[], pluginContext?: {
577
607
  [key: string]: any;
578
608
  }) => Types.Promisable<void>;
579
- export declare type AddToSchemaResult = string | DocumentNode | undefined;
609
+ export type AddToSchemaResult = string | DocumentNode | undefined;
580
610
  export interface CodegenPlugin<T = any> {
581
611
  plugin: PluginFunction<T>;
582
612
  addToSchema?: AddToSchemaResult | ((config: T) => AddToSchemaResult);
@@ -21,6 +21,7 @@ export declare namespace Types {
21
21
  };
22
22
  profiler?: Profiler;
23
23
  cache?<T>(namespace: string, key: string, factory: () => Promise<T>): Promise<T>;
24
+ documentTransforms?: ConfiguredDocumentTransform[];
24
25
  }
25
26
  type FileOutput = {
26
27
  filename: string;
@@ -287,6 +288,10 @@ export declare namespace Types {
287
288
  * For more details: https://graphql-code-generator.com/docs/config-reference/lifecycle-hooks
288
289
  */
289
290
  hooks?: Partial<LifecycleHooksDefinition>;
291
+ /**
292
+ * @description DocumentTransform changes documents before executing plugins.
293
+ */
294
+ documentTransforms?: OutputDocumentTransform[];
290
295
  }
291
296
  type PresetFnArgs<Config = any, PluginConfig = {
292
297
  [key: string]: any;
@@ -306,6 +311,7 @@ export declare namespace Types {
306
311
  };
307
312
  profiler?: Profiler;
308
313
  cache?<T>(namespace: string, key: string, factory: () => Promise<T>): Promise<T>;
314
+ documentTransforms?: ConfiguredDocumentTransform[];
309
315
  };
310
316
  type OutputPreset<TPresetConfig = any> = {
311
317
  buildGeneratesSection: (options: PresetFnArgs<TPresetConfig>) => Promisable<GenerateOptions[]>;
@@ -398,6 +404,8 @@ export declare namespace Types {
398
404
  */
399
405
  watch?: boolean | string | string[];
400
406
  /**
407
+ * @deprecated this is not necessary since we are using `@parcel/watcher` instead of `chockidar`.
408
+ *
401
409
  * @description Allows overriding the behavior of watch to use stat polling over native file watching support.
402
410
  *
403
411
  * Config fields have the same defaults and sematics as the identically named ones for chokidar.
@@ -563,9 +571,31 @@ export declare namespace Types {
563
571
  */
564
572
  skipValidationAgainstSchema?: boolean;
565
573
  } | boolean;
574
+ type DocumentTransformFunction<Config = object> = (options: {
575
+ documents: Types.DocumentFile[];
576
+ schema: DocumentNode;
577
+ config: Config;
578
+ pluginContext?: {
579
+ [key: string]: any;
580
+ };
581
+ }) => Types.Promisable<Types.DocumentFile[]>;
582
+ type DocumentTransformObject<T = object> = {
583
+ transform: DocumentTransformFunction<T>;
584
+ };
585
+ type DocumentTransformFileName = string;
586
+ type DocumentTransformFileConfig<T = object> = {
587
+ [name: DocumentTransformFileName]: T;
588
+ };
589
+ type DocumentTransformFile<T> = DocumentTransformFileName | DocumentTransformFileConfig<T>;
590
+ type OutputDocumentTransform<T = object> = DocumentTransformObject<T> | DocumentTransformFile<T>;
591
+ type ConfiguredDocumentTransform<T = object> = {
592
+ name: string;
593
+ transformObject: DocumentTransformObject<T>;
594
+ config?: T;
595
+ };
566
596
  }
567
597
  export declare function isComplexPluginOutput(obj: Types.PluginOutput): obj is Types.ComplexPluginOutput;
568
- export declare type PluginFunction<T = any, TOutput extends Types.PluginOutput = Types.PluginOutput> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, info?: {
598
+ export type PluginFunction<T = any, TOutput extends Types.PluginOutput = Types.PluginOutput> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, info?: {
569
599
  outputFile?: string;
570
600
  allPlugins?: Types.ConfiguredPlugin[];
571
601
  pluginContext?: {
@@ -573,10 +603,10 @@ export declare type PluginFunction<T = any, TOutput extends Types.PluginOutput =
573
603
  };
574
604
  [key: string]: any;
575
605
  }) => Types.Promisable<TOutput>;
576
- export declare type PluginValidateFn<T = any> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, outputFile: string, allPlugins: Types.ConfiguredPlugin[], pluginContext?: {
606
+ export type PluginValidateFn<T = any> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, outputFile: string, allPlugins: Types.ConfiguredPlugin[], pluginContext?: {
577
607
  [key: string]: any;
578
608
  }) => Types.Promisable<void>;
579
- export declare type AddToSchemaResult = string | DocumentNode | undefined;
609
+ export type AddToSchemaResult = string | DocumentNode | undefined;
580
610
  export interface CodegenPlugin<T = any> {
581
611
  plugin: PluginFunction<T>;
582
612
  addToSchema?: AddToSchemaResult | ((config: T) => AddToSchemaResult);