Skip to content

[near-operation-file-preset] Add presetConfig.filePerOperation to generate one file per operation#1399

Open
eddeee888 wants to merge 10 commits intomainfrom
add-operation-file-name-config
Open

[near-operation-file-preset] Add presetConfig.filePerOperation to generate one file per operation#1399
eddeee888 wants to merge 10 commits intomainfrom
add-operation-file-name-config

Conversation

@eddeee888
Copy link
Collaborator

@eddeee888 eddeee888 commented Mar 11, 2026

Description

This option adds filePerOperation option to change the generated filename.
By default, the filename is based on source file
When filePerOperation:true, the file name is based on the named operation found.

This helps with Apollo Tooling -> Codegen migration

Related #203
Migration guide: https://the-guild.dev/graphql/codegen/docs/migration/apollo-tooling

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Unit test

@changeset-bot
Copy link

changeset-bot bot commented Mar 11, 2026

🦋 Changeset detected

Latest commit: bf7c2b1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@graphql-codegen/near-operation-file-preset Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@eddeee888 eddeee888 force-pushed the add-operation-file-name-config branch from a282b75 to 65757c7 Compare March 13, 2026 12:41
@eddeee888 eddeee888 marked this pull request as ready for review March 13, 2026 12:45
@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2026

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@graphql-codegen/near-operation-file-preset 5.1.0-alpha-20260316114250-bf7c2b14a5f801aa185814b9066fa16a1f269c83 npm ↗︎ unpkg ↗︎

const generatedFilePath = generateFilePath(documentFile.location);
const operationOrFragmentName = documentFile.document.definitions.find(
d => d.kind === Kind.OPERATION_DEFINITION || d.kind === Kind.FRAGMENT_DEFINITION,
)?.name?.value;
Copy link

@ikusakov2 ikusakov2 Mar 13, 2026

Choose a reason for hiding this comment

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

I'm not sure the above will work. A fragment could come earlier than operation in the array, but operation should take precedence.

Maybe the following would be better?

const operationOrFragmentName = documentFile.document.definitions.find(
        d => d.kind === Kind.OPERATION_DEFINITION)?.name?.value ?? documentFile.document.definitions.find(d.kind === Kind.FRAGMENT_DEFINITION,
      )?.name?.value;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops I wrote a comment for this change but didn't click submit. 😬

From my testing, if a fragment and an operation are in a file, then one file is generated for the fragment, and one for the operation. I've added a test here, unless I misunderstood how the generated files should work?


const fragmentName = fragment.name.value;
const filePath = generateFilePath(documentRecord.location);
const filePath = generateFilePath(documentRecord.location, fragmentName);

Choose a reason for hiding this comment

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

Good catch!

return appendFileNameToFilePath(
newFilePath,
filePerOperation
? customFilename // Note: Unnamed operations will cause `operationName` to be undefined. In such case, the generated filename will be based on the source document file.
Copy link

@ikusakov2 ikusakov2 Mar 13, 2026

Choose a reason for hiding this comment

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

To match what you describe in the comment probably better:

filePerOpration && customFile ? customFile : fileName

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True! This is fixed here: bf7c2b1

export type User4Query = { __typename?: 'Query', user4?: { __typename?: 'User', name: string } | null };
"
`);
});

Choose a reason for hiding this comment

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

Yes, with .ts the tests look better!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants