Skip to content
Source
Sign inSign up
TanStack/query

getQueryArgs does not correctly handle all prefetchQuery signatures #1207

Send to my agent
Closeddrbalm(GitHub profile, opens in a new tab) opened this issue 7 comments

prefetchQuery 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>
  1. @boschni thoughts on this?

  2. tannerlinsley added labelsenhancement

  3. Is this still the case in V3? And can you provide a wrong case scenario?

  4. Here is an example how the onSuccess option is ignored in prefetchQuery: https://codesandbox.io/s/romantic-edison-vr8hw?file=/src/App.js

  5. Hi @petrkrejcik! prefetchQuery does not have the same options as useQuery: 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.

  6. @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 onSuccess callback to the result of the promise.

  7. Yeah we should definitely update the docs on this

  8. A PR to clarify this is welcome.

  9. tannerlinsley closed this

Sign in to join this conversation.