Mathias
Mathias5mo ago

How to use skip in my useStableQuery?

I wonder if I can use skip using my useStableQuery, when args are not present. The reason I got to think about it was when I realised that I got a lot of similar logs like this one within Convex dashboard:
flows:getById
failure
1ms
ArgumentValidationError: Value does not match validator.
Path: .id
Value: null
Validator: v.id("flows")
flows:getById
failure
1ms
ArgumentValidationError: Value does not match validator.
Path: .id
Value: null
Validator: v.id("flows")
I then tried something like this:
const { data: flow, isPending } = useStableQuery(
`flows + ${flowId}`,
api.flows.getById,
flowId !== undefined ? { id: flowId } : "skip"
)
const { data: flow, isPending } = useStableQuery(
`flows + ${flowId}`,
api.flows.getById,
flowId !== undefined ? { id: flowId } : "skip"
)
This is my useStableQuery:
export const useStableQuery = ((globalKey, query, ...args) => {
const { status, data, error } = useQuery(query, ...args)
const convex = useConvex()
const cache = ((convex as any).__cache__ ??= {})

if (status === "success" && data !== undefined) {
cache[globalKey] = data
}

return {
status,
data: cache[globalKey],
error,
isSuccess: status === "success",
isPending: status === "pending",
isError: status === "error",
}
}) as QueryWithGlobalCacheKey
export const useStableQuery = ((globalKey, query, ...args) => {
const { status, data, error } = useQuery(query, ...args)
const convex = useConvex()
const cache = ((convex as any).__cache__ ??= {})

if (status === "success" && data !== undefined) {
cache[globalKey] = data
}

return {
status,
data: cache[globalKey],
error,
isSuccess: status === "success",
isPending: status === "pending",
isError: status === "error",
}
}) as QueryWithGlobalCacheKey
How can I make sure I conditionally query when args are present?
3 Replies
lee
lee5mo ago
this is going to sound silly. The error message says you're passing in {id: null}. And null !== undefined so you're not hitting the condition to skip
Mathias
MathiasOP5mo ago
Haha, thanks @lee ! Can you tell me if the approach is the correct one, except for the null vs undefined part?
lee
lee5mo ago
totally, looks good to me

Did you find this page helpful?