Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"ignore": [
"website",
"example-*",
"dev-test*",
Copy link
Collaborator Author

@eddeee888 eddeee888 Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image

Similar to example folders, the modules in dev-test (which includes the example for Apollo tooling migration), should be ignored, otherwise it'd show up in Changeset interface

"@graphql-codegen/client-preset-swc-plugin",
"example-apollo-client-swc-plugin",
"example-react-nextjs-swr"
Expand Down
6 changes: 6 additions & 0 deletions .changeset/wise-poets-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/typescript-operations': major
'@graphql-codegen/client-preset': major
---

BREAKING CHANGE: config.avoidOptionals now only supports object, inputValue, defaultValue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* This version of AvoidOptionalsConfig is a cut down version of the type of the same name in `@graphql-codegen/visitor-plugins-common`
* This version only deal with types available in client use cases.
*/
export interface AvoidOptionalsConfig {
object?: boolean;
inputValue?: boolean;
defaultValue?: boolean;
}
export type NormalizedAvoidOptionalsConfig = Required<AvoidOptionalsConfig>;

export const normalizeAvoidOptionals = (
avoidOptionals?: boolean | AvoidOptionalsConfig
): NormalizedAvoidOptionalsConfig => {
const defaultAvoidOptionals: NormalizedAvoidOptionalsConfig = {
object: false,
inputValue: false,
defaultValue: false,
};

if (typeof avoidOptionals === 'boolean') {
return {
object: avoidOptionals,
inputValue: avoidOptionals,
defaultValue: avoidOptionals,
};
}

return {
...defaultAvoidOptionals,
...avoidOptionals,
};
};
45 changes: 19 additions & 26 deletions packages/plugins/typescript/operations/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import {
AvoidOptionalsConfig,
type ConvertSchemaEnumToDeclarationBlockString,
type EnumValuesMap,
RawDocumentsConfig,
} from '@graphql-codegen/visitor-plugin-common';
import type { AvoidOptionalsConfig } from './config.avoidOptionals';

/**
* @description This plugin generates TypeScript types based on your GraphQLSchema _and_ your GraphQL operations and fragments.
* It generates types for your GraphQL documents: Query, Mutation, Subscription and Fragment.
*
* Note: In most configurations, this plugin requires you to use `typescript as well, because it depends on its base types.
Comment on lines -11 to -12
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this makes me feel we are so close now!

*/
export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
/**
Expand All @@ -28,7 +26,7 @@ export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* plugins: ['typescript-operations'],
* config: {
* arrayInputCoercion: false
* },
Expand All @@ -55,7 +53,7 @@ export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* plugins: ['typescript-operations'],
* config: {
* avoidOptionals: true
* },
Expand All @@ -74,13 +72,12 @@ export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* plugins: ['typescript-operations'],
* config: {
* avoidOptionals: {
* field: true
* inputValue: true
* object: true
* defaultValue: true
* inputValue: true,
* object: true,
* defaultValue: true,
* }
* },
* },
Expand All @@ -102,7 +99,7 @@ export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* plugins: ['typescript-operations'],
* config: {
* immutableTypes: true
* },
Expand All @@ -125,7 +122,7 @@ export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript', 'typescript-operations'],
* plugins: ['typescript-operations'],
* config: {
* flattenGeneratedTypes: true
* },
Expand All @@ -149,7 +146,7 @@ export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript', 'typescript-operations'],
* plugins: ['typescript-operations'],
* config: {
* flattenGeneratedTypes: true,
* flattenGeneratedTypesIncludeFragments: true
Expand All @@ -175,7 +172,7 @@ export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* plugins: ['typescript-operations'],
* config: {
* noExport: true
* },
Expand Down Expand Up @@ -206,23 +203,19 @@ export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
* const config: CodegenConfig = {
* // ...
* generates: {
* "./typings/api.ts": {
* "plugins": [
* "typescript"
* ]
* },
* "./": {
* "./": {
* "preset": "near-operation-file",
* "presetConfig": {
* "baseTypesPath": "./typings/api.ts",
* "extension": ".gql.d.ts"
* "baseTypesPath": "./typings/api.ts",
* "extension": ".gql.d.ts"
* },
* "plugins": [
* "@graphql-codegen/typescript-operations"
* "typescript-operations"
* ],
* "config": {
* "addOperationExport": true
* "addOperationExport": true
* }
* }
* }
* };
* export default config;
Expand Down Expand Up @@ -298,7 +291,7 @@ export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* plugins: ['typescript-operations'],
* config: {
* allowUndefinedQueryVariables: true
* },
Expand Down Expand Up @@ -339,7 +332,7 @@ export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript', 'typescript-operations'],
* plugins: ['typescript-operations'],
* config: {
* nullability: {
* errorHandlingClient: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
OperationVariablesToObject,
ConvertNameFn,
NormalizedAvoidOptionalsConfig,
NormalizedScalarsMap,
ParsedEnumValuesMap,
printTypeScriptMaybeType,
} from '@graphql-codegen/visitor-plugin-common';
import { Kind, TypeNode } from 'graphql';
import type { NormalizedAvoidOptionalsConfig } from './config.avoidOptionals';

export const SCALARS = {
ID: 'string | number',
Expand Down
8 changes: 2 additions & 6 deletions packages/plugins/typescript/operations/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
getEnumsImports,
isNativeNamedType,
LoadedFragment,
normalizeAvoidOptionals,
NormalizedAvoidOptionalsConfig,
ParsedDocumentsConfig,
type ParsedEnumValuesMap,
parseEnumValues,
Expand Down Expand Up @@ -46,8 +44,9 @@ import {
visit,
visitWithTypeInfo,
} from 'graphql';
import { TypeScriptDocumentsPluginConfig } from './config.js';
import type { TypeScriptDocumentsPluginConfig } from './config.js';
import { TypeScriptOperationVariablesToObject, SCALARS } from './ts-operation-variables-to-object.js';
import { normalizeAvoidOptionals, NormalizedAvoidOptionalsConfig } from './config.avoidOptionals.js';

export interface TypeScriptDocumentsParsedConfig extends ParsedDocumentsConfig {
arrayInputCoercion: boolean;
Expand Down Expand Up @@ -175,9 +174,6 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
this.setVariablesTransformer(
new TypeScriptOperationVariablesToObject(
{
// FIXME: this is the legacy avoidOptionals which was used to make Result fields non-optional. This use case is no longer valid.
// It's also being used for Variables so people could already be using it.
// Maybe it's better to deprecate and remove, to see what users think.
Comment on lines -178 to -180
Copy link
Collaborator Author

@eddeee888 eddeee888 Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it for now to keep the scope of this project manageable

avoidOptionals: this.config.avoidOptionals,
immutableTypes: this.config.immutableTypes,
inputMaybeValue: this.config.inputMaybeValue,
Expand Down
Loading