@graphql-codegen/typescript-resolvers

  • Version 4.0.6
  • Published
  • 54.6 kB
  • 6 dependencies
  • MIT license

Install

npm i @graphql-codegen/typescript-resolvers
yarn add @graphql-codegen/typescript-resolvers
pnpm add @graphql-codegen/typescript-resolvers

Overview

GraphQL Code Generator plugin for generating TypeScript types for resolvers signature

Index

Variables

variable plugin

const plugin: PluginFunction<
TypeScriptResolversPluginConfig,
Types.ComplexPluginOutput
>;

    Classes

    class TypeScriptResolversVisitor

    class TypeScriptResolversVisitor extends BaseResolversVisitor<
    TypeScriptResolversPluginConfig,
    ParsedTypeScriptResolversConfig
    > {}

      constructor

      constructor(
      pluginConfig: TypeScriptResolversPluginConfig,
      schema: GraphQLSchema
      );

        method buildEnumResolverContentBlock

        protected buildEnumResolverContentBlock: (
        node: EnumTypeDefinitionNode,
        mappedEnumType: string
        ) => string;

          method buildEnumResolversExplicitMappedValues

          protected buildEnumResolversExplicitMappedValues: (
          node: EnumTypeDefinitionNode,
          valuesMapping: { [valueName: string]: string | number }
          ) => string;

            method formatRootResolver

            protected formatRootResolver: (
            schemaTypeName: string,
            resolverType: string,
            declarationKind: DeclarationKind
            ) => string;

              method getParentTypeForSignature

              protected getParentTypeForSignature: (
              node: FieldDefinitionNode
              ) => 'ParentType' | 'UnwrappedObject<ParentType>';

                method getPunctuation

                protected getPunctuation: (_declarationKind: DeclarationKind) => string;

                  method ListType

                  ListType: (node: ListTypeNode) => string;

                    method NamedType

                    NamedType: (node: NamedTypeNode) => string;

                      method NonNullType

                      NonNullType: (node: NonNullTypeNode) => string;

                        method transformParentGenericType

                        protected transformParentGenericType: (parentType: string) => string;

                          method wrapWithListType

                          protected wrapWithListType: (str: string) => string;

                            Interfaces

                            interface TypeScriptResolversPluginConfig

                            interface TypeScriptResolversPluginConfig extends RawResolversConfig {}
                            • This plugin generates TypeScript signature for resolve functions of your GraphQL API. You can use this plugin to generate simple resolvers signature based on your GraphQL types, or you can change its behavior be providing custom model types (mappers).

                              You can find a blog post explaining the usage of this plugin here: https://the-guild.dev/blog/better-type-safety-for-resolvers-with-graphql-codegen

                            property allowParentTypeOverride

                            allowParentTypeOverride?: boolean;
                            • Allow you to override the ParentType generic in each resolver, by avoid enforcing the base type of the generated generic type.

                              This will generate ParentType = Type instead of ParentType extends Type = Type in each resolver.

                              import type { CodegenConfig } from '@graphql-codegen/cli';
                              const config: CodegenConfig = {
                              // ...
                              generates: {
                              'path/to/file.ts': {
                              plugins: ['typescript', 'typescript-resolvers'],
                              config: {
                              allowParentTypeOverride: true
                              },
                              },
                              },
                              };
                              export default config;

                            property customResolveInfo

                            customResolveInfo?: string;
                            • You can provide your custom GraphQLResolveInfo instead of the default one from graphql-js "graphql#GraphQLResolveInfo"

                              import type { CodegenConfig } from '@graphql-codegen/cli';
                              const config: CodegenConfig = {
                              // ...
                              generates: {
                              'path/to/file.ts': {
                              plugins: ['typescript', 'typescript-resolvers'],
                              config: {
                              customResolveInfo: './my-types#MyResolveInfo'
                              },
                              },
                              },
                              };
                              export default config;

                            property customResolverFn

                            customResolverFn?: string;
                            • You can provide your custom ResolveFn instead the default. It has to be a type that uses the generics <TResult, TParent, TContext, TArgs> "(parent: TParent, args: TArgs, context: TContext, info: GraphQLResolveInfo) => Promise | TResult"

                              ## Custom Signature

                              import type { CodegenConfig } from '@graphql-codegen/cli';
                              const config: CodegenConfig = {
                              // ...
                              generates: {
                              'path/to/file.ts': {
                              plugins: ['typescript', 'typescript-resolvers'],
                              config: {
                              customResolverFn: './my-types#MyResolveFn'
                              },
                              },
                              },
                              };
                              export default config;

                              ## With Graphile

                              import type { CodegenConfig } from '@graphql-codegen/cli';
                              const config: CodegenConfig = {
                              // ...
                              generates: {
                              'path/to/file.ts': {
                              plugins: ['typescript', 'typescript-resolvers'],
                              config: {
                              customResolverFn: './my-types#MyResolveFn'
                              },
                              },
                              },
                              };
                              export default config;

                              import type { CodegenConfig } from '@graphql-codegen/cli';
                              const config: CodegenConfig = {
                              // ...
                              generates: {
                              "path/to/file.ts": {
                              "plugins": [
                              {
                              "add": {
                              "content": "import { GraphileHelpers } from 'graphile-utils/node8plus/fieldHelpers';"
                              }
                              },
                              "typescript",
                              "typescript-resolvers"
                              ],
                              "config": {
                              "customResolverFn": "(\n parent: TParent,\n args: TArgs,\n context: TContext,\n info: GraphQLResolveInfo & { graphile: GraphileHelpers<TParent> }\n) => Promise<TResult> | TResult;\n"
                              }
                              }
                              }
                              };
                              export default config;

                            property directiveResolverMappings

                            directiveResolverMappings?: Record<string, string>;
                            • Map the usage of a directive into using a specific resolver.

                              import type { CodegenConfig } from '@graphql-codegen/cli';
                              const config: CodegenConfig = {
                              // ...
                              generates: {
                              'path/to/file.ts': {
                              plugins: ['typescript', 'typescript-resolvers'],
                              config: {
                              customResolverFn: '../resolver-types.ts#UnauthenticatedResolver',
                              directiveResolverMappings: {
                              authenticated: '../resolvers-types.ts#AuthenticatedResolver',
                              },
                              },
                              },
                              },
                              };
                              export default config;

                            property makeResolverTypeCallable

                            makeResolverTypeCallable?: boolean;
                            • Set to true in order to allow the Resolver type to be callable

                              import type { CodegenConfig } from '@graphql-codegen/cli';
                              const config: CodegenConfig = {
                              // ...
                              generates: {
                              'path/to/file.ts': {
                              plugins: ['typescript', 'typescript-resolvers'],
                              config: {
                              makeResolverTypeCallable: true
                              },
                              },
                              },
                              };
                              export default config;

                            property noSchemaStitching

                            noSchemaStitching?: boolean;
                            • Disables/Enables Schema Stitching support. By default, the resolver signature does not include the support for schema-stitching. Set to false to enable that.

                              true

                              import type { CodegenConfig } from '@graphql-codegen/cli';
                              const config: CodegenConfig = {
                              // ...
                              generates: {
                              'path/to/file.ts': {
                              plugins: ['typescript', 'typescript-resolvers'],
                              config: {
                              noSchemaStitching: false
                              },
                              },
                              },
                              };
                              export default config;

                            property optionalInfoArgument

                            optionalInfoArgument?: boolean;
                            • Sets info argument of resolver function to be optional field. Useful for testing.

                              import type { CodegenConfig } from '@graphql-codegen/cli';
                              const config: CodegenConfig = {
                              // ...
                              generates: {
                              'path/to/file.ts': {
                              plugins: ['typescript', 'typescript-resolvers'],
                              config: {
                              optionalInfoArgument: true
                              },
                              },
                              },
                              };
                              export default config;

                            property useIndexSignature

                            useIndexSignature?: boolean;
                            • Adds an index signature to any generates resolver. false

                              import type { CodegenConfig } from '@graphql-codegen/cli';
                              const config: CodegenConfig = {
                              // ...
                              generates: {
                              'path/to/file.ts': {
                              plugins: ['typescript', 'typescript-resolvers'],
                              config: {
                              useIndexSignature: true
                              },
                              },
                              },
                              };
                              export default config;

                            property wrapFieldDefinitions

                            wrapFieldDefinitions?: boolean;
                            • Set to true in order to wrap field definitions with FieldWrapper. This is useful to allow return types such as Promises and functions. Needed for compatibility with federation: true when true

                            Package Files (3)

                            Dependencies (6)

                            Dev Dependencies (0)

                            No dev dependencies.

                            Peer Dependencies (1)

                            Badge

                            To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                            You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@graphql-codegen/typescript-resolvers.

                            • Markdown
                              [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@graphql-codegen/typescript-resolvers)
                            • HTML
                              <a href="https://www.jsdocs.io/package/@graphql-codegen/typescript-resolvers"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>