TanStackT
TanStack4mo ago
4 replies
ordinary-sapphire

Query without a queryFn but just manually using setQueryData?

I have a weird use case which probably means I'm doing something wrong, haha. I have a case where I'm streaming some events back from a server using Server Sent Events. There's some events I'd like to store in memory and make use of their results later. Since I already use TanStack Query, I thought I'd just leverage that. But basically I just want to manually manage the query data myself.

I have this:
queryClient.setQueryDefaults(
        [CHAT_SESSION_QUERY_KEY, chatSessionExternalKey, "metadata"],
        {
          gcTime: Infinity,
        },
      );
      queryClient.setQueryData<ChatMetadata>(
        [CHAT_SESSION_QUERY_KEY, chatSessionExternalKey, "metadata"],
        (old) => (old ? { ...old, ...metadataUpdateRequest } : eventData),
      );


That works, but I run into trouble later when trying to use that query. Since useQuery needs a queryFn, I don't really know what to supply for that? I don't actually want to use a queryFn, I just want the useQuery to look up whatever data I've already manually stored (and keep it synced if I ever change it).

Any ideas?
Was this page helpful?