Open History to see the last commit
TanStack Svelte Query Playground Example (SvelteKit)#
A todo list with adjustable staleTime, gcTime and error rate, to see how queries and mutations react to them.
Built with SvelteKit.
How it works#
src/lib/services/todos/queries.tsdefinestodosQueries.list(filter)andtodosQueries.detail(id)withqueryOptions, keyed under['todos', 'list']and['todos', 'detail', id], andtodosMutations.create()andupdate()withmutationOptions.src/lib/services/todos/api.tssimulates a server in memory. Every call waits a random time betweenqueryTimeMinandqueryTimeMaxand fails aterrorRate, which are reactive values fromsrc/lib/stores.svelte.ts.src/routes/+layout.sveltecreates theQueryClientwithenabled: browser, so queries only run in the browser.src/routes/+page.sveltebinds the inputs to the stores and callsqueryClient.setDefaultOptionswith the chosenstaleTimeandgcTimefrom an$effect.src/routes/TodosList.sveltereads each filtered list withcreateQuery, and "Force Refetch All" insrc/routes/App.sveltecallsqueryClient.invalidateQueries().src/routes/AddTodo.svelteandsrc/routes/EditTodo.svelterun the mutations withcreateMutation. On success they invalidatetodosQueries.lists(), and saving an edit also writes the result into the detail query withqueryClient.setQueryData.
To run this example:
npm installnpm run dev