[near-operation-file-preset] Add presetConfig.filePerOperation to generate one file per operation#1399
[near-operation-file-preset] Add presetConfig.filePerOperation to generate one file per operation#1399
presetConfig.filePerOperation to generate one file per operation#1399Conversation
🦋 Changeset detectedLatest commit: bf7c2b1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
a282b75 to
65757c7
Compare
| const generatedFilePath = generateFilePath(documentFile.location); | ||
| const operationOrFragmentName = documentFile.document.definitions.find( | ||
| d => d.kind === Kind.OPERATION_DEFINITION || d.kind === Kind.FRAGMENT_DEFINITION, | ||
| )?.name?.value; |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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); |
| 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. |
There was a problem hiding this comment.
To match what you describe in the comment probably better:
filePerOpration && customFile ? customFile : fileName
| export type User4Query = { __typename?: 'Query', user4?: { __typename?: 'User', name: string } | null }; | ||
| " | ||
| `); | ||
| }); |
There was a problem hiding this comment.
Yes, with .ts the tests look better!
Description
This option adds
filePerOperationoption 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
How Has This Been Tested?