@graphql-codegen/visitor-plugin-common 5.7.2-alpha-20250314001208-26b650f2e9df4ac12ed3c6875084807691bbe9f0 → 5.8.0-alpha-20250320115712-0c95c1869f1781093936579f0f8ebc1091082c5c

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.
@@ -47,6 +47,9 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
47
47
  },
48
48
  resolversNonOptionalTypename: normalizeResolversNonOptionalTypename((0, utils_js_1.getConfigValue)(rawConfig.resolversNonOptionalTypename, false)),
49
49
  avoidCheckingAbstractTypesRecursively: (0, utils_js_1.getConfigValue)(rawConfig.avoidCheckingAbstractTypesRecursively, false),
50
+ customDirectives: {
51
+ semanticNonNull: rawConfig.customDirectives?.semanticNonNull ?? false,
52
+ },
50
53
  ...additionalConfig,
51
54
  });
52
55
  this._schema = _schema;
@@ -610,17 +613,11 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
610
613
  const declarationKind = 'type';
611
614
  return (parentName, avoidResolverOptionals) => {
612
615
  const original = parent[key];
613
- const baseType = (0, utils_js_1.getBaseTypeNode)(original.type);
614
- const realType = baseType.name.value;
615
616
  const parentType = this.schema.getType(parentName);
616
617
  if (this._federation.skipField({ fieldNode: original, parentType })) {
617
618
  return null;
618
619
  }
619
620
  const contextType = this.getContextType(parentName, node);
620
- const typeToUse = this.getTypeToUse(realType);
621
- const mappedType = this._variablesTransformer.wrapAstTypeWithModifiers(typeToUse, original.type);
622
- const subscriptionType = this._schema.getSubscriptionType();
623
- const isSubscriptionType = subscriptionType && subscriptionType.name === parentName;
624
621
  let argsType = hasArguments
625
622
  ? this.convertName(parentName +
626
623
  (this.config.addUnderscoreToArgsType ? '_' : '') +
@@ -647,12 +644,35 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
647
644
  parentType,
648
645
  parentTypeSignature: this.getParentTypeForSignature(node),
649
646
  });
