Skip to content

Commit 98392fc

Browse files
yangiroveddeee888
andauthored
feat(presets/client): support enumValues option (#10120)
Co-authored-by: Eddy Nguyen <[email protected]>
1 parent 157c823 commit 98392fc

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
@@ -132,6 +132,7 @@ export const preset: Types.OutputPreset<ClientPresetConfig> = {
132132
arrayInputCoercion: options.config.arrayInputCoercion,
133133
enumsAsTypes: options.config.enumsAsTypes,
134134
enumsAsConst: options.config.enumsAsConst,
135+
enumValues: options.config.enumValues,
135136
futureProofEnums: options.config.futureProofEnums,
136137
dedupeFragments: options.config.dedupeFragments,
137138
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
@@ -3146,4 +3146,48 @@ export * from "./gql.js";`);
31463146
export const FavoriteColorDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"FavoriteColor"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"thing"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"color"}}]}}]}}]} as unknown as DocumentNode<FavoriteColorQuery, FavoriteColorQueryVariables>;
31473147
`);
31483148
});
3149+
3150+
it('support enumValues option', async () => {
3151+
const result = await executeCodegen({
3152+
schema: [
3153+
/* GraphQL */ `
3154+
enum Color {
3155+
RED
3156+
BLUE
3157+
}
3158+
`,
3159+
],
3160+
generates: {
3161+
'out1/': {
3162+
preset,
3163+
config: {
3164+
enumValues: {
3165+
Color: './fixtures/with-enum-values#MyColor',
3166+
},
3167+
},
3168+
},
3169+
},
3170+
});
3171+
3172+
const graphqlFile = result.find(file => file.filename === 'out1/graphql.ts');
3173+
expect(graphqlFile.content).toBeSimilarStringTo(`/* eslint-disable */
3174+
import { MyColor as Color } from './fixtures/with-enum-values';
3175+
export type Maybe<T> = T | null;
3176+
export type InputMaybe<T> = Maybe<T>;
3177+
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
3178+
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
3179+
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
3180+
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
3181+
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
3182+
/** All built-in and custom scalars, mapped to their actual values */
3183+
export type Scalars = {
3184+
ID: { input: string; output: string; }
3185+
String: { input: string; output: string; }
3186+
Boolean: { input: boolean; output: boolean; }
3187+
Int: { input: number; output: number; }
3188+
Float: { input: number; output: number; }
3189+
};
3190+
3191+
export { Color };`);
3192+
});
31493193
});
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
@@ -509,6 +509,7 @@ The `client` preset allows the following `config` options:
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.
511511
- [`enumsAsConst`](/plugins/typescript/typescript#enumsasconst): 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.
512+
- [`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.
512513
- [`dedupeFragments`](/plugins/typescript/typescript#dedupefragments): Removes fragment duplicates for reducing data transfer. It is done by removing sub-fragments imports from fragment definition.
513514
- [`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.
514515
- [`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
@@ -53,6 +53,7 @@ The `client` preset allows the following `config` options:
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.
5555
- [`enumsAsConst`](/plugins/typescript/typescript#enumsasconst): 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.
56+
- [`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.
5657
- [`futureProofEnums`](/plugins/typescript/typescript#futureproofenums): Adds a catch-all entry to enum type definitions for values that may be added in the future.
5758
- [`dedupeFragments`](/plugins/typescript/typescript#dedupefragments): Removes fragment duplicates for reducing data transfer. It is done by removing sub-fragments imports from fragment definition.
5859
- [`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)