Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

README.md

127 lines3.0 KB

1# @tanstack/lit-query
2​
3Lit adapter for `@tanstack/query-core` using Lit reactive controllers.
4​
5This package is currently experimental and v0.1. Pin exact versions if you use
6it in production while the API is early.
7​
8## Install
9​
10```bash
11npm install @tanstack/lit-query @tanstack/query-core lit
12```
13​
14For local development in this repository:
15​
16```bash
17pnpm install
18pnpm --dir packages/lit-query run build
19```
20​
21## Quick Start
22​
23```ts
24import { LitElement, html } from 'lit'
25import {
26 QueryClient,
27 QueryClientProvider,
28 createQueryController,
29} from '@tanstack/lit-query'
30​
31const client = new QueryClient({
32 defaultOptions: { queries: { retry: false } },
33})
34​
35class AppProvider extends QueryClientProvider {
36 constructor() {
37 super()
38 this.client = client
39 }
41customElements.define('app-provider', AppProvider)
42​
43class UsersView extends LitElement {
44 private readonly users = createQueryController(this, {
45 queryKey: ['users'],
46 queryFn: async () => {
47 const response = await fetch('/api/users')
48 return response.json() as Promise<Array<{ id: string; name: string }>>
49 },
50 })
51​
52 render() {
53 const query = this.users()
54 if (query.isPending) return html`Loading...`
55 if (query.isError) return html`Error`
56 return html`<ul>
57 ${query.data?.map((u) => html`<li>${u.name}</li>`)}
58 </ul>`
59 }
61customElements.define('users-view', UsersView)
62```
63​
64## API Surface
65​
66- `QueryClientProvider`, `useQueryClient`, `resolveQueryClient`
67- `createQueryController`
68- `createMutationController`
69- `createInfiniteQueryController`
70- `createQueriesController`
71- `useIsFetching`, `useIsMutating`, `useMutationState`
72- `queryOptions`, `infiniteQueryOptions`, `mutationOptions`
73​
74## Runnable Examples
75​
76This repo includes runnable Lit examples under the top-level `examples/lit`
77directory so they can be surfaced in the docs:
78​
79- `examples/lit/basic`: Vite Lit app covering query and mutation primitives.
80- `examples/lit/pagination`: pagination, prefetching, optimistic updates, and error recovery.
81- `examples/lit/ssr`: Lit SSR render, dehydrate, and hydrate flow.
82​
83Run an example from the repo root:
84​
85```bash
86pnpm --dir examples/lit/basic run dev
87pnpm --dir examples/lit/pagination run dev
88pnpm --dir examples/lit/ssr run dev
89```
90​
91Open:
92​
93- `http://127.0.0.1:4173/` (basic example)
94- `http://127.0.0.1:4183/` (pagination example app)
95- `http://127.0.0.1:4174/` (SSR example app)
96​
97Use a different port (optional):
98​
99```bash
100DEMO_PORT=4180 pnpm --dir examples/lit/basic run dev
101PAGINATION_DEMO_PORT=4181 PAGINATION_API_PORT=4182 pnpm --dir examples/lit/pagination run dev
102SSR_PORT=4180 pnpm --dir examples/lit/ssr run dev
103SSR_HOST=0.0.0.0 pnpm --dir examples/lit/ssr run dev
104```
105​
106## Integration Smoke
107​
108For the framework build smoke used in CI:
109​
110```bash
111pnpm --dir integrations/lit-vite run build
112```
113​
114## Quality Gates
115​
116From the repository root:
117​
118```bash
119pnpm --dir packages/lit-query run test:types
120pnpm --dir packages/lit-query run test:lib
121pnpm --dir packages/lit-query run build
122pnpm --dir packages/lit-query run test:build
123```
124​
125## License
126​
127MIT