@graphql-codegen/typescript-operations

  • Version 6.0.3
  • Published
  • 116 kB
  • 5 dependencies
  • MIT license

Install

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

Overview

GraphQL Code Generator plugin for generating TypeScript types for GraphQL queries, mutations, subscriptions and fragments

Index

Variables

variable plugin

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

    Classes

    class TypeScriptDocumentsVisitor

    class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
    TypeScriptDocumentsPluginConfig,
    TypeScriptDocumentsParsedConfig
    > {}

      constructor

      constructor(
      schema: GraphQLSchema,
      config: TypeScriptDocumentsPluginConfig,
      documentNode: DocumentNode,
      outputPath: string
      );

        method applyVariablesWrapper

        protected applyVariablesWrapper: (
        variablesBlock: string,
        operationType: string
        ) => string;

          method EnumTypeDefinition

          EnumTypeDefinition: (node: EnumTypeDefinitionNode) => string | null;

            method getEnumsImports

            getEnumsImports: () => string[];

              method getExactUtilityType

              getExactUtilityType: () => string | null;

                method getExternalSchemaTypeImports

                getExternalSchemaTypeImports: () => Array<string>;

                  method getImports

                  getImports: () => Array<string>;

                    method getIncrementalUtilityType

                    getIncrementalUtilityType: () => string | null;

                      method getPunctuation

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

                        method getScalarsImports

                        getScalarsImports: () => string[];

                          method InputObjectTypeDefinition

                          InputObjectTypeDefinition: (
                          node: InputObjectTypeDefinitionNode
                          ) => string | null;

                            method InputValueDefinition

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

                              Interfaces

                              interface TypeScriptDocumentsPluginConfig

                              interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {}
                              • This plugin generates TypeScript types based on your GraphQLSchema _and_ your GraphQL operations and fragments. It generates types for your GraphQL documents: Query, Mutation, Subscription and Fragment.

                              property addOperationExport

                              addOperationExport?: boolean;
                              • addOperationExport boolean Add const export of the operation name to output file. Pay attention that the file should be d.ts. You can combine it with near-operation-file preset and therefore the types will be generated along with graphql file. Then you need to set extension in presetConfig to be .gql.d.ts and by that you can import gql file in ts files. It will allow you to get everything with one import:

                                import { GetClient, GetClientQuery, GetClientQueryVariables } from './GetClient.gql.js'

                                false

                                See Also

                                • https://github.com/dotansimha/graphql-code-generator/issues/3949

                                  import type { CodegenConfig } from '@graphql-codegen/cli';
                                  const config: CodegenConfig = {
                                  // ...
                                  generates: {
                                  "./": {
                                  "preset": "near-operation-file",
                                  "presetConfig": {
                                  "baseTypesPath": "./typings/api.ts",
                                  "extension": ".gql.d.ts"
                                  },
                                  "plugins": [
                                  "typescript-operations"
                                  ],
                                  "config": {
                                  "addOperationExport": true
                                  }
                                  }
                                  }
                                  };
                                  export default config;

                              property allowUndefinedQueryVariables

                              allowUndefinedQueryVariables?: boolean;
                              • Adds undefined as a possible type for query variables false

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

                              property arrayInputCoercion

                              arrayInputCoercion?: boolean;
                              • The [GraphQL spec](https://spec.graphql.org/draft/#sel-FAHjBJFCAACE_Gh7d) allows arrays and a single primitive value for list input. This allows to deactivate that behavior to only accept arrays instead of single values. If set to false, the definition: query foo(bar: [Int!]!): Foo will output bar: Array<Int> instead of bar: Array<Int> | Int for the variable part. true

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

                              property enumType

                              enumType?: ConvertSchemaEnumToDeclarationBlockString['outputType'];
                              • Controls the enum output type. Options: string-literal | native-numeric | const | native-const | native; string-literal

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

                              property enumValues

                              enumValues?: EnumValuesMap;
                              • Overrides the default value of enum values declared in your GraphQL schema. You can also map the entire enum to an external type by providing a string that of module#type.

                                ## With Custom Values

                                import type { CodegenConfig } from '@graphql-codegen/cli';
                                const config: CodegenConfig = {
                                // ...
                                generates: {
                                'path/to/file': {
                                // plugins...
                                config: {
                                enumValues: {
                                MyEnum: {
                                A: 'foo'
                                }
                                }
                                },
                                },
                                },
                                };
                                export default config;

                                ## With External Enum

                                import type { CodegenConfig } from '@graphql-codegen/cli';
                                const config: CodegenConfig = {
                                // ...
                                generates: {
                                'path/to/file': {
                                // plugins...
                                config: {
                                enumValues: {
                                MyEnum: './my-file#MyCustomEnum',
                                }
                                },
                                },
                                },
                                };
                                export default config;

                                ## Import All Enums from a file

                                import type { CodegenConfig } from '@graphql-codegen/cli';
                                const config: CodegenConfig = {
                                // ...
                                generates: {
                                'path/to/file': {
                                // plugins...
                                config: {
                                enumValues: {
                                MyEnum: './my-file',
                                }
                                },
                                },
                                },
                                };
                                export default config;

                              property flattenGeneratedTypes

                              flattenGeneratedTypes?: boolean;
                              • Flatten fragment spread and inline fragments into a simple selection set before generating. false

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

                              property flattenGeneratedTypesIncludeFragments

                              flattenGeneratedTypesIncludeFragments?: boolean;
                              • Include all fragments types when flattenGeneratedTypes is enabled. false

                                import type { CodegenConfig } from '@graphql-codegen/cli';
                                const config: CodegenConfig = {
                                // ...
                                generates: {
                                'path/to/file.ts': {
                                plugins: ['typescript-operations'],
                                config: {
                                flattenGeneratedTypes: true,
                                flattenGeneratedTypesIncludeFragments: 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-operations'],
                                config: {
                                futureProofEnums: true
                                }
                                }
                                }
                                }
                                export default config

                              property ignoreEnumValuesFromSchema

                              ignoreEnumValuesFromSchema?: boolean;
                              • This will cause the generator to ignore enum values defined in GraphQLSchema false

                                ## Ignore enum values from schema

                                import type { CodegenConfig } from '@graphql-codegen/cli';
                                const config: CodegenConfig = {
                                // ...
                                generates: {
                                'path/to/file': {
                                // plugins...
                                config: {
                                ignoreEnumValuesFromSchema: 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-operations'],
                                config: {
                                immutableTypes: true
                                },
                                },
                                },
                                };
                                export default config;

                              property inputMaybeValue

                              inputMaybeValue?: string;
                              • Allows overriding the type of Input and Variables nullable types. T | null | undefined

                                ## Disallow undefined Disallowing undefined is useful if you want to force explicit null to be passed in as Variables to the server. Use inputMaybeValue: 'T | null' with avoidOptionals.inputValue: true to achieve this.

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

                              property maybeValue

                              maybeValue?: string;
                              • Allows overriding the type value of nullable fields to match GraphQL client's runtime behaviour. T | null

                                ## Allow undefined By default, a GraphQL server will return either the expected type or null for a nullable field. maybeValue option could be used to change this behaviour if your GraphQL client does something different such as returning undefined.

                                import type { CodegenConfig } from '@graphql-codegen/cli';
                                const config: CodegenConfig = {
                                // ...
                                generates: {
                                'path/to/file.ts': {
                                plugins: ['typescript-operations'],
                                config: {
                                maybeValue: 'T | null | undefined'
                                },
                                },
                                },
                                };
                                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

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

                              property nullability

                              nullability?: {
                              errorHandlingClient: boolean;
                              };
                              • Options related to handling nullability ## errorHandlingClient An error handling client is a client which prevents the user from reading a null used as a placeholder for an error in a GraphQL response. The client may do so by throwing when an errored field is accessed (as is the case for [graphql-toe](https://github.com/graphile/graphql-toe)), or when a fragment containing an error is read (as is the case for Relay's @throwOnFieldError directive), or by preventing any data from being read if an error occurred (as with Apollo Client's errorPolicy: "none").

                                When using error handling clients, a semantic non-nullable field can never be null. If a semantic non-nullable field's value in the response is null, there must be a respective error. The error handling client will throw in this case, so the null value is never read.

                                To enable this option, install graphql-sock peer dependency:

                                npm install -D graphql-sock

                                Now, you can enable support for error handling clients:

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

                              Package Files (3)

                              Dependencies (5)

                              Dev Dependencies (0)

                              No dev dependencies.

                              Peer Dependencies (2)

                              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-operations.

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