From 1d20cdd3374f9c46f454566aecb7f6b4ba7a6509 Mon Sep 17 00:00:00 2001 From: Emil Yangirov Date: Thu, 22 Aug 2024 16:26:22 +0300 Subject: [PATCH] feat(presets/client): support enumValues option --- .changeset/yellow-eyes-poke.md | 5 +++ packages/presets/client/src/index.ts | 1 + .../client/tests/client-preset.spec.ts | 44 +++++++++++++++++++ .../client/tests/fixtures/with-enum-values.ts | 5 +++ website/src/pages/docs/guides/react-vue.mdx | 1 + .../pages/plugins/presets/preset-client.mdx | 1 + 6 files changed, 57 insertions(+) create mode 100644 .changeset/yellow-eyes-poke.md create mode 100644 packages/presets/client/tests/fixtures/with-enum-values.ts diff --git a/.changeset/yellow-eyes-poke.md b/.changeset/yellow-eyes-poke.md new file mode 100644 index 00000000000..0b4cb595da2 --- /dev/null +++ b/.changeset/yellow-eyes-poke.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/client-preset': patch +--- + +The `@graphql-codegen/client-preset` package now supports the `enumValues` option. diff --git a/packages/presets/client/src/index.ts b/packages/presets/client/src/index.ts index 7bf2cbeaea9..1f7251efc75 100644 --- a/packages/presets/client/src/index.ts +++ b/packages/presets/client/src/index.ts @@ -127,6 +127,7 @@ export const preset: Types.OutputPreset = { skipTypename: options.config.skipTypename, arrayInputCoercion: options.config.arrayInputCoercion, enumsAsTypes: options.config.enumsAsTypes, + enumValues: options.config.enumValues, futureProofEnums: options.config.futureProofEnums, dedupeFragments: options.config.dedupeFragments, nonOptionalTypename: options.config.nonOptionalTypename, diff --git a/packages/presets/client/tests/client-preset.spec.ts b/packages/presets/client/tests/client-preset.spec.ts index 82cb20abbd1..33097061863 100644 --- a/packages/presets/client/tests/client-preset.spec.ts +++ b/packages/presets/client/tests/client-preset.spec.ts @@ -2748,4 +2748,48 @@ export * from "./gql.js";`); `); }); }); + + it('support enumValues option', async () => { + const result = await executeCodegen({ + schema: [ + /* GraphQL */ ` + enum Color { + RED + BLUE + } + `, + ], + generates: { + 'out1/': { + preset, + config: { + enumValues: { + Color: './fixtures/with-enum-values#MyColor', + }, + }, + }, + }, + }); + + const graphqlFile = result.find(file => file.filename === 'out1/graphql.ts'); + expect(graphqlFile.content).toBeSimilarStringTo(`/* eslint-disable */ + import { MyColor as Color } from './fixtures/with-enum-values'; + export type Maybe = T | null; + export type InputMaybe = Maybe; + export type Exact = { [K in keyof T]: T[K] }; + export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; + export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; + export type MakeEmpty = { [_ in K]?: never }; + export type Incremental = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never }; + /** All built-in and custom scalars, mapped to their actual values */ + export type Scalars = { + ID: { input: string; output: string; } + String: { input: string; output: string; } + Boolean: { input: boolean; output: boolean; } + Int: { input: number; output: number; } + Float: { input: number; output: number; } + }; + + export { Color };`); + }); }); diff --git a/packages/presets/client/tests/fixtures/with-enum-values.ts b/packages/presets/client/tests/fixtures/with-enum-values.ts new file mode 100644 index 00000000000..2c93119f3ad --- /dev/null +++ b/packages/presets/client/tests/fixtures/with-enum-values.ts @@ -0,0 +1,5 @@ +export enum MyColor { + RED, + BLUE, + GREEN, +} diff --git a/website/src/pages/docs/guides/react-vue.mdx b/website/src/pages/docs/guides/react-vue.mdx index 9513a56c78d..777af044a96 100644 --- a/website/src/pages/docs/guides/react-vue.mdx +++ b/website/src/pages/docs/guides/react-vue.mdx @@ -508,6 +508,7 @@ The `client` preset allows the following `config` options: - [`skipTypename`](/plugins/typescript/typescript#skiptypename): Does not add `__typename` to the generated types, unless it was specified in the selection set. - [`arrayInputCoercion`](/plugins/typescript/typescript-operations#arrayinputcoercion): 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. - [`enumsAsTypes`](/plugins/typescript/typescript#enumsastypes): 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. +- [`enumValues`](/plugins/typescript/typescript#enumvalues): 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. - [`dedupeFragments`](/plugins/typescript/typescript#dedupefragments): Removes fragment duplicates for reducing data transfer. It is done by removing sub-fragments imports from fragment definition. - [`nonOptionalTypename`](/plugins/typescript/typescript#nonoptionaltypename): Automatically adds `__typename` field to the generated types, even when they are not specified in the selection set, and makes it non-optional. - [`avoidOptionals`](/plugins/typescript/typescript#avoidoptionals): This will cause the generator to avoid using TypeScript optionals (`?`) on types. diff --git a/website/src/pages/plugins/presets/preset-client.mdx b/website/src/pages/plugins/presets/preset-client.mdx index bfc97f03095..731e139fe73 100644 --- a/website/src/pages/plugins/presets/preset-client.mdx +++ b/website/src/pages/plugins/presets/preset-client.mdx @@ -52,6 +52,7 @@ The `client` preset allows the following `config` options: - [`skipTypename`](/plugins/typescript/typescript#skiptypename): Does not add `__typename` to the generated types, unless it was specified in the selection set. - [`arrayInputCoercion`](/plugins/typescript/typescript-operations#arrayinputcoercion): 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. - [`enumsAsTypes`](/plugins/typescript/typescript#enumsastypes): 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. +- [`enumValues`](/plugins/typescript/typescript#enumvalues): 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. - [`futureProofEnums`](/plugins/typescript/typescript#futureproofenums): Adds a catch-all entry to enum type definitions for values that may be added in the future. - [`dedupeFragments`](/plugins/typescript/typescript#dedupefragments): Removes fragment duplicates for reducing data transfer. It is done by removing sub-fragments imports from fragment definition. - [`nonOptionalTypename`](/plugins/typescript/typescript#nonoptionaltypename): Automatically adds `__typename` field to the generated types, even when they are not specified in the selection set, and makes it non-optional.