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

mutation.ts

136 lines3.3 KB

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