MathiasM
Convex Community2y ago
3 replies
Mathias

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")


I then tried something like this:

 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


How can I make sure I conditionally query when args are present?
Was this page helpful?