650
- const mappedTypeKey = isSubscriptionType ? `${mappedType}, "${node.name}"` : mappedType;
651
- const directiveMappings = node.directives
652
- ?.map(directive => this._directiveResolverMappings[directive.name])
653
- .filter(Boolean)
654
- .reverse() ?? [];
655
- const resolverType = isSubscriptionType ? 'SubscriptionResolver' : directiveMappings[0] ?? 'Resolver';
647
+ const { mappedTypeKey, resolverType } = (() => {
648
+ const baseType = (0, utils_js_1.getBaseTypeNode)(original.type);
649
+ const realType = baseType.name.value;
650
+ const typeToUse = this.getTypeToUse(realType);
651
+ /**
652
+ * Turns GraphQL type to TypeScript types (`mappedType`) e.g.
653
+ * - String! -> ResolversTypes['String']>
654
+ * - String -> Maybe<ResolversTypes['String']>
655
+ * - [String] -> Maybe<Array<Maybe<ResolversTypes['String']>>>
656
+ * - [String!]! -> Array<ResolversTypes['String']>
657
+ */
658
+ const mappedType = this._variablesTransformer.wrapAstTypeWithModifiers(typeToUse, original.type);
659
+ const subscriptionType = this._schema.getSubscriptionType();
660
+ const isSubscriptionType = subscriptionType && subscriptionType.name === parentName;
661
+ if (isSubscriptionType) {
662
+ return {
663
+ mappedTypeKey: `${mappedType}, "${node.name}"`,
664
+ resolverType: 'SubscriptionResolver',
665
+ };
666
+ }
667
+ const directiveMappings = node.directives
668
+ ?.map(directive => this._directiveResolverMappings[directive.name])
669
+ .filter(Boolean)
670
+ .reverse() ?? [];
671
+ return {
672
+ mappedTypeKey: mappedType,
673
+ resolverType: directiveMappings[0] ?? 'Resolver',
674
+ };
675
+ })();
656
676
  const signature = {
657
677
  name: node.name,
658
678
  modifier: avoidResolverOptionals ? '' : '?',
@@ -699,6 +719,12 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
699
719
  applyOptionalFields(argsType, _fields) {
700
720
  return `Partial<${argsType}>`;
701
721
  }
722
+ clearOptional(str) {
723
+ if (str.startsWith('Maybe')) {
724
+ return str.replace(/Maybe<(.*?)>$/, '$1');
725
+ }
726
+ return str;
727
+ }
702
728
  ObjectTypeDefinition(node) {
703
729
  const declarationKind = 'type';
704
730
  const name = this.convertName(node, {
@@ -43,6 +43,9 @@ export class BaseResolversVisitor extends BaseVisitor {
43
43
  },
44
44
  resolversNonOptionalTypename: normalizeResolversNonOptionalTypename(getConfigValue(rawConfig.resolversNonOptionalTypename, false)),
45
45
  avoidCheckingAbstractTypesRecursively: getConfigValue(rawConfig.avoidCheckingAbstractTypesRecursively, false),
46
+ customDirectives: {
47
+ semanticNonNull: rawConfig.customDirectives?.semanticNonNull ?? false,
48
+ },
46
49
  ...additionalConfig,
47
50
  });
48
51
  this._schema = _schema;
@@ -606,17 +609,11 @@ export class BaseResolversVisitor extends BaseVisitor {
606
609
  const declarationKind = 'type';
607
610
  return (parentName, avoidResolverOptionals) => {
608
611
  const original = parent[key];
609
- const baseType = getBaseTypeNode(original.type);
610
- const realType = baseType.name.value;
611
612
  const parentType = this.schema.getType(parentName);
612
613
  if (this._federation.skipField({ fieldNode: original, parentType })) {
613
614
  return null;
614
615
  }
615
616
  const contextType = this.getContextType(parentName, node);
616
- const typeToUse = this.getTypeToUse(realType);
617
- const mappedType = this._variablesTransformer.wrapAstTypeWithModifiers(typeToUse, original.type);
618
- const subscriptionType = this._schema.getSubscriptionType();
619
- const isSubscriptionType = subscriptionType && subscriptionType.name === parentName;
620
617
  let argsType = hasArguments
621
618
  ? this.convertName(parentName +
622
619
  (this.config.addUnderscoreToArgsType ? '_' : '') +
@@ -643,12 +640,35 @@ export class BaseResolversVisitor extends BaseVisitor {
643
640
  parentType,
644
641
  parentTypeSignature: this.getParentTypeForSignature(node),
645
642
  });
646
- const mappedTypeKey = isSubscriptionType ? `${mappedType}, "${node.name}"` : mappedType;
647
- const directiveMappings = node.directives
648
- ?.map(directive => this._directiveResolverMappings[directive.name])
649
- .filter(Boolean)
650
- .reverse() ?? [];
651
- const resolverType = isSubscriptionType ? 'SubscriptionResolver' : directiveMappings[0] ?? 'Resolver';
643
+ const { mappedTypeKey, resolverType } = (() => {
644
+ const baseType = getBaseTypeNode(original.type);
645
+ const realType = baseType.name.value;
646
+ const typeToUse = this.getTypeToUse(realType);
647
+ /**
648
+ * Turns GraphQL type to TypeScript types (`mappedType`) e.g.
649
+ * - String! -> ResolversTypes['String']>
650
+ * - String -> Maybe<ResolversTypes['String']>
651
+ * - [String] -> Maybe<Array<Maybe<ResolversTypes['String']>>>
652
+ * - [String!]! -> Array<ResolversTypes['String']>
653
+ */
654
+ const mappedType = this._variablesTransformer.wrapAstTypeWithModifiers(typeToUse, original.type);
655
+ const subscriptionType = this._schema.getSubscriptionType();
656
+ const isSubscriptionType = subscriptionType && subscriptionType.name === parentName;
657
+ if (isSubscriptionType) {
658
+ return {
659
+ mappedTypeKey: `${mappedType}, "${node.name}"`,
660
+ resolverType: 'SubscriptionResolver',
661
+ };
662
+ }
663
+ const directiveMappings = node.directives
664
+ ?.map(directive => this._directiveResolverMappings[directive.name])
665
+ .filter(Boolean)
666
+ .reverse() ?? [];
667
+ return {
668
+ mappedTypeKey: mappedType,
669
+ resolverType: directiveMappings[0] ?? 'Resolver',
670
+ };
671
+ })();
652
672
  const signature = {
653
673
  name: node.name,
654
674
  modifier: avoidResolverOptionals ? '' : '?',
@@ -695,6 +715,12 @@ export class BaseResolversVisitor extends BaseVisitor {
695
715
  applyOptionalFields(argsType, _fields) {
696
716
  return `Partial<${argsType}>`;
697
717
  }
718
+ clearOptional(str) {
719
+ if (str.startsWith('Maybe')) {
720
+ return str.replace(/Maybe<(.*?)>$/, '$1');
721
+ }
722
+ return str;
723
+ }
698
724
  ObjectTypeDefinition(node) {
699
725
  const declarationKind = 'type';
700
726
  const name = this.convertName(node, {
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@graphql-codegen/visitor-plugin-common",
3
- "version": "5.7.2-alpha-20250314001208-26b650f2e9df4ac12ed3c6875084807691bbe9f0",
3
+ "version": "5.8.0-alpha-20250320115712-0c95c1869f1781093936579f0f8ebc1091082c5c",
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": "^2.0.0",
9
- "@graphql-codegen/plugin-helpers": "5.1.1-alpha-20250314001208-26b650f2e9df4ac12ed3c6875084807691bbe9f0",
9
+ "@graphql-codegen/plugin-helpers": "^5.1.0",
10
10
  "@graphql-tools/relay-operation-optimizer": "^7.0.0",
11
11
  "@graphql-tools/utils": "^10.0.0",
12
12
  "auto-bind": "~4.0.0",
@@ -14,7 +14,7 @@
14
14
  "graphql-tag": "^2.11.0",
15
15
  "parse-filepath": "^1.0.2",
16
16
  "change-case-all": "1.0.15",
17
- "tslib": "~2.8.0"
17
+ "tslib": "~2.6.0"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
@@ -32,6 +32,9 @@ export interface ParsedResolversConfig extends ParsedConfig {
32
32
  directiveResolverMappings: Record<string, string>;
33
33
  resolversNonOptionalTypename: ResolversNonOptionalTypenameConfig;
34
34
  avoidCheckingAbstractTypesRecursively: boolean;
35
+ customDirectives: {
36
+ semanticNonNull: boolean;
37
+ };
35
38
  }
36
39
  type FieldDefinitionPrintFn = (parentName: string, avoidResolverOptionals: boolean) => string | null;
37
40
  export interface RootResolver {
@@ -505,6 +508,31 @@ export interface RawResolversConfig extends RawConfig {
505
508
  * ```
506
509
  */
507
510
  enumSuffix?: boolean;
511
+ /**
512
+ * @description Configures behavior for custom directives from various GraphQL libraries.
513
+ * @exampleMarkdown
514
+ * ```ts filename="codegen.ts"
515
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
516
+ *
517
+ * const config: CodegenConfig = {
518
+ * // ...
519
+ * generates: {
520
+ * 'path/to/file.ts': {
521
+ * plugins: ['typescript-resolvers'],
522
+ * config: {
523
+ * customDirectives: {
524
+ * semanticNonNull: true
525
+ * }
526
+ * },
527
+ * },
528
+ * },
529
+ * };
530
+ * export default config;
531
+ * ```
532
+ */
533
+ customDirectives?: {
534
+ semanticNonNull?: boolean;
535
+ };
508
536
  /**
509
537
  * @default false
510
538
  * @description Sets the `__resolveType` field as optional field.
@@ -721,6 +749,7 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
721
749
  private getContextType;
722
750
  protected applyRequireFields(argsType: string, fields: InputValueDefinitionNode[]): string;
723
751
  protected applyOptionalFields(argsType: string, _fields: readonly InputValueDefinitionNode[]): string;
752
+ protected clearOptional(str: string): string;
724
753
  ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string;
725
754
  UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number, parent: any): string;
726
755
  ScalarTypeDefinition(node: ScalarTypeDefinitionNode): string;
@@ -32,6 +32,9 @@ export interface ParsedResolversConfig extends ParsedConfig {
32
32
  directiveResolverMappings: Record<string, string>;
33
33
  resolversNonOptionalTypename: ResolversNonOptionalTypenameConfig;
34
34
  avoidCheckingAbstractTypesRecursively: boolean;
35
+ customDirectives: {
36
+ semanticNonNull: boolean;
37
+ };
35
38
  }
36
39
  type FieldDefinitionPrintFn = (parentName: string, avoidResolverOptionals: boolean) => string | null;
37
40
  export interface RootResolver {
@@ -505,6 +508,31 @@ export interface RawResolversConfig extends RawConfig {
505
508
  * ```
506
509
  */
507
510
  enumSuffix?: boolean;
511
+ /**
512
+ * @description Configures behavior for custom directives from various GraphQL libraries.
513
+ * @exampleMarkdown
514
+ * ```ts filename="codegen.ts"
515
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
516
+ *
517
+ * const config: CodegenConfig = {
518
+ * // ...
519
+ * generates: {
520
+ * 'path/to/file.ts': {
521
+ * plugins: ['typescript-resolvers'],
522
+ * config: {
523
+ * customDirectives: {
524
+ * semanticNonNull: true
525
+ * }
526
+ * },
527
+ * },
528
+ * },
529
+ * };
530
+ * export default config;
531
+ * ```
532
+ */
533
+ customDirectives?: {
534
+ semanticNonNull?: boolean;
535
+ };
508
536
  /**
509
537
  * @default false
510
538
  * @description Sets the `__resolveType` field as optional field.
@@ -721,6 +749,7 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig
721
749
  private getContextType;
722
750
  protected applyRequireFields(argsType: string, fields: InputValueDefinitionNode[]): string;
723
751
  protected applyOptionalFields(argsType: string, _fields: readonly InputValueDefinitionNode[]): string;
752
+ protected clearOptional(str: string): string;
724
753
  ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string;
725
754
  UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number, parent: any): string;
726
755
  ScalarTypeDefinition(node: ScalarTypeDefinitionNode): string;