getQueryArgs does not correctly handle all prefetchQuery signatures #1207
Send to my agentprefetchQuery invokes getQueryArgs to get queryKey, config, and options.
prefetchQuery itself supports several different signatures.
We're using the signature where we pass queryKey, queryFn, and options.
prefetchQuery<TResult = unknown, TError = unknown>(
queryKey: QueryKey,
queryFn: QueryFunction<TResult>,
options?: PrefetchQueryOptions
): Promise<TResult | undefined>
In this case, the args as passed to prefetchQuery are forwarded on to getQueryArgs, which inspects some arguments to map them to queryKey, queryFn, config, and options, respectively.
With the prefetchQuery signature we're using, we fall into the else clause, which correctly assigns the queryKey and queryFn, but it then assigns the options to config and undefined to options, which seems incorrect. Ultimately, the options we get back are undefined.
If we instead use the prefetchQuery signature that includes queryConfig, all 4 values are mapped as expected.
There is also a prefetchQuery signature that takes PrefetchQueryObjectConfig.
It appears this would fall into the first case of getQueryArgs, but getQueryArgs expects the options to come fromarg2 in this case.
The prefetchQuery signature for passing a PrefetchQueryObjectConfig does not specify having to pass the options separately as a 2nd argument, so it's not what I was expecting for that signature.
prefetchQuery<TResult = unknown, TError = unknown>(
config: PrefetchQueryObjectConfig<TResult, TError>
): Promise<TResult | undefined>tannerlinsley added labelsenhancement
Is this still the case in V3? And can you provide a wrong case scenario?
Here is an example how the
onSuccessoption is ignored inprefetchQuery: https://codesandbox.io/s/romantic-edison-vr8hw?file=/src/App.jsHi @petrkrejcik!
prefetchQuerydoes not have the same options asuseQuery: https://github.com/tannerlinsley/react-query/blob/master/src/core/types.ts#L37 . The returned promise can be used to determine if a query is successful.@boschni Ah ok, now it makes sense. I was following the docs where I've read:
The options for prefetchQuery are exactly the same as those of useQuery with the exception of the last options object
I will use your advice and change the
onSuccesscallback to the result of the promise.Yeah we should definitely update the docs on this
A PR to clarify this is welcome.
tannerlinsley closed this
Sign in to join this conversation.
@boschni thoughts on this?