@graphql-codegen/typescript

  • Version 6.0.1
  • Published
  • 79.2 kB
  • 5 dependencies
  • MIT license

Install

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

Overview

GraphQL Code Generator plugin for generating TypeScript types

Index

Variables

variable plugin

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

    Functions

    function includeIntrospectionTypesDefinitions

    includeIntrospectionTypesDefinitions: (
    schema: GraphQLSchema,
    documents: Types.DocumentFile[],
    config: TypeScriptPluginConfig
    ) => string[];

      Classes

      class TsIntrospectionVisitor

      class TsIntrospectionVisitor extends TsVisitor {}

        constructor

        constructor(
        schema: GraphQLSchema,
        pluginConfig: TypeScriptPluginConfig,
        typesToInclude: GraphQLNamedType[]
        );

          method DirectiveDefinition

          DirectiveDefinition: () => any;

            method EnumTypeDefinition

            EnumTypeDefinition: (node: EnumTypeDefinitionNode) => string;

              method ObjectTypeDefinition

              ObjectTypeDefinition: (
              node: ObjectTypeDefinitionNode,
              key: string | number,
              parent: any
              ) => string;

                class TsVisitor

                class TsVisitor<
                TRawConfig extends TypeScriptPluginConfig = TypeScriptPluginConfig,
                TParsedConfig extends TypeScriptPluginParsedConfig = TypeScriptPluginParsedConfig
                > extends BaseTypesVisitor<TRawConfig, TParsedConfig> {}

                  constructor

                  constructor(
                  schema: GraphQLSchema,
                  pluginConfig: TypeScriptPluginConfig,
                  additionalConfig?: Partial<TParsedConfig>
                  );

                    method clearOptional

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

                      method EnumTypeDefinition

                      EnumTypeDefinition: (node: EnumTypeDefinitionNode) => string;

                        method FieldDefinition

                        FieldDefinition: (
                        node: FieldDefinitionNode,
                        key?: number | string,
                        parent?: any
                        ) => string;

                          method getExportPrefix

                          protected getExportPrefix: () => string;

                            method getInputMaybeValue

                            getInputMaybeValue: () => string;

                              method getMaybeValue

                              getMaybeValue: () => string;

                                method getMaybeWrapper

                                getMaybeWrapper: (ancestors: any) => string;

                                  method getPunctuation

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

                                    method getWrapperDefinitions

                                    getWrapperDefinitions: () => string[];

                                      method InputValueDefinition

                                      InputValueDefinition: (
                                      node: InputValueDefinitionNode,
                                      key?: number | string,
                                      parent?: any,
                                      _path?: Array<string | number>,
                                      ancestors?: Array<TypeDefinitionNode>
                                      ) => string;

                                        method ListType

                                        ListType: (
                                        node: ListTypeNode,
                                        key: any,
                                        parent: any,
                                        path: any,
                                        ancestors: any
                                        ) => string;

                                          method NamedType

                                          NamedType: (
                                          node: NamedTypeNode,
                                          key: any,
                                          parent: any,
                                          path: any,
                                          ancestors: any
                                          ) => string;

                                            method NonNullType

                                            NonNullType: (node: NonNullTypeNode) => string;

                                              method UnionTypeDefinition

                                              UnionTypeDefinition: (
                                              node: UnionTypeDefinitionNode,
                                              key: string | number | undefined,
                                              parent: any
                                              ) => string;

                                                method wrapWithListType

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

                                                  class TypeScriptOperationVariablesToObject

                                                  class TypeScriptOperationVariablesToObject extends OperationVariablesToObject {}

                                                    constructor

                                                    constructor(
                                                    _scalars: NormalizedScalarsMap,
                                                    _convertName: ConvertNameFn,
                                                    _avoidOptionals: NormalizedAvoidOptionalsConfig,
                                                    _immutableTypes: boolean,
                                                    _namespacedImportName?: string,
                                                    _enumNames?: string[],
                                                    _enumPrefix?: boolean,
                                                    _enumSuffix?: boolean,
                                                    _enumValues?: ParsedEnumValuesMap,
                                                    _applyCoercion?: boolean,
                                                    _directiveArgumentAndInputFieldMappings?: ParsedDirectiveArgumentAndInputFieldMappings,
                                                    _maybeType?: string
                                                    );

                                                      method clearOptional

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

                                                        method formatFieldString

                                                        protected formatFieldString: (
                                                        fieldName: string,
                                                        isNonNullType: boolean,
                                                        hasDefaultValue: boolean
                                                        ) => string;

                                                          method formatTypeString

                                                          protected formatTypeString: (
                                                          fieldType: string,
                                                          isNonNullType: boolean,
                                                          hasDefaultValue: boolean
                                                          ) => string;

                                                            method getAvoidOption

                                                            protected getAvoidOption: (
                                                            isNonNullType: boolean,
                                                            hasDefaultValue: boolean
                                                            ) => boolean;

                                                              method getPunctuation

                                                              protected getPunctuation: () => string;

                                                                method wrapAstTypeWithModifiers

                                                                wrapAstTypeWithModifiers: (
                                                                baseType: string,
                                                                typeNode: TypeNode,
                                                                applyCoercion?: boolean
                                                                ) => string;

                                                                  method wrapMaybe

                                                                  protected wrapMaybe: (type?: string) => string;

                                                                    Interfaces

                                                                    interface TypeScriptPluginConfig

                                                                    interface TypeScriptPluginConfig extends RawTypesConfig {}
                                                                    • This plugin generates the base TypeScript types, based on your GraphQL schema.

                                                                      The types generated by this plugin are simple, and refers to the exact structure of your schema, and it's used as the base types for other plugins (such as typescript-operations / typescript-resolvers)

                                                                    property allowEnumStringTypes

                                                                    allowEnumStringTypes?: boolean;
                                                                    • Allow using enum string values directly.

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

                                                                    property avoidOptionals

                                                                    avoidOptionals?: boolean | AvoidOptionalsConfig;
                                                                    • This will cause the generator to avoid using TypeScript optionals (?) on types, so the following definition: type A { myField: String } will output myField: Maybe<string> instead of myField?: Maybe<string>. false

                                                                      ## Override all definition types

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

                                                                      ## Override only specific definition types

                                                                      import type { CodegenConfig } from '@graphql-codegen/cli'
                                                                      const config: CodegenConfig = {
                                                                      // ...
                                                                      generates: {
                                                                      'path/to/file.ts': {
                                                                      plugins: ['typescript'],
                                                                      config: {
                                                                      avoidOptionals: {
                                                                      field: true
                                                                      inputValue: true
                                                                      object: true
                                                                      defaultValue: true
                                                                      }
                                                                      }
                                                                      }
                                                                      }
                                                                      }
                                                                      export default config

                                                                    property constEnums

                                                                    constEnums?: boolean;
                                                                    • Will prefix every generated enum with const, you can read more about const enums here: https://www.typescriptlang.org/docs/handbook/enums.html. false

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

                                                                    property disableDescriptions

                                                                    disableDescriptions?: boolean;
                                                                    • Set the value to true in order to disable all description generation. false

                                                                      ## Disable description generation

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

                                                                    property entireFieldWrapperValue

                                                                    entireFieldWrapperValue?: string;
                                                                    • entireFieldWrapperValue string Allow to override the type value of EntireFieldWrapper. This wrapper applies outside of Array and Maybe unlike fieldWrapperValue, that will wrap the inner type. T | Promise | (() => T | Promise)

                                                                      Example 1

                                                                      Only allow values

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

                                                                    property enumsAsConst

                                                                    enumsAsConst?: boolean;
                                                                    • Generates enum as TypeScript const assertions instead of enum. This can even be used to enable enum-like patterns in plain JavaScript code if you choose not to use TypeScript’s enum construct. false

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

                                                                    property enumsAsTypes

                                                                    enumsAsTypes?: boolean;
                                                                    • Generates enum as TypeScript string union type instead of an enum. Useful if you wish to generate .d.ts declaration file instead of .ts, or if you want to avoid using TypeScript enums due to bundle size concerns false

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

                                                                    property futureProofEnums

                                                                    futureProofEnums?: boolean;
                                                                    • This option controls whether or not a catch-all entry is added to enum type definitions for values that may be added in the future. This is useful if you are using relay. false

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

                                                                    property futureProofUnions

                                                                    futureProofUnions?: boolean;
                                                                    • This option controls whether or not a catch-all entry is added to union type definitions for values that may be added in the future. This is useful if you are using relay. false

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

                                                                    property immutableTypes

                                                                    immutableTypes?: boolean;
                                                                    • Generates immutable types by adding readonly to properties and uses ReadonlyArray. false

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

                                                                    property inputMaybeValue

                                                                    inputMaybeValue?: string;
                                                                    • Allow to override the type value of Maybe for input types and arguments. This is useful in case you want to differentiate between the wrapper of input and output types. By default, this type just refers to Maybe type, but you can override its definition.

                                                                      Maybe

                                                                      ## Allow undefined

                                                                      import type { CodegenConfig } from '@graphql-codegen/cli'
                                                                      const config: CodegenConfig = {
                                                                      // ...
                                                                      generates: {
                                                                      'path/to/file.ts': {
                                                                      plugins: ['typescript'],
                                                                      config: {
                                                                      inputMaybeValue: 'T | null | undefined'
                                                                      }
                                                                      }
                                                                      }
                                                                      }
                                                                      export default config

                                                                      ## Allow null in resolvers:

                                                                      import type { CodegenConfig } from '@graphql-codegen/cli'
                                                                      const config: CodegenConfig = {
                                                                      // ...
                                                                      generates: {
                                                                      'path/to/file.ts': {
                                                                      plugins: ['typescript'],
                                                                      config: {
                                                                      inputMaybeValue: 'T extends PromiseLike<infer U> ? Promise<U | null> : T | null'
                                                                      }
                                                                      }
                                                                      }
                                                                      }
                                                                      export default config

                                                                    property maybeValue

                                                                    maybeValue?: string;
                                                                    • Allow to override the type value of Maybe. T | null

                                                                      ## Allow undefined

                                                                      import type { CodegenConfig } from '@graphql-codegen/cli'
                                                                      const config: CodegenConfig = {
                                                                      // ...
                                                                      generates: {
                                                                      'path/to/file.ts': {
                                                                      plugins: ['typescript'],
                                                                      config: {
                                                                      maybeValue: 'T | null | undefined'
                                                                      }
                                                                      }
                                                                      }
                                                                      }
                                                                      export default config

                                                                      ## Allow null in resolvers:

                                                                      import type { CodegenConfig } from '@graphql-codegen/cli'
                                                                      const config: CodegenConfig = {
                                                                      // ...
                                                                      generates: {
                                                                      'path/to/file.ts': {
                                                                      plugins: ['typescript', 'typescript-resolvers'],
                                                                      config: {
                                                                      maybeValue: 'T extends PromiseLike<infer U> ? Promise<U | null> : T | null'
                                                                      }
                                                                      }
                                                                      }
                                                                      }
                                                                      export default config

                                                                    property noExport

                                                                    noExport?: boolean;
                                                                    • Set to true in order to generate output without export modifier. This is useful if you are generating .d.ts file and want it to be globally available. false

                                                                      ## Disable all export from a file

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

                                                                    property numericEnums

                                                                    numericEnums?: boolean;
                                                                    • Controls whether to preserve typescript enum values as numbers false

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

                                                                    property onlyEnums

                                                                    onlyEnums?: boolean;
                                                                    • This will cause the generator to emit types for enums only. false

                                                                      Override all definition types

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

                                                                    property onlyOperationTypes

                                                                    onlyOperationTypes?: boolean;
                                                                    • This will cause the generator to emit types for operations only (basically only enums and scalars). Interacts well with preResolveTypes: true false

                                                                      Override all definition types

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

                                                                    property useImplementingTypes

                                                                    useImplementingTypes?: boolean;
                                                                    • When a GraphQL interface is used for a field, this flag will use the implementing types, instead of the interface itself. false

                                                                      ## Override all definition types

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

                                                                    property wrapEntireFieldDefinitions

                                                                    wrapEntireFieldDefinitions?: boolean;
                                                                    • wrapEntireFieldDefinitions boolean Set to true in order to wrap field definitions with EntireFieldWrapper. This is useful to allow return types such as Promises and functions for fields. Differs from wrapFieldDefinitions in that this wraps the entire field definition if i.e. the field is an Array, while wrapFieldDefinitions will wrap every single value inside the array. false

                                                                      Example 1

                                                                      Enable wrapping entire fields

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

                                                                    interface TypeScriptPluginParsedConfig

                                                                    interface TypeScriptPluginParsedConfig extends ParsedTypesConfig {}

                                                                      property avoidOptionals

                                                                      avoidOptionals: NormalizedAvoidOptionalsConfig;

                                                                        property constEnums

                                                                        constEnums: boolean;

                                                                          property enumsAsConst

                                                                          enumsAsConst: boolean;

                                                                            property enumsAsTypes

                                                                            enumsAsTypes: boolean;

                                                                              property futureProofEnums

                                                                              futureProofEnums: boolean;

                                                                                property futureProofUnions

                                                                                futureProofUnions: boolean;

                                                                                  property immutableTypes

                                                                                  immutableTypes: boolean;

                                                                                    property inputMaybeValue

                                                                                    inputMaybeValue: string;

                                                                                      property maybeValue

                                                                                      maybeValue: string;

                                                                                        property noExport

                                                                                        noExport: boolean;

                                                                                          property numericEnums

                                                                                          numericEnums: boolean;

                                                                                            property onlyEnums

                                                                                            onlyEnums: boolean;

                                                                                              property onlyOperationTypes

                                                                                              onlyOperationTypes: boolean;

                                                                                                property useImplementingTypes

                                                                                                useImplementingTypes: boolean;

                                                                                                  Package Files (5)

                                                                                                  Dependencies (5)

                                                                                                  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.

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