@graphql-codegen/typescript-operations

  • Version 2.5.8
  • Published
  • 42.4 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,
      allFragments: LoadedFragment[]
      );

        method applyVariablesWrapper

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

          method getImports

          getImports: () => Array<string>;

            method getPunctuation

            protected getPunctuation: (_declarationKind: DeclarationKind) => 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.

                Note: In most configurations, this plugin requires you to use `typescript as well, because it depends on its base types.

              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: {
                  "./typings/api.ts": {
                  "plugins": [
                  "typescript"
                  ]
                  },
                  "./": {
                  "preset": "near-operation-file",
                  "presetConfig": {
                  "baseTypesPath": "./typings/api.ts",
                  "extension": ".gql.d.ts"
                  },
                  "plugins": [
                  "@graphql-codegen/typescript-operations"
                  ],
                  "config": {
                  "addOperationExport": 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'],
                config: {
                arrayInputCoercion: false
                },
                },
                },
                };
                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 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', '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', 'typescript-operations'],
                config: {
                flattenGeneratedTypes: true,
                flattenGeneratedTypesIncludeFragments: true
                },
                },
                },
                };
                export default config;

              property globalNamespace

              globalNamespace?: boolean;

                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 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'],
                  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

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

                Package Files (3)

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