Typing of Query<>
should include query.options.enabled
#4762
-
Describe the bugContextI'm trying to do query invalidation based using predicates. For the particular use case, I would like to know if the query is enabled, since the typing of the queryKey may vary with it. Issueconst queryClient = useQueryClient()
queryClient.invalidateQueries({
predicate: (query) => {
// query.options.enabled is not included in typing, but it is part of the object if we console log.
return false
},
}) Your minimal, reproducible examplehttps://codesandbox.io/s/vibrant-poincare-mojft4?file=/src/index.jsx Steps to reproduce
{
...
options: {
...
enabled: false
}
}
Expected behaviorI expected to have query.options: typeof query.options & {enabled? : boolean} as the type of the query options How often does this bug happen?Every time Screenshots or VideosNo response Platform
TanStack Query version4.20.4 TypeScript version4.9.3 Additional contextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 5 replies
-
Still, the typings are correct and you shouldn't rely on that value being present.
Strictly speaking, a query itself cannot be enabled or disabled - it can just "be". Observers can be disabled if they shouldn't trigger requests by themselves, that's all. Can you elaborate on your use-case? Why would tpyings be different for enabled or disabled observers? |
Beta Was this translation helpful? Give feedback.
-
My use case is the following:
The only issue I had when doing things in this 'wrong way' was this. Trying to invalidate queries, I relied in the typing that was not always true, as it could be undefined when the query is not enabled. |
Beta Was this translation helpful? Give feedback.
-
query invalidation never targets disabled queries - there is an explicit check that filters out queries where all observers are disabled from the refetching logic: query/packages/query-core/src/queryClient.ts Lines 266 to 269 in 1c43f58 |
Beta Was this translation helpful? Give feedback.
-
The issue with this is that I'm using trpc as well as react-query, if that changes something, but in this particular case i don't think so |
Beta Was this translation helpful? Give feedback.
-
If you query is not disabled, it means it has at least one enabled observer :) query/packages/query-core/src/query.ts Lines 241 to 247 in abd1703 The devtools would show you how many observers there are, and if the query is fully disabled or not. I can't help much more without seeing the code in question. This is turning into a discussion, so I'll move it there. |
Beta Was this translation helpful? Give feedback.
-
@TkDodo Here is a better example I made, where we can see that query.options.enabled // false - State I would like to get
query.isDisabled() // false - Is not what I would expect to see, since I was expecting the opposite Does the example I provide here count as having an active observer? |
Beta Was this translation helpful? Give feedback.
-
in your example, you are executing I've moved the https://codesandbox.io/s/usequery-forked-cln6vx?file=/src/index.js |
Beta Was this translation helpful? Give feedback.
-
@TkDodo It seems that the problem is the following:
invalidate({ predicate: ({ queryKey: [path, input] }) => {
input.data.a.b
})
I believe this is unexpected as the enabled was false but it was added to the cache anyway and run by the Is this expected and if so, what would be the right general workaround, without checking for undefined inputs? Here we can see the error: |
Beta Was this translation helpful? Give feedback.
If you query is not disabled, it means it has at least one enabled observer :)
query/packages/query-core/src/query.ts
Lines 241 to 247 in abd1703
The devtools would show you how many observers there are, and if the query is fully disabled or not. I can't help much more without seeing the code in question.
This is turning into a discussion, so I'll move it there.