query icon indicating copy to clipboard operation
query copied to clipboard

Svelte query - query does not react to changing default options by queryClient.setDefaultOptions in runtime

Open Pawel-Zygmunt opened this issue 2 years ago • 1 comments

Describe the bug

I have initialized QueryClient in main +layout.svelte:

const queryClient = new QueryClient({
    defaultOptions: {
      queries: {
        enabled: browser,
        refetchInterval: $dataRefetchIntervalS * 1000
      }
    }
  });

  $: {
    queryClient.setDefaultOptions({
      queries: {
        ...queryClient.getDefaultOptions().queries,
        refetchInterval: $dataRefetchIntervalS * 1000
      }
    });
  }

then, somewhere in my app on route /example i have:

const streamsQuery = createQuery({
    queryKey: ['streams'],
    queryFn: async () => {
      const res = await fetch(`${PUBLIC_API_KEY}/streams`);
      const data = await res.json();
      return data.map(streamMapper);
    }
  });
  $: ({ data, status } = $streamsQuery);

$dataRefetchIntervalS as reactive store can be changed and should cause that streamsQuery would react to a new value, but its not. StreamsQuery updates it's options only when unmounted and mounted again - while for e.g changing routes withing app.

Your minimal, reproducible example

https://stackblitz.com/edit/stackblitz-starters-megwxq?description=React%20%20%20TypeScript%20starter%20project&file=src%2Findex.tsx,src%2FApp.tsx&title=React%20Starter

Steps to reproduce

Reproducted the same behaviour in react and works fine.

Expected behavior

Queries should react to defaultOptions changed by queryClient.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: any
  • Browser: any

Tanstack Query adapter

svelte-query

TanStack Query version

5.0.0-beta.15 but also in stable version

TypeScript version

5.0.0

Additional context

No response

Pawel-Zygmunt avatar Aug 08 '23 20:08 Pawel-Zygmunt

@lachlancollins any progress on this ?

Pawel-Zygmunt avatar Aug 18 '23 18:08 Pawel-Zygmunt

this is a general design decision in the query core. Calling queryClient.setQueryDefaults is not an operation that has any effect on already existing queries - the defaults are only taken into account when the query is created.

TkDodo avatar Feb 17 '24 18:02 TkDodo