Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Pawel-Zygmunt opened this issue Aug 8, 2023 · 5 comments
Labels
wontfix This will not be worked on works as designed

Comments

@Pawel-Zygmunt
Copy link

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
Copy link
Author

@lachlancollins any progress on this ?

@TkDodo
Copy link
Collaborator

TkDodo commented Feb 17, 2024

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 TkDodo closed this as not planned Won't fix, can't repro, duplicate, stale Feb 17, 2024
@TkDodo TkDodo added wontfix This will not be worked on works as designed and removed package: svelte-query labels Feb 17, 2024
@mihnea-tirca
Copy link

Hi @TkDodo. I'm trying to do basically the same thing: dynamically modify staleTime (with a call to the backend that gets as response a fetching configuration that includes a staleTime value). How would I make this work? I have tried doing queryClient.removeQueries() after setting calling queryClient.setDefaultOptions() and it still doesn't work.

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 5, 2025

I have tried doing queryClient.removeQueries() after setting calling queryClient.setDefaultOptions()

should work I think, show a reproduction?

@mihnea-tirca
Copy link

I figured out the problem... :D I didn't take the documentation's advice to heart (regardingstable query client) and I was invalidating queries on the wrong QueryClient instance. I didn't know it was creating multiple instances, so I fixed it and made it a singleton and everything works perfectly fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on works as designed
Projects
None yet
Development

No branches or pull requests

4 participants