Skip to content

Conversation

mortik
Copy link

@mortik mortik commented Aug 18, 2025

Summary

Fix #2096

This PR adds a new configuration option useOperationIdAsQueryKey that allows users to generate query keys using the OpenAPI operation ID instead of the route string for TanStack Query implementations.

Changes

  • Added useOperationIdAsQueryKey?: boolean to QueryOptions and NormalizedQueryOptions types
  • Modified query key generation logic in packages/query/src/index.ts to use operation ID when enabled
  • Updated normalizeQueryOptions function to handle the new option
  • Added comprehensive tests in packages/query/src/utils.test.ts
  • Added documentation in /docs/src/pages/reference/configuration/output.md

Configuration Example

module.exports = {
  petstore: {
    output: {
      client: 'react-query',
      override: {
        query: {
          useOperationIdAsQueryKey: true,
        },
      },
    },
    input: './petstore.yaml',
  },
};

@mortik
Copy link
Author

mortik commented Aug 18, 2025

This should resolve #2096

AllieJonsson
AllieJonsson previously approved these changes Aug 19, 2025
Copy link
Contributor

@AllieJonsson AllieJonsson left a comment

Choose a reason for hiding this comment

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

LGTM!

@mortik
Copy link
Author

mortik commented Aug 19, 2025

I realized that this approach still needs an update to include the path params like id etc. i will update the pr to include that in the querykey function

@soartec-lab
Copy link
Member

Please give me a little time to salvage past discussions about cases where you might want to use arbitrary strings for query keys. And once a policy has been decided, it will need to be documented in the documentation to avoid any confusion.

@soartec-lab soartec-lab self-assigned this Aug 20, 2025
@soartec-lab
Copy link
Member

Hi, @mortik Sorry for the long wait, I've retraced the steps so far.

First, if you want to customize the query key, you can currently do so by using override.query.querykey.

https://orval.dev/reference/configuration/output#querykey

For mutator, starting with #1730, the operationId defined in the OpenAPI is now used as the query key by default.

Other discussions:


With this change, when you add an option, the OpenAPI operationId will be used as the query key by default. This makes sense to me, as it's the same behavior as the default for mutator 👍

Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

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

@mortik I added a question.

Comment on lines 1337 to 1338
? [`'${operationName}'`, ...queryKeyPathParams]
: routeString;
Copy link
Member

Choose a reason for hiding this comment

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

Should this be common and no separation is necessary?

Suggested change
? [`'${operationName}'`, ...queryKeyPathParams]
: routeString;
? operationName : routeString;

Copy link
Member

@soartec-lab soartec-lab Sep 20, 2025

Choose a reason for hiding this comment

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

@mortik
I still don't understand why this change is necessary so could you explain for me?
Or, incorporate this change.

#2314 (comment)

@soartec-lab soartec-lab added the enhancement New feature or request label Sep 13, 2025
@soartec-lab soartec-lab added this to the 7.12.0 milestone Sep 13, 2025
@melloware
Copy link
Collaborator

@mortik looks like conflicts too

@mortik
Copy link
Author

mortik commented Sep 14, 2025

Hi, @mortik Sorry for the long wait, I've retraced the steps so far.

First, if you want to customize the query key, you can currently do so by using override.query.querykey.

https://orval.dev/reference/configuration/output#querykey

For mutator, starting with #1730, the operationId defined in the OpenAPI is now used as the query key by default.

Other discussions:

With this change, when you add an option, the OpenAPI operationId will be used as the query key by default. This makes sense to me, as it's the same behavior as the default for mutator 👍

i tried to use the override.query.querykey option but sadly the function does not get the nescessary information from the schema to use, for example, the operationId as a querykey. Would also be an interessting change if the custom query key function would get more context information to change the default querykey behavior.

mortik and others added 6 commits September 14, 2025 18:45
This commit adds a new configuration option useOperationIdAsQueryKey to the query
options that allows users to generate query keys using the OpenAPI operation ID
instead of the route string.

Changes Made:
- Added useOperationIdAsQueryKey option to QueryOptions and NormalizedQueryOptions interfaces
- Modified query key generation logic to use operation ID when enabled
- Added support for the new option in normalizeQueryOptions function
- Added comprehensive tests for the new functionality

Benefits:
- Stable keys that dont change when route paths are modified
- Cleaner, more semantic query keys
- Consistent naming across applications
- Backward compatible (defaults to false)

Fixes orval-labs#2096
- Added comprehensive documentation for the useOperationIdAsQueryKey option
  in the existing output configuration docs
- Removed accidental .tool-versions file that's not part of the project structure
- Added code examples showing the difference between default and new behavior
- Integrated documentation into the existing project structure
@mortik mortik force-pushed the feat/use-operation-id-as-query-key branch from 544d036 to 4251d09 Compare September 14, 2025 16:53
@mortik
Copy link
Author

mortik commented Sep 14, 2025

@mortik looks like conflicts too

i resolved the conflict :)

Co-authored-by: Shodai Suzuki <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request tanstack-query TanStack Query related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Add operationId to queryKey for better React Query hook identification
4 participants