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
6 changes: 6 additions & 0 deletions .changeset/every-queens-sin.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
---

Conditionally generate input types and output enums into target file
9 changes: 9 additions & 0 deletions dev-test/star-wars/types.avoidOptionals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** The episodes in the Star Wars trilogy */
export type Episode =
/** Star Wars Episode V: The Empire Strikes Back, released in 1980. */
| 'EMPIRE'
/** Star Wars Episode VI: Return of the Jedi, released in 1983. */
| 'JEDI'
/** Star Wars Episode IV: A New Hope, released in 1977. */
| 'NEWHOPE';

export type CreateReviewForEpisodeMutationVariables = Exact<{
episode: Episode;
review: ReviewInput;
Expand Down
9 changes: 9 additions & 0 deletions dev-test/star-wars/types.excludeQueryAlpha.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** The episodes in the Star Wars trilogy */
export type Episode =
/** Star Wars Episode V: The Empire Strikes Back, released in 1980. */
| 'EMPIRE'
/** Star Wars Episode VI: Return of the Jedi, released in 1983. */
| 'JEDI'
/** Star Wars Episode IV: A New Hope, released in 1977. */
| 'NEWHOPE';

export type CreateReviewForEpisodeMutationVariables = Exact<{
episode: Episode;
review: ReviewInput;
Expand Down
9 changes: 9 additions & 0 deletions dev-test/star-wars/types.excludeQueryBeta.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** The episodes in the Star Wars trilogy */
export type Episode =
/** Star Wars Episode V: The Empire Strikes Back, released in 1980. */
| 'EMPIRE'
/** Star Wars Episode VI: Return of the Jedi, released in 1983. */
| 'JEDI'
/** Star Wars Episode IV: A New Hope, released in 1977. */
| 'NEWHOPE';

export type CreateReviewForEpisodeMutationVariables = Exact<{
episode: Episode;
review: ReviewInput;
Expand Down
9 changes: 9 additions & 0 deletions dev-test/star-wars/types.globallyAvailable.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** The episodes in the Star Wars trilogy */
type Episode =
/** Star Wars Episode V: The Empire Strikes Back, released in 1980. */
| 'EMPIRE'
/** Star Wars Episode VI: Return of the Jedi, released in 1983. */
| 'JEDI'
/** Star Wars Episode IV: A New Hope, released in 1977. */
| 'NEWHOPE';

type CreateReviewForEpisodeMutationVariables = Exact<{
episode: Episode;
review: ReviewInput;
Expand Down
9 changes: 9 additions & 0 deletions dev-test/star-wars/types.immutableTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** The episodes in the Star Wars trilogy */
export type Episode =
/** Star Wars Episode V: The Empire Strikes Back, released in 1980. */
| 'EMPIRE'
/** Star Wars Episode VI: Return of the Jedi, released in 1983. */
| 'JEDI'
/** Star Wars Episode IV: A New Hope, released in 1977. */
| 'NEWHOPE';

export type CreateReviewForEpisodeMutationVariables = Exact<{
episode: Episode;
review: ReviewInput;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** The episodes in the Star Wars trilogy */
export type Episode =
/** Star Wars Episode V: The Empire Strikes Back, released in 1980. */
| 'EMPIRE'
/** Star Wars Episode VI: Return of the Jedi, released in 1983. */
| 'JEDI'
/** Star Wars Episode IV: A New Hope, released in 1977. */
| 'NEWHOPE';

export type CreateReviewForEpisodeMutationVariables = Exact<{
episode: Episode;
review: ReviewInput;
Expand Down
9 changes: 9 additions & 0 deletions dev-test/star-wars/types.preResolveTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** The episodes in the Star Wars trilogy */
export type Episode =
/** Star Wars Episode V: The Empire Strikes Back, released in 1980. */
| 'EMPIRE'
/** Star Wars Episode VI: Return of the Jedi, released in 1983. */
| 'JEDI'
/** Star Wars Episode IV: A New Hope, released in 1977. */
| 'NEWHOPE';

export type CreateReviewForEpisodeMutationVariables = Exact<{
episode: Episode;
review: ReviewInput;
Expand Down
9 changes: 9 additions & 0 deletions dev-test/star-wars/types.skipSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** The episodes in the Star Wars trilogy */
export type Episode =
/** Star Wars Episode V: The Empire Strikes Back, released in 1980. */
| 'EMPIRE'
/** Star Wars Episode VI: Return of the Jedi, released in 1983. */
| 'JEDI'
/** Star Wars Episode IV: A New Hope, released in 1977. */
| 'NEWHOPE';

export type CreateReviewForEpisodeMutationVariables = Exact<{
episode: Episode;
review: ReviewInput;
Expand Down
9 changes: 9 additions & 0 deletions dev-test/star-wars/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** The episodes in the Star Wars trilogy */
export type Episode =
/** Star Wars Episode V: The Empire Strikes Back, released in 1980. */
| 'EMPIRE'
/** Star Wars Episode VI: Return of the Jedi, released in 1983. */
| 'JEDI'
/** Star Wars Episode IV: A New Hope, released in 1977. */
| 'NEWHOPE';

export type CreateReviewForEpisodeMutationVariables = Exact<{
episode: Episode;
review: ReviewInput;
Expand Down
30 changes: 11 additions & 19 deletions packages/plugins/typescript/operations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,14 @@ export const plugin: PluginFunction<TypeScriptDocumentsPluginConfig, Types.Compl

const operationsResult = oldVisit(allAst, { leave: visitor });

let operationsContent = operationsResult.definitions.join('\n');
const operationsDefinitions = operationsResult.definitions;
Copy link
Author

Choose a reason for hiding this comment

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

I rearranged/simplified the code here - please check.


if (config.addOperationExport) {
const exportConsts = [];

for (const d of allAst.definitions) {
if ('name' in d) {
exportConsts.push(`export declare const ${d.name.value}: import("graphql").DocumentNode;`);
operationsDefinitions.push(`export declare const ${d.name.value}: import("graphql").DocumentNode;`);
}
}

operationsContent = operationsResult.definitions.concat(exportConsts).join('\n');
}

if (config.globalNamespace) {
Copy link
Author

@ikusakov2 ikusakov2 Dec 2, 2025

Choose a reason for hiding this comment

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

I moved this block to the end - to include all the generated code.

operationsContent = `
declare global {
${operationsContent}
}`;
}

const schemaTypes = oldVisit(transformSchemaAST(schema, config).ast, { leave: visitor });
Expand All @@ -53,13 +42,16 @@ export const plugin: PluginFunction<TypeScriptDocumentsPluginConfig, Types.Compl
// It will leave the node as an object.
// Here, we filter in nodes that have been turned into strings, i.e. they have been transformed
// This way, we do not have to explicitly declare a method for every node type to convert them to null
const schemaTypesContent = schemaTypes.definitions.filter(def => typeof def === 'string').join('\n');
const schemaTypesDefinitions = schemaTypes.definitions.filter(def => typeof def === 'string');

let content = [...schemaTypesDefinitions, ...operationsDefinitions].join('\n');

const content: string[] = [];
if (schemaTypesContent) {
content.push(schemaTypesContent);
if (config.globalNamespace) {
content = `
declare global {
${content}
}`;
}
content.push(operationsContent);

return {
prepend: [
Expand All @@ -70,7 +62,7 @@ export const plugin: PluginFunction<TypeScriptDocumentsPluginConfig, Types.Compl
visitor.getExactUtilityType(),
visitor.getIncrementalUtilityType(),
],
content: content.join('\n'),
content,
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TypeScriptOperationVariablesToObject as TSOperationVariablesToObject } from '@graphql-codegen/typescript';

const SCALARS = {
export const SCALARS = {
ID: 'string | number',
String: 'string',
Int: 'number',
Expand Down
Loading