Open History to see the last commit
1import { LitElement, html } from 'lit'
2import {
3 QueryClient,
4 QueryClientProvider,
5 createMutationController,
6 createQueryController,
7} from '@tanstack/lit-query'
8import { addTodoOnServer, fetchTodosFromServer, resetTodoApi } from './todoApi'
9import type { Todo, TodosResponse } from './todoApi'
10β
11resetTodoApi()
12β
13const queryClient = new QueryClient({
14 defaultOptions: {
15 queries: { retry: false },
16 mutations: { retry: false },
17 },
18})
19β
20class MutationExampleProvider extends QueryClientProvider {
21 constructor() {
22 super()
23 this.client = queryClient
24 }
25β
26 protected override createRenderRoot(): HTMLElement | DocumentFragment {
27 return this
28 }
29}
30β
31customElements.define('mutation-example-provider', MutationExampleProvider)
32β
33class MutationExample extends LitElement {
34 static properties = {
35 nextTitle: { state: true },
36 }
37β
38 private nextTitle = 'Created from mutation example'
39β
40 private readonly todos = createQueryController<TodosResponse, Error>(this, {
41 queryKey: ['todos'],
42 queryFn: fetchTodosFromServer,
43 })
44β
45 private readonly addTodo = createMutationController<Todo, Error, string>(
46 this,
47 {
48 mutationKey: ['create-todo'],
49 mutationFn: addTodoOnServer,
50 onSuccess: (created) => {
51 queryClient.setQueryData<TodosResponse>(['todos'], (existing) => {
52 if (!existing) {
53 return {
54 items: [created],
55 requestCount: 0,
56 source: 'cache',
57 }
58 }
59β
60 return {
61 items: [...existing.items, created],
62 requestCount: existing.requestCount,
63 source: 'cache',
64 }
65 })
66 },
67 },
68 )
69β
70 protected override createRenderRoot(): HTMLElement | DocumentFragment {
71 return this
72 }
73β
74 private onInput(event: Event): void {
75 const target = event.target as HTMLInputElement
76 this.nextTitle = target.value
77 }
78β
79 private submit(): void {
80 const title = this.nextTitle.trim()
81 if (!title) return
82 this.addTodo.mutate(title)
83 this.nextTitle = ''
84 }
85β
86 render() {
87 const query = this.todos()
88 const mutation = this.addTodo()
89 const items = query.data?.items ?? []
90β
91 return html`
92 <main>
93 <h1>Mutation Example</h1>
94 <p data-testid="mutation-query-status">
95 Query: <strong>${query.status}</strong>
96 </p>
97 <p data-testid="mutation-mutation-status">
98 Mutation: <strong>${mutation.status}</strong>
99 </p>
100β
101 <label for="title">Todo title</label>
102 <input
103 id="title"
104 data-testid="mutation-input"
105 .value=${this.nextTitle}
106 @input=${this.onInput}
107 />
108 <button data-testid="mutation-add" @click=${this.submit}>
109 Add Todo
110 </button>
111β
112 <ul data-testid="mutation-todo-list">
113 ${items.map(
114 (todo) =>
115 html`<li data-testid="mutation-todo-item">${todo.title}</li>`,
116 )}
117 </ul>
118 </main>
119 `
120 }
121}
122β
123customElements.define('mutation-example', MutationExample)
124β
125class MutationExampleRoot extends LitElement {
126 protected override createRenderRoot(): HTMLElement | DocumentFragment {
127 return this
128 }
129β
130 render() {
131 return html`
132 <mutation-example-provider>
133 <mutation-example></mutation-example>
134 </mutation-example-provider>
135 `
136 }
137}
138β
139customElements.define('mutation-example-root', MutationExampleRoot)