TanStack / query

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.
https://tanstack.com/query
MIT License
42.62k stars 2.92k forks source link

[Alpha] svelte-query v5 doesn't accept derived stores for options #5668

Closed skokenes closed 1 year ago

skokenes commented 1 year ago

Describe the bug

In the documentation for svelte-query v5, it is suggested that you can provide stores for queryOptions in order to make reactive queries: https://tanstack.com/query/v5/docs/svelte/reactivity

This works great with writable stores, as seen in this simple example: https://svelte.dev/repl/7d299dce4d9a4b6ba590d6707f383d62?version=4.0.2

However, it does not work with a derived store, as seen in this example: https://svelte.dev/repl/ee90c13684af4672a19661e9e9021da3?version=4.0.2

The derived store options appear to be completely ignored; no fetching happens.

Your minimal, reproducible example

https://svelte.dev/repl/ee90c13684af4672a19661e9e9021da3?version=4.0.2

Steps to reproduce

Try changing the input box to a different number. Observe that the query never fetches. Also observe in the console that the updated options store value is produced as expected.

Expected behavior

A user should be able to provide a derived store as query options and it should work the same as if a writable store is provided: the query should react to the latest value of the store for its options.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

OSX Chrome

Tanstack Query adapter

svelte-query

TanStack Query version

v5.0.0-alpha.7

TypeScript version

No response

Additional context

No response

lachlancollins commented 1 year ago

Hi @skokenes , I think I've figured out the problem. The current logic to determine whether a store has been passed in checks specifically for properties of writable stores (subscribe, set, update). These aren't all present on derived stores where the return type is the same as readable stores. Therefore, createBaseQuery tries to make a writable store out of the derived store, causing something to break. Derived stores should definitely be supported, so I'll implement a fix shortly.

Relevant files: utils.ts, createBaseQuery.ts

lachlancollins commented 1 year ago

Fixed in #5672 !