Skip to content

Conversation

Armanio
Copy link
Contributor

@Armanio Armanio commented Sep 10, 2025

Fix #2359

Status

READY

Description

  • Generate unique query keys for infinite vs regular queries

PS: I'm not sure if this edit requires a doc edit, if not, please correct me

${
!queryKeyMutator
? queries
.filter(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

reject all non unique queryKeyFnName for prevent generate duplication functions

Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that the issue was already there?
Or has this change meant that there is now a possibility of duplication?

Copy link
Member

Choose a reason for hiding this comment

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

After the discussion, if we define this processing, insert a variable definition such as uniqueQueryKeys to make the nesting shallowe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this mean that the issue was already there?

No, this is an introduced problem. This happens because each query type has its queryKeyFnName
For example QueryType.QUERY and QueryType.SUSPENSE_QUERY has same queryKeyFnName

Probably we need drop queryKeyFnName from queries array and use somewhat like

const queryKeyFnName = query.type === QueryType.INFINITE || query.type === QueryType.SUSPENSE_INFINITE ? camel(`get-${operationName}-infinite-query-key`)  : camel(`get-${operationName}-query-key`) ```

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, the simple solution didn't work.
We need an array of keys: one for regular queries, the other for infinite ones.
Therefore, we have to iterate through the array of queries to collect the required ones.

If we stick with the ternary solution I suggested above, we'll end up with duplicate keys.

Perhaps I'm approaching the problem incorrectly and there's a simpler way. If so, I'd appreciate any hints.

@Armanio Armanio force-pushed the fix_infinity_query_key branch from 77f8f56 to 051e3b2 Compare September 10, 2025 18:26
options: query?.options,
type: QueryType.SUSPENSE_INFINITE,
queryParam: query?.useInfiniteQueryParam,
queryKeyFnName: camel(`get-${operationName}-infinite-query-key`),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

now each query type has own queryKeyFnName

@melloware melloware added the tanstack-query TanStack Query related issue label Sep 10, 2025
@Armanio Armanio force-pushed the fix_infinity_query_key branch 2 times, most recently from 38cd852 to 24fc7d6 Compare September 10, 2025 21:08
melloware
melloware previously approved these changes Sep 11, 2025
@melloware
Copy link
Collaborator

this LGTM but i would like @soartec-lab to review also.

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.

Thanks for the changes. I added some comments.


// Convert "param: Type" to "param?: Type" for queryKey functions
// to enable cache invalidation without type assertion
export const makeParamsOptional = (params: string) => {
Copy link
Member

Choose a reason for hiding this comment

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

Currently, we are not strictly extracting functions into utils.
There are also benefits to having functions exposed in the files you use, closer to the processing they are used in.
What was the reason for moving them here now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I felt that this was a rather isolated and generic function and it didn't belong in the index file.
After all, the index file is too big and hard to understand.

Comment on lines 769 to 780
${[
routeString,
query.type === QueryType.INFINITE ||
query.type === QueryType.SUSPENSE_INFINITE
? `'infinite'`
: '',
queryParams ? '...(params ? [params]: [])' : '',
body.implementation,
]
.filter((x) => !!x)
.join(', ')}
] as const;
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be enough to just prepend the string to the array for infinate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wht not, i agree

Copy link
Member

Choose a reason for hiding this comment

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

Great, then let's just add a prefix for a more concise fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the prefix as you suggested

${
!queryKeyMutator
? queries
.filter(
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that the issue was already there?
Or has this change meant that there is now a possibility of duplication?

${
!queryKeyMutator
? queries
.filter(
Copy link
Member

Choose a reason for hiding this comment

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

After the discussion, if we define this processing, insert a variable definition such as uniqueQueryKeys to make the nesting shallowe.

return '{ signal }';
};

const generateQueryKeyImplementation = ({
Copy link
Member

Choose a reason for hiding this comment

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

By extracting the function, the distance between the declaration and the place where it is actually used has increased. This increases the burden of understanding the overall processing, so consider reverting it if there is no significant benefit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I understand what you're talking about. However, I'll put in my two cents:
I thought that in this way I was actually increasing the clarity of the code and its maintainability
generateQueryImplementation is already a very large function

Now the generator functions are divided by type of responsibility:
image

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

@Armanio
I agree that index.ts is verbose. It's also true that it makes it difficult for us to make changes.
Related to that, it's also true that the changes are difficult to understand.

So I'd like to separate feature additions and refactorings.
Could you just make the feature changes in this PR and do the refactoring in a follow-up PR? I'd be happy to do that.

@Armanio
Copy link
Contributor Author

Armanio commented Sep 20, 2025

Of course! Look a great plan. Give me little more time 🤝

…support

- Generate unique query keys for infinite vs regular queries
@Armanio Armanio force-pushed the fix_infinity_query_key branch from 027d6d4 to 2ffac79 Compare September 27, 2025 03:35
@Armanio
Copy link
Contributor Author

Armanio commented Sep 27, 2025

@soartec-lab
Updated the branch to include the latest changes.
Removed unnecessary refactoring and kept only the necessary changes.

@melloware melloware modified the milestones: 7.12.0, 7.13.0, 7.14.0 Sep 27, 2025
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.

[react-query] When generating both normal query and infinite query, use different query key for infinite query
3 participants