Skip to content

Commit 87184aa

Browse files
authored
[client-preset] Forward immutableTypes option to client preset (#10459)
* Forward immutableTypes to client preset * Add changeset
1 parent 17b6afc commit 87184aa

File tree

5 files changed

+109
-50
lines changed

5 files changed

+109
-50
lines changed

.changeset/gold-zoos-show.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': minor
3+
---
4+
5+
Forward immutableTypes to client preset config

packages/presets/client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export const preset: Types.OutputPreset<ClientPresetConfig> = {
141141
onlyOperationTypes: options.config.onlyOperationTypes,
142142
onlyEnums: options.config.onlyEnums,
143143
customDirectives: options.config.customDirectives,
144+
immutableTypes: options.config.immutableTypes,
144145
};
145146

146147
const visitor = new ClientSideBaseVisitor(options.schemaAst, [], options.config, options.config);

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3190,4 +3190,85 @@ export * from "./gql.js";`);
31903190
31913191
export { Color };`);
31923192
});
3193+
3194+
it('supports immutableTypes', async () => {
3195+
const { result } = await executeCodegen({
3196+
schema: [
3197+
/* GraphQL */ `
3198+
type Query {
3199+
user(id: ID!): User
3200+
}
3201+
type User {
3202+
id: ID!
3203+
name: String!
3204+
friends: [User!]!
3205+
}
3206+
`,
3207+
],
3208+
documents: [
3209+
/* GraphQL */ `
3210+
query Test_User {
3211+
user(id: "user-001") {
3212+
id
3213+
name
3214+
}
3215+
}
3216+
`,
3217+
],
3218+
generates: {
3219+
'out1/': {
3220+
preset,
3221+
config: {
3222+
immutableTypes: true,
3223+
},
3224+
},
3225+
},
3226+
});
3227+
3228+
const graphqlFile = result.find(file => file.filename === 'out1/graphql.ts');
3229+
expect(graphqlFile.content).toMatchInlineSnapshot(`
3230+
"/* eslint-disable */
3231+
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
3232+
export type Maybe<T> = T | null;
3233+
export type InputMaybe<T> = T | null | undefined;
3234+
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
3235+
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
3236+
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
3237+
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
3238+
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
3239+
/** All built-in and custom scalars, mapped to their actual values */
3240+
export type Scalars = {
3241+
ID: { input: string; output: string; }
3242+
String: { input: string; output: string; }
3243+
Boolean: { input: boolean; output: boolean; }
3244+
Int: { input: number; output: number; }
3245+
Float: { input: number; output: number; }
3246+
};
3247+
3248+
export type Query = {
3249+
readonly __typename?: 'Query';
3250+
readonly user?: Maybe<User>;
3251+
};
3252+
3253+
3254+
export type QueryUserArgs = {
3255+
id: Scalars['ID']['input'];
3256+
};
3257+
3258+
export type User = {
3259+
readonly __typename?: 'User';
3260+
readonly friends: ReadonlyArray<User>;
3261+
readonly id: Scalars['ID']['output'];
3262+
readonly name: Scalars['String']['output'];
3263+
};
3264+
3265+
export type Test_UserQueryVariables = Exact<{ [key: string]: never; }>;
3266+
3267+
3268+
export type Test_UserQuery = { readonly __typename?: 'Query', readonly user?: { readonly __typename?: 'User', readonly id: string, readonly name: string } | null };
3269+
3270+
3271+
export const Test_UserDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Test_User"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"user"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"id"},"value":{"kind":"StringValue","value":"user-001","block":false}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}}]}}]}}]} as unknown as DocumentNode<Test_UserQuery, Test_UserQueryVariables>;"
3272+
`);
3273+
});
31933274
});

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The `client` preset allows the following `config` options:
4949
- [`strictScalars`](/plugins/typescript/typescript#strictscalars): If `scalars` are found in the schema that are not defined in scalars an error will be thrown during codegen.
5050
- [`namingConvention`](/plugins/typescript/typescript#namingconvention): Available case functions in `change-case-all` are `camelCase`, `capitalCase`, `constantCase`, `dotCase`, `headerCase`, `noCase`, `paramCase`, `pascalCase`, `pathCase`, `sentenceCase`, `snakeCase`, `lowerCase`, `localeLowerCase`, `lowerCaseFirst`, `spongeCase`, `titleCase`, `upperCase`, `localeUpperCase` and `upperCaseFirst`.
5151
- [`useTypeImports`](/plugins/typescript/typescript#usetypeimports): Will use `import type {}` rather than `import {}` when importing only types. This gives compatibility with TypeScript's [`"importsNotUsedAsValues": "error"`](https://www.typescriptlang.org/tsconfig#importsNotUsedAsValues) option.
52+
- [`immutableTypes`](/plugins/typescript/typescript#immutabletypes): Generates immutable types by adding `readonly` to properties and `ReadonlyArray` for lists.
5253
- [`skipTypename`](/plugins/typescript/typescript#skiptypename): Does not add `__typename` to the generated types, unless it was specified in the selection set.
5354
- [`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.
5455
- [`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.

0 commit comments

Comments
 (0)