Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getQueryKey should not enforce options #1682

Open
iffa opened this issue Feb 7, 2025 · 6 comments
Open

getQueryKey should not enforce options #1682

iffa opened this issue Feb 7, 2025 · 6 comments
Labels
feature 🚀 New feature or request prioritized 🚚 This issue has been prioritized and will be worked on soon

Comments

@iffa
Copy link

iffa commented Feb 7, 2025

Description

Continuing from what was mentioned in #980:

The query key function is strict for now, we could make it partial if needed

We would like to see support for non-strict version, as we may be invalidating a query in another part of the app that doesn't know of the callers options, but just wants the query to be invalidated

Obviously not a blocker as we can always just go to declaration and figure out how to do it manually but still

@iffa iffa added the feature 🚀 New feature or request label Feb 7, 2025
@mrlubos mrlubos added the prioritized 🚚 This issue has been prioritized and will be worked on soon label Feb 7, 2025
@mrlubos mrlubos added this to the TanStack Query milestone Feb 7, 2025
@iffa
Copy link
Author

iffa commented Feb 18, 2025

This seems to be rather more important to us now actually, as I just checked how the query keys are constructed, and the "operation id" is inside the options object and not a separate array element as a prefix, so we can only invalidate with exact options without hacks :/

@Redmega
Copy link

Redmega commented Feb 18, 2025

strictly speaking using predicate isn't a hack but I agree the api can be cleaner :)

@iffa
Copy link
Author

iffa commented Feb 18, 2025

strictly speaking using predicate isn't a hack but I agree the api can be cleaner :)

Yes, youre right! :)

@willtcarey
Copy link

Would anyone be able to clarify what the predicate hack/not hack is? I'm running into this same invalidating using partial options thing and if there's a workaround that would be nice to know.

@iffa
Copy link
Author

iffa commented Feb 27, 2025

Would anyone be able to clarify what the predicate hack/not hack is? I'm running into this same invalidating using partial options thing and if there's a workaround that would be nice to know.

Instead of passing queryKey to invalidateQueries you can pass predicate: (query) => void (or something like that, inside which you can do something like: return query.queryKey[0]._id === "getFooBar"

@Redmega
Copy link

Redmega commented Feb 27, 2025

Would anyone be able to clarify what the predicate hack/not hack is? I'm running into this same invalidating using partial options thing and if there's a workaround that would be nice to know.

https://github.com/orgs/hey-api/discussions/1672#discussioncomment-12180276

you could also use a predicate:

queryClient.invalidateQueries({ 
  predicate: query => query.queryKey[0]._id === getPetByIdQueryKey(options)[0]._id
})

if the options are required (e.x. path parameter or required body/query parameter) you can just use the operation id I think. This one I'm not sure, you'd have to test it out with your own openapi spec. For me, the generated keys are all related to the operationIds

queryClient.invalidateQueries({ 
  predicate: query => query.queryKey[0]._id === "getPetById" // assuming operation id is get-pet-by-id?
})

You can use the predicate similarly to target the path or whatever other part of the query key you need.

All this to say, It would be nice if heyapi support this natively, but there are workarounds available to you if you need it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🚀 New feature or request prioritized 🚚 This issue has been prioritized and will be worked on soon
Projects
None yet
Development

No branches or pull requests

4 participants