| 31 | customElements.define('mutation-example-provider', MutationExampleProvider) |
| 32 | |
| 33 | class 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 |
nothing calls this directly
no test coverage detected