Skip to content

Commit 8f4ae4a

Browse files
authored
fix(query): pass operationId and operationName to queryOptions mutator (#3427)
* fix(query): pass operationId and operationName to queryOptions mutator The `mutationOptions` mutator has received `{ operationId, operationName }` as its third argument since #1974, but the symmetric `queryOptions` mutator was left with only `{ url }`. That asymmetry made it impossible to write `queryOptions` overrides that branch on operation identity (e.g. attach per-operation metadata or invalidate by `operationId`). Extend the third argument of `queryOptions` mutator calls from `{ url }` to `{ url, operationId, operationName }`. The change is additive — mutators that today read `arg3.url` keep working unchanged. Both call sites are updated: the main query options builder and the `applyQueryOptionsMutator` helper that backs `invalidate`/`set`/`get`. The 2nd-arg asymmetry between `queryOptions` (`{ queryProperties }`) and `mutationOptions` (`{ url }`) is shipped behaviour and is left alone. Closes #3153 * docs(query): correct queryOptions vs mutationOptions 3rd-arg shapes test(query): decouple operationName from operationId in #3153 regression
1 parent d5df441 commit 8f4ae4a

26 files changed

Lines changed: 1608 additions & 2 deletions

docs/content/docs/reference/configuration/output.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,15 @@ Include abort signal in queries.
858858

859859
Custom query/mutation key or options functions.
860860

861+
When a `queryOptions` or `mutationOptions` mutator declares a third
862+
parameter, orval passes operation identity so the mutator can branch on it
863+
(for example, to attach per-operation metadata or invalidate by
864+
`operationId`). The exact shape differs between the two:
865+
866+
- `queryOptions` mutator — `{ url, operationId, operationName }`
867+
- `mutationOptions` mutator — `{ operationId, operationName }` (the `url`
868+
is supplied in the second parameter)
869+
861870
### shouldExportMutatorHooks
862871

863872
**Type:** `Boolean`

packages/query/src/query-generator.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ const generatePrefetch = ({
348348

349349
const generateQueryImplementation = ({
350350
queryOption: { name, queryParam, options, type, queryKeyFnName },
351+
operationId,
351352
operationName,
352353
queryProperties,
353354
queryKeyProperties,
@@ -382,6 +383,7 @@ const generateQueryImplementation = ({
382383
queryKeyFnName: string;
383384
};
384385
isRequestOptions: boolean;
386+
operationId: string;
385387
operationName: string;
386388
queryProperties: string;
387389
queryKeyProperties: string;
@@ -652,12 +654,17 @@ ${hookOptions}
652654
? // Pass the same options object the non-mutator branch returns so
653655
// generated guards (e.g. the `enabled` clause for nullish path
654656
// params) reach the mutator instead of being dropped. See #1522.
657+
// The third arg additionally carries operation identity (matching
658+
// mutationOptions per #1974) so mutators can branch on the source
659+
// operation. See #3153.
655660
`const customOptions = ${
656661
queryOptionsMutator.name
657662
}({ queryKey, queryFn, ${queryOptionsImp}}${
658663
queryOptionsMutator.hasSecondArg ? `, { ${queryProperties} }` : ''
659664
}${
660-
queryOptionsMutator.hasThirdArg ? `, { url: \`${route}\` }` : ''
665+
queryOptionsMutator.hasThirdArg
666+
? `, { url: \`${route}\`, operationId: '${operationId}', operationName: '${operationName}' }`
667+
: ''
661668
});`
662669
: ''
663670
}
@@ -733,7 +740,9 @@ export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${q
733740
? `${queryOptionsMutator.name}({ queryKey: ${baseExpr} }${
734741
queryOptionsMutator.hasSecondArg ? `, { ${queryProperties} }` : ''
735742
}${
736-
queryOptionsMutator.hasThirdArg ? `, { url: \`${route}\` }` : ''
743+
queryOptionsMutator.hasThirdArg
744+
? `, { url: \`${route}\`, operationId: '${operationId}', operationName: '${operationName}' }`
745+
: ''
737746
}).queryKey`
738747
: baseExpr;
739748

@@ -1145,6 +1154,7 @@ ${queryKeyFns}`;
11451154
for (const queryOption of queries) {
11461155
queryImplementations += generateQueryImplementation({
11471156
queryOption,
1157+
operationId,
11481158
operationName,
11491159
queryProperties,
11501160
queryKeyProperties,

0 commit comments

Comments
 (0)