| 43 | customElements.define('demo-query-provider', DemoQueryProvider) |
| 44 | |
| 45 | class TanstackLitQueryDemo extends LitElement { |
| 46 | static properties = { |
| 47 | nextTodoTitle: { state: true }, |
| 48 | cacheSeedCount: { state: true }, |
| 49 | } |
| 50 | |
| 51 | private nextTodoTitle = 'Add mutation assertion' |
| 52 | private cacheSeedCount = 0 |
| 53 | |
| 54 | private readonly todosQuery = createQueryController<TodosResponse, Error>( |
| 55 | this, |
| 56 | { |
| 57 | queryKey: ['todos'], |
| 58 | queryFn: fetchTodosFromServer, |
| 59 | }, |
| 60 | ) |
| 61 | |
| 62 | private readonly createTodoMutation = createMutationController< |
| 63 | Todo, |
| 64 | Error, |
| 65 | string |
| 66 | >(this, { |
| 67 | mutationKey: ['create-todo'], |
| 68 | mutationFn: addTodoOnServer, |
| 69 | onSuccess: (createdTodo) => { |
| 70 | demoQueryClient.setQueryData<TodosResponse>(['todos'], (existing) => { |
| 71 | if (!existing) { |
| 72 | return { |
| 73 | items: [createdTodo], |
| 74 | requestCount: 0, |
| 75 | source: 'cache', |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return { |
| 80 | items: [...existing.items, createdTodo], |
| 81 | requestCount: existing.requestCount, |
| 82 | source: 'cache', |
| 83 | } |
| 84 | }) |
| 85 | }, |
| 86 | }) |
| 87 | |
| 88 | private readonly isFetching = useIsFetching(this, { |
| 89 | queryKey: ['todos'], |
| 90 | }) |
| 91 | |
| 92 | private readonly isMutating = useIsMutating(this, { |
| 93 | mutationKey: ['create-todo'], |
| 94 | }) |
| 95 | |
| 96 | protected override createRenderRoot(): HTMLElement | DocumentFragment { |
| 97 | return this |
| 98 | } |
| 99 | |
| 100 | private onTitleInput(event: Event): void { |
| 101 | const target = event.target as HTMLInputElement |
| 102 | this.nextTodoTitle = target.value |
nothing calls this directly
no test coverage detected