Skip to content
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

feat(svelte-query): Svelte Query Adapter for TanStack Query #4768

Merged
merged 91 commits into from Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from 84 commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
7d08921
feat: svelte-query adapter
drejohnson Oct 18, 2022
232b8ac
add examples
drejohnson Oct 18, 2022
d0d8099
add @testing-library/svelte and svelte-jester
drejohnson Oct 19, 2022
59ac487
add transform for testing svelte components
drejohnson Oct 19, 2022
5f8c91d
re-export @tanstack/core core
drejohnson Oct 19, 2022
1c50f02
add a few initial test
drejohnson Oct 19, 2022
d352e80
delete tests in examples
drejohnson Oct 23, 2022
f6d4f8d
use ^ in dependencies
drejohnson Oct 23, 2022
3f19c34
delete and gitignore .vscode
drejohnson Oct 23, 2022
99140b9
rename basic-typescript to basic
drejohnson Oct 23, 2022
7454eff
add basic example to ci.json
drejohnson Oct 23, 2022
3950310
remove transform from global preset
drejohnson Oct 23, 2022
e0717f5
Merge branch 'main' into svelte-adapter
drejohnson Oct 23, 2022
f86449e
update version
drejohnson Oct 23, 2022
b8a2e39
don't gitignore lib directory in svelte examples
drejohnson Oct 23, 2022
2678b32
fix build files location in package.json
drejohnson Oct 23, 2022
e79c71f
Merge branch 'main' into svelte-adapter
TkDodo Oct 23, 2022
7a2d72f
chore: prettier formatting
drejohnson Oct 23, 2022
3955939
add missing state
drejohnson Oct 23, 2022
bdfeac4
chore: fix eslint errors
drejohnson Oct 23, 2022
86903d9
add context
drejohnson Nov 5, 2022
90bd962
unsubscribe
drejohnson Nov 5, 2022
bf46205
Merge branch 'main' into svelte-adapter
drejohnson Nov 5, 2022
49c96a3
add missing export
drejohnson Nov 11, 2022
9a03449
use svelte context
drejohnson Nov 11, 2022
3b3aeed
update version
drejohnson Nov 11, 2022
75cada4
update examples to use context
drejohnson Nov 11, 2022
026c912
Merge branch 'main' into svelte-adapter
drejohnson Nov 11, 2022
5910980
release: v4.14.5
drejohnson Nov 11, 2022
d1b85ea
remove onMount
drejohnson Nov 14, 2022
296f377
add onMount to setQueryClient
drejohnson Nov 14, 2022
976306e
remove unneeded exports
drejohnson Nov 14, 2022
2c79007
Merge branch 'main' into svelte-adapter
TkDodo Dec 30, 2022
6782e8f
Add basic svelte docs overview
lachlancollins Jan 5, 2023
d6fc0ca
Add SvelteKit 1.0 example
lachlancollins Jan 5, 2023
720a360
Merge pull request #1 from lachlancollins/svelte-adapter
lachlancollins Jan 5, 2023
77a8bed
Edit nodeResolve settings
lachlancollins Jan 6, 2023
5888103
More rollup and babel tweaks
lachlancollins Jan 6, 2023
946456b
Try svelte plugin options
lachlancollins Jan 6, 2023
62fb343
Separate out svelte config
lachlancollins Jan 6, 2023
bffaf80
Merge branch 'main' into main
lachlancollins Jan 6, 2023
1da3204
Switch to svelte-package
lachlancollins Jan 6, 2023
e36f405
Reset rollup config
lachlancollins Jan 6, 2023
4414db2
Output to ./build
lachlancollins Jan 6, 2023
d6a18b0
Merge branch 'rollup-fixes'
lachlancollins Jan 6, 2023
098a01a
Use vitest in svelte-query
lachlancollins Jan 6, 2023
3aa7023
Fix test imports
lachlancollins Jan 6, 2023
30cf564
Avoid transpiling TS during typecheck
lachlancollins Jan 6, 2023
59ff286
Fix vitest command
lachlancollins Jan 6, 2023
60b3741
More vitest migration work
lachlancollins Jan 6, 2023
53857d7
Minor fixes to tests
lachlancollins Jan 6, 2023
4867ede
Rename file to types.d.ts
lachlancollins Jan 6, 2023
a6796bc
Replace setQueryContext with QueryClientProvider
lachlancollins Jan 6, 2023
5e66984
Replace tabs with spaces for repo consistency
lachlancollins Jan 6, 2023
3795933
Update examples to sveltekit 1.0
lachlancollins Jan 6, 2023
97e79fa
Implement Hydrate component
lachlancollins Jan 6, 2023
09b50be
Add playground example
lachlancollins Jan 6, 2023
fb1af52
Start adding star-wars example
lachlancollins Jan 6, 2023
d68b131
Add films and film pages
lachlancollins Jan 7, 2023
2422aa2
Finish star-wars example
lachlancollins Jan 7, 2023
24ea771
Rename store functions with create prefix
lachlancollins Jan 7, 2023
ad076b9
Add correct favicons
lachlancollins Jan 7, 2023
53b8aa8
Reorder some files in svelte-query
lachlancollins Jan 7, 2023
47e358b
Undo rollup.config.ts changes
lachlancollins Jan 7, 2023
15b278c
Add new createQuery and createMutation tests
lachlancollins Jan 7, 2023
3600ee9
More descriptive test name
lachlancollins Jan 7, 2023
ab3e11b
Misc fixes from feedback
lachlancollins Jan 7, 2023
d3f6d10
Fix pnpm-lock
lachlancollins Jan 7, 2023
819ef56
Specify svelte-package source, update TS to 4.7.4
lachlancollins Jan 7, 2023
59649fb
Remove unnecessary packages in examples
lachlancollins Jan 7, 2023
089ccb2
Sync pnpm-lock
lachlancollins Jan 7, 2023
64c7dc9
Reset pnpm-lock to upstream
lachlancollins Jan 7, 2023
583c2e3
Run pnpm install
lachlancollins Jan 7, 2023
49144c4
Remove svelte-query from root tsconfg
lachlancollins Jan 7, 2023
db11e0a
Merge branch 'main' into main
TkDodo Jan 7, 2023
094df8d
Run prettier, remove unused import
lachlancollins Jan 7, 2023
a1aa970
Run prettier on svelte files
lachlancollins Jan 7, 2023
bcc96ff
Prettier for changed files
lachlancollins Jan 7, 2023
6aa0d97
Fix cipublish from svelte-query
lachlancollins Jan 7, 2023
731725d
rimraf to remove build/lib/package.json
lachlancollins Jan 7, 2023
1bc23a8
Run svelte-kit sync before vitest
lachlancollins Jan 7, 2023
488eff1
Add files field to package.json
lachlancollins Jan 7, 2023
35e1a68
Add types field to package.json
lachlancollins Jan 7, 2023
015943e
Bump svelte-query version to 4.20.0
lachlancollins Jan 7, 2023
e34e1d1
Add path alias to vitest
lachlancollins Jan 7, 2023
6bae962
Update vitest to 0.26
lachlancollins Jan 7, 2023
06b2de3
More CI improvements
lachlancollins Jan 8, 2023
cf14766
Remove --parallel from pnpm scripts
lachlancollins Jan 8, 2023
6651d34
Remove unused dependency
lachlancollins Jan 8, 2023
875a462
Add eslint settings
lachlancollins Jan 8, 2023
ef81d26
Add coverage report
lachlancollins Jan 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codesandbox/ci.json
@@ -1,6 +1,6 @@
{
"installCommand": "install:csb",
"sandboxes": ["/examples/react/basic-typescript", "/examples/solid/basic-typescript", "/examples/vue/basic"],
"sandboxes": ["/examples/react/basic-typescript", "/examples/solid/basic-typescript", "/examples/svelte/basic", "/examples/vue/basic"],
"packages": ["packages/**"],
"node": "16"
}
1 change: 1 addition & 0 deletions .prettierignore
@@ -0,0 +1 @@
/packages/svelte-query/.svelte-kit
2 changes: 1 addition & 1 deletion babel.config.js
Expand Up @@ -33,7 +33,7 @@ module.exports = {
].filter(Boolean),
overrides: [
{
exclude: ['./packages/solid-query/**', './packages/vue-query/**'],
exclude: ['./packages/solid-query/**', './packages/svelte-query/**', './packages/vue-query/**'],
presets: ['@babel/react'],
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/config.json
Expand Up @@ -688,7 +688,7 @@
"label": "Getting Started",
"children": [
{
"label": "Coming Soon",
"label": "Overview",
"to": "svelte/overview"
}
]
Expand Down
67 changes: 64 additions & 3 deletions docs/svelte/overview.md
@@ -1,8 +1,69 @@
---
id: overview
title: Svelte Query (Coming Soon)
title: Svelte Query
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DamianOsipiuk should this ref the react docs and replace some parts like the vue docs do?

---
id: overview
title: Overview
ref: docs/react/overview.md
replace: { 'React': 'Vue', 'react-query': 'vue-query' }
---

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally it should if content is appropriate.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this in a follow up @lachlancollins ?

---

> ⚠️ This module has not yet been developed. It requires an adapter similar to `react-query` to work. We estimate the amount of code to do this is low-to-moderate, but does require familiarity with the Svelte framework. If you would like to contribute this adapter, please open a PR!
The `@tanstack/svelte-query` package offers a 1st-class API for using TanStack Query via Svelte.

The `@tanstack/svelte-query` package offers a 1st-class API for using TanStack Query via Svelte. However, all of the primitives you receive from this API are core APIs that are shared across all of the TanStack Adapters including the Query Client, query results, query subscriptions, etc.
## Example

Include the QueryClientProvider near the root of your project:

```svelte
<script lang="ts">
import { QueryClientProvider, QueryClient } from '@tanstack/svelte-query'
import Simple from './lib/Example.svelte'

const queryClient = new QueryClient()
</script>

<QueryClientProvider client={queryClient}>
<Simple />
</QueryClientProvider>
```

Then call any function (e.g. createQuery) from any component:

```svelte
<script lang="ts">
import { createQuery } from '@tanstack/svelte-query'

const query = createQuery({
queryKey: ['todos'],
queryFn: () => fetchTodos(),
})
</script>

<div>
{#if $query.isLoading}
<p>Loading...</p>
{:else if $query.isError}
<p>Error: {$query.error.message}</p>
{:else if $query.isSuccess}
{#each $query.data as todo}
<p>{todo.title}</p>
{/each}
{/if}
</div>
```

## Available Functions

Svelte Query offers useful functions and components that will make managing server state in Svelte apps easier.

- `createQuery`
- `createQueries`
- `createInfiniteQuery`
- `createMutation`
- `useQueryClient`
- `useIsFetching`
- `useIsMutating`
- `useHydrate`
- `<QueryClientProvider>`
- `<Hydrate>`

## Important Differences between Svelte Query & React Query

Svelte Query offers an API similar to React Query, but there are some key differences to be mindful of.

- Many of the functions in Svelte Query return a Svelte store. To access values on these stores reactively, you need to prefix the store with a `$`. You can learn more about Svelte stores [here](https://svelte.dev/tutorial/writable-stores).
9 changes: 9 additions & 0 deletions examples/svelte/auto-refetching/.gitignore
@@ -0,0 +1,9 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
!lib/
38 changes: 38 additions & 0 deletions examples/svelte/auto-refetching/README.md
@@ -0,0 +1,38 @@
# create-svelte

Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).

## Creating a project

If you're seeing this, you've probably already done this step. Congrats!

```bash
# create a new project in the current directory
npm create svelte@latest

# create a new project in my-app
npm create svelte@latest my-app
```

## Developing

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:

```bash
npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open
```

## Building

To create a production version of your app:

```bash
npm run build
```

You can preview the production build with `npm run preview`.

> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
25 changes: 25 additions & 0 deletions examples/svelte/auto-refetching/package.json
@@ -0,0 +1,25 @@
{
"name": "@tanstack/query-example-svelte-auto-refetching",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json"
},
"dependencies": {
"@tanstack/svelte-query": "^4.12.0"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^1.0.0",
"@sveltejs/kit": "^1.0.0",
"svelte": "^3.54.0",
"svelte-check": "^2.9.2",
"svelte-preprocess": "^4.10.7",
"tslib": "^2.4.1",
"typescript": "^4.7.4",
"vite": "^4.0.0"
},
"type": "module"
}
81 changes: 81 additions & 0 deletions examples/svelte/auto-refetching/src/app.css
@@ -0,0 +1,81 @@
:root {
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
font-size: 16px;
line-height: 24px;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

.card {
padding: 2em;
}

main {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
9 changes: 9 additions & 0 deletions examples/svelte/auto-refetching/src/app.d.ts
@@ -0,0 +1,9 @@
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
// and what to do when importing types
declare namespace App {
// interface Locals {}
// interface PageData {}
// interface Error {}
// interface Platform {}
}
12 changes: 12 additions & 0 deletions examples/svelte/auto-refetching/src/app.html
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" type="image/svg+xml" href="%sveltekit.assets%/emblem-light.svg" />
<meta name="viewport" content="width=device-width" />
%sveltekit.head%
</head>
<body>
<div>%sveltekit.body%</div>
</body>
</html>
12 changes: 12 additions & 0 deletions examples/svelte/auto-refetching/src/routes/+layout.svelte
@@ -0,0 +1,12 @@
<script lang="ts">
import '../app.css'
import { QueryClientProvider, QueryClient } from '@tanstack/svelte-query'

const queryClient = new QueryClient()
</script>

<QueryClientProvider client={queryClient}>
<main>
<slot />
</main>
</QueryClientProvider>
101 changes: 101 additions & 0 deletions examples/svelte/auto-refetching/src/routes/+page.svelte
@@ -0,0 +1,101 @@
<script lang="ts">
import {
useQueryClient,
createQuery,
createMutation,
} from '@tanstack/svelte-query'

let intervalMs = 1000
let value: string

const client = useQueryClient()

const endpoint = 'http://localhost:5173/api/data'

const todos = createQuery<{ items: string[] }, Error>({
queryKey: ['refetch'],
queryFn: async () => await fetch(endpoint).then((r) => r.json()),
// Refetch the data every second
refetchInterval: intervalMs,
})

const addMutation = createMutation(
(value: string) => fetch(`${endpoint}?add=${value}`).then((r) => r.json()),
{ onSuccess: () => client.invalidateQueries(['refetch']) },
)
const clearMutation = createMutation(
() => fetch(`${endpoint}?clear=1`).then((r) => r.json()),
{
onSuccess: () => client.invalidateQueries(['refetch']),
},
)
</script>

<h1>Auto Refetch with stale-time set to 1s</h1>

<p>
This example is best experienced on your own machine, where you can open
multiple tabs to the same localhost server and see your changes propagate
between the two.
</p>

<label>
Query Interval speed (ms):{' '}
<div class="flex">
<input bind:value={intervalMs} type="number" step="100" />{' '}

<span
style="display:inline-block;
margin-left:.5rem;
width:.75rem;
height:.75rem;
background: {$todos.isFetching ? 'green' : 'transparent'};
transition:: {!$todos.isFetching ? 'all .3s ease' : 'none'};
border-radius: 100%;
transform: 'scale(2)"
/>
</div>
</label>
<h2>Todo List</h2>
<form
on:submit={(e) => {
e.preventDefault()
e.stopPropagation()
$addMutation.mutate(value, {
onSuccess: () => (value = ''),
})
}}
>
<input placeholder="enter something" bind:value />
</form>

{#if $todos.isLoading}
Loading...
{/if}
{#if $todos.error}
An error has occurred:
{$todos.error.message}
{/if}
{#if $todos.isSuccess}
<ul>
{#each $todos.data.items as item}
<li>{item}</li>
{/each}
</ul>
<div>
<button on:click={() => $clearMutation.mutate(undefined)}>
Clear All
</button>
</div>
{/if}
{#if $todos.isFetching}
<div style="color:darkgreen; font-weight:700">
'Background Updating...' : ' '
</div>
{/if}

<style>
li {
text-align: left;
}
</style>