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
9 changes: 9 additions & 0 deletions docs/content/docs/reference/configuration/output.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,15 @@ Include abort signal in queries.

Custom query/mutation key or options functions.

When a `queryOptions` or `mutationOptions` mutator declares a third
parameter, orval passes operation identity so the mutator can branch on it
(for example, to attach per-operation metadata or invalidate by
`operationId`). The exact shape differs between the two:

- `queryOptions` mutator — `{ url, operationId, operationName }`
- `mutationOptions` mutator — `{ operationId, operationName }` (the `url`
is supplied in the second parameter)

### shouldExportMutatorHooks

**Type:** `Boolean`
Expand Down
14 changes: 12 additions & 2 deletions packages/query/src/query-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ const generatePrefetch = ({

const generateQueryImplementation = ({
queryOption: { name, queryParam, options, type, queryKeyFnName },
operationId,
operationName,
queryProperties,
queryKeyProperties,
Expand Down Expand Up @@ -382,6 +383,7 @@ const generateQueryImplementation = ({
queryKeyFnName: string;
};
isRequestOptions: boolean;
operationId: string;
operationName: string;
queryProperties: string;
queryKeyProperties: string;
Expand Down Expand Up @@ -652,12 +654,17 @@ ${hookOptions}
? // Pass the same options object the non-mutator branch returns so
// generated guards (e.g. the `enabled` clause for nullish path
// params) reach the mutator instead of being dropped. See #1522.
// The third arg additionally carries operation identity (matching
// mutationOptions per #1974) so mutators can branch on the source
// operation. See #3153.
`const customOptions = ${
queryOptionsMutator.name
}({ queryKey, queryFn, ${queryOptionsImp}}${
queryOptionsMutator.hasSecondArg ? `, { ${queryProperties} }` : ''
}${
queryOptionsMutator.hasThirdArg ? `, { url: \`${route}\` }` : ''
queryOptionsMutator.hasThirdArg
? `, { url: \`${route}\`, operationId: '${operationId}', operationName: '${operationName}' }`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Out of scope for this PR. The same single-quote interpolation pattern is already in use for mutationOptions (see packages/query/src/mutation-generator.ts:471, introduced in #1974):

mutationOptionsMutator.hasThirdArg
  ? `, { operationId: '${operationId}', operationName: '${operationName}' }`
  : ''

If operationId / operationName need safe escaping, both call sites should switch together (and probably url too — that one also predates this PR). Doing it on the query side only here would diverge the two mutator types' output formats. Happy to file a follow-up issue for repo-wide operation-id encoding once this lands.

: ''
});`
: ''
}
Expand Down Expand Up @@ -733,7 +740,9 @@ export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${q
? `${queryOptionsMutator.name}({ queryKey: ${baseExpr} }${
queryOptionsMutator.hasSecondArg ? `, { ${queryProperties} }` : ''
}${
queryOptionsMutator.hasThirdArg ? `, { url: \`${route}\` }` : ''
queryOptionsMutator.hasThirdArg
? `, { url: \`${route}\`, operationId: '${operationId}', operationName: '${operationName}' }`
: ''
}).queryKey`
: baseExpr;

Expand Down Expand Up @@ -1145,6 +1154,7 @@ ${queryKeyFns}`;
for (const queryOption of queries) {
queryImplementations += generateQueryImplementation({
queryOption,
operationId,
operationName,
queryProperties,
queryKeyProperties,
Expand Down
Loading
Loading