-
Notifications
You must be signed in to change notification settings - Fork 31
Provide examples for useQuery with reactive values #88
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
Comments
I'm pretty sure you're using this incorrectly and your fetch is only happening because You change your |
You're right. My example was invalid, lol. I literally forgot to add the |
Curious, based on the updated REPL, what else are you looking for? Is it just documentation or a better way to set things up? |
Part of it is documentation/examples. We couldn't find any of either for updating queries with new values. Attempting to follow the react-query paradigms lead us to trying to write reactive queries like Additionally we were having a lot of issues that seem to stem from #89/#91, but it doesn't seem like anyone is looking at issues/PRs? In general though, the svelte-query way of doing things involves us duplicating a lot of configuration at both the
In an ideal world, we'd just have something like Instead, we need to duplicate the query key logic and select function in
|
Ditto, I'd love to see some best practices documentation for custom hooks & reactive values! I'd like my component code to look similar to What's the proper |
I totally agree with @pzcfg, the current solution of using Some (more or less helpful) suggestions to reduce the code duplication: I found it unnecessary to repeat Although it can definitely be improved by a lot, I'd like to throw my adaptation of the generic type GenericUseQueryOptions<TQueryData, TError, TData, TKey, TQueryInput> =
Omit<
UseQueryOptions<TQueryData, TError, TData, [TKey, TQueryInput]>,
'queryKey' | 'queryFn'
>
export const createCustomHook = <
TKey extends string,
TQueryData,
TError,
TQueryInput = void,
TData = TQueryData
>(
key: TKey,
query: (input: TQueryInput) => Promise<TQueryData>
) => ({
key,
useQuery: (
input: TQueryInput,
options?: GenericUseQueryOptions<
TQueryData,
TError,
TData,
TKey,
TQueryInput
>
) => {
const queryStore = useQuery({
queryKey: [key, input],
queryFn: (context) => query(context.queryKey[1]),
...options,
})
return {
...queryStore,
// Add the reactive update helper
update: (input: TQueryInput) =>
queryStore.updateOptions({ queryKey: [key, input] }),
}
},
}) const myHook = createCustomHook('myQuery', (input: ...) =>
myQuery(input)
)
const query = myHook.useQuery(reactiveInput, { select: (data) => ... })
$: query.update(reactiveInput) |
You need to repeat the
Yeah we ended up building something similar to this, although were still having some issues with it. In our case we still wanted to update more than just the queryKey and so I think we still ended up having to write or at least pass in new select statements in multiple places. |
+1, this is a pain to the point I find I'd contemplating just use fetch. It is not idiomatic at all. |
After lots of debugging headaches #87 we discovered that we weren't using
useQuery
correctly when passing in reactive values.We searched all sorts of different keywords online, and the only reference I could find to reactive values in queries was this one issue #43
Could you please add examples for the docs using dependent queries with reactive values?
The text was updated successfully, but these errors were encountered: