Skip to content

Commit 1d20cdd

Browse files
author
Emil Yangirov
committed
feat(presets/client): support enumValues option
1 parent 6a030b3 commit 1d20cdd

File tree

6 files changed

+57
-0
lines changed

6 files changed

+57
-0
lines changed

.changeset/yellow-eyes-poke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/client-preset': patch
3+
---
4+
5+
The `@graphql-codegen/client-preset` package now supports the `enumValues` option.

packages/presets/client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export const preset: Types.OutputPreset<ClientPresetConfig> = {
127127
skipTypename: options.config.skipTypename,
128128
arrayInputCoercion: options.config.arrayInputCoercion,
129129
enumsAsTypes: options.config.enumsAsTypes,
130+
enumValues: options.config.enumValues,
130131
futureProofEnums: options.config.futureProofEnums,
131132
dedupeFragments: options.config.dedupeFragments,
132133
nonOptionalTypename: options.config.nonOptionalTypename,

packages/presets/client/tests/client-preset.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,4 +2748,48 @@ export * from "./gql.js";`);
27482748
`);
27492749
});
27502750
});
2751+
2752+
it('support enumValues option', async () => {
2753+
const result = await executeCodegen({
2754+
schema: [
2755+
/* GraphQL */ `
2756+
enum Color {
2757+
RED
2758+
BLUE
2759+
}
2760+
`,
2761+
],
2762+
generates: {
2763+
'out1/': {
2764+
preset,
2765+
config: {
2766+
enumValues: {
2767+
Color: './fixtures/with-enum-values#MyColor',
2768+
},
2769+
},
2770+
},
2771+
},
2772+
});
2773+
2774+
const graphqlFile = result.find(file => file.filename === 'out1/graphql.ts');
2775+
expect(graphqlFile.content).toBeSimilarStringTo(`/* eslint-disable */
2776+
import { MyColor as Color } from './fixtures/with-enum-values';
2777+
export type Maybe<T> = T | null;
2778+
export type InputMaybe<T> = Maybe<T>;
2779+
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
2780+
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
2781+
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
2782+
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
2783+
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
2784+
/** All built-in and custom scalars, mapped to their actual values */
2785+
export type Scalars = {
2786+
ID: { input: string; output: string; }
2787+
String: { input: string; output: string; }
2788+
Boolean: { input: boolean; output: boolean; }
2789+
Int: { input: number; output: number; }
2790+
Float: { input: number; output: number; }
2791+
};
2792+
2793+
export { Color };`);
2794+
});
27512795
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum MyColor {
2+
RED,
3+
BLUE,
4+
GREEN,
5+
}

website/src/pages/docs/guides/react-vue.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ The `client` preset allows the following `config` options:
508508
- [`skipTypename`](/plugins/typescript/typescript#skiptypename): Does not add `__typename` to the generated types, unless it was specified in the selection set.
509509
- [`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.
510510
- [`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.
511+
- [`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.
511512
- [`dedupeFragments`](/plugins/typescript/typescript#dedupefragments): Removes fragment duplicates for reducing data transfer. It is done by removing sub-fragments imports from fragment definition.
512513
- [`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.
513514
- [`avoidOptionals`](/plugins/typescript/typescript#avoidoptionals): This will cause the generator to avoid using TypeScript optionals (`?`) on types.

website/src/pages/plugins/presets/preset-client.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ The `client` preset allows the following `config` options:
5252
- [`skipTypename`](/plugins/typescript/typescript#skiptypename): Does not add `__typename` to the generated types, unless it was specified in the selection set.
5353
- [`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.
5454
- [`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.
55+
- [`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.
5556
- [`futureProofEnums`](/plugins/typescript/typescript#futureproofenums): Adds a catch-all entry to enum type definitions for values that may be added in the future.
5657
- [`dedupeFragments`](/plugins/typescript/typescript#dedupefragments): Removes fragment duplicates for reducing data transfer. It is done by removing sub-fragments imports from fragment definition.
5758
- [`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.

0 commit comments

Comments
 (0)