Skip to content

Commit 05aa6b4

Browse files
authored
feat(presets/client): support enumsAsConst option (#9981)
* feat(presets/client): support `enumsAsConst` option * Create shy-rabbits-juggle.md * docs: add `enumsAsConst` to `client` preset config * Add enumsAsConst to preset-client.mdx
1 parent b73ed8f commit 05aa6b4

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

.changeset/shy-rabbits-juggle.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 client preset now allows the use of the `enumsAsConst` config 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+
enumsAsConst: options.config.enumsAsConst,
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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,4 +2926,76 @@ export * from "./gql.js";`);
29262926
`);
29272927
});
29282928
});
2929+
2930+
it('support enumsAsConst option', async () => {
2931+
const result = await executeCodegen({
2932+
schema: [
2933+
/* GraphQL */ `
2934+
type Query {
2935+
thing: Thing
2936+
}
2937+
type Thing {
2938+
color: Color!
2939+
}
2940+
enum Color {
2941+
RED
2942+
BLUE
2943+
}
2944+
`,
2945+
],
2946+
documents: path.join(__dirname, 'fixtures/enum.ts'),
2947+
generates: {
2948+
'out1/': {
2949+
preset,
2950+
config: {
2951+
enumsAsConst: true,
2952+
},
2953+
},
2954+
},
2955+
});
2956+
const graphqlFile = result.find(file => file.filename === 'out1/graphql.ts');
2957+
expect(graphqlFile.content).toBeSimilarStringTo(`
2958+
/* eslint-disable */
2959+
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
2960+
export type Maybe<T> = T | null;
2961+
export type InputMaybe<T> = Maybe<T>;
2962+
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
2963+
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
2964+
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
2965+
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
2966+
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
2967+
/** All built-in and custom scalars, mapped to their actual values */
2968+
export type Scalars = {
2969+
ID: { input: string; output: string; }
2970+
String: { input: string; output: string; }
2971+
Boolean: { input: boolean; output: boolean; }
2972+
Int: { input: number; output: number; }
2973+
Float: { input: number; output: number; }
2974+
};
2975+
2976+
export const Color = {
2977+
Blue: 'BLUE',
2978+
Red: 'RED'
2979+
} as const;
2980+
2981+
export type Color = typeof Color[keyof typeof Color];
2982+
export type Query = {
2983+
__typename?: 'Query';
2984+
thing?: Maybe<Thing>;
2985+
};
2986+
2987+
export type Thing = {
2988+
__typename?: 'Thing';
2989+
color: Color;
2990+
};
2991+
2992+
export type FavoriteColorQueryVariables = Exact<{ [key: string]: never; }>;
2993+
2994+
2995+
export type FavoriteColorQuery = { __typename?: 'Query', thing?: { __typename?: 'Thing', color: Color } | null };
2996+
2997+
2998+
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>;
2999+
`);
3000+
});
29293001
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint-disable @typescript-eslint/ban-ts-comment */
2+
//@ts-ignore
3+
import gql from 'gql-tag';
4+
5+
//@ts-ignore
6+
const A = gql(/* GraphQL */ `
7+
query FavoriteColor {
8+
thing {
9+
color
10+
}
11+
}
12+
`);

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+
- [`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.
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+
- [`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.
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)