Open History to see the last commit
1# TanStack Svelte Query Default Query Function Example
2β
3Sets a default `queryFn` on the `QueryClient` that builds the request URL from the query key, so each query only passes a `queryKey`.
4β
5## How it works
6β
7- `src/App.svelte` defines `defaultQueryFn`, which fetches `https://jsonplaceholder.typicode.com` plus the first element of the `queryKey`, and sets it as `defaultOptions.queries.queryFn` on the `QueryClient`.
8- `src/lib/Posts.svelte` calls `createQuery` with only `queryKey: ['/posts']`, and `src/lib/Post.svelte` with ``queryKey: [`/posts/${postId}`]`` and `enabled: !!postId`. Neither passes a `queryFn`.
9- `src/lib/Posts.svelte` calls ``queryClient.getQueryData([`/posts/${post.id}`])`` to style links whose post is already cached.
10β
11To run this example:
12β
13- `npm install`
14- `npm run dev`