diff --git a/examples/lit/basic/src/basic-query.ts b/examples/lit/basic/src/basic-query.ts index 2ea03c0766..1e70694ee8 100644 --- a/examples/lit/basic/src/basic-query.ts +++ b/examples/lit/basic/src/basic-query.ts @@ -4,7 +4,8 @@ import { QueryClientProvider, createQueryController, } from '@tanstack/lit-query' -import { fetchTodosFromServer, resetTodoApi } from './todoApi' +import { resetTodoApi } from './services/todos/api' +import { todosQueries } from './services/todos/queries' resetTodoApi() @@ -28,10 +29,7 @@ class BasicQueryProvider extends QueryClientProvider { customElements.define('basic-query-provider', BasicQueryProvider) class BasicQueryExample extends LitElement { - private readonly todos = createQueryController(this, { - queryKey: ['todos'], - queryFn: fetchTodosFromServer, - }) + private readonly todos = createQueryController(this, todosQueries.list()) protected override createRenderRoot(): HTMLElement | DocumentFragment { return this diff --git a/examples/lit/basic/src/main.ts b/examples/lit/basic/src/main.ts index 013e25e597..1d3e8dbe5f 100644 --- a/examples/lit/basic/src/main.ts +++ b/examples/lit/basic/src/main.ts @@ -8,13 +8,12 @@ import { useIsMutating, } from '@tanstack/lit-query' import { - addTodoOnServer, failNextFetchRequest, failNextMutationRequest, - fetchTodosFromServer, resetTodoApi, -} from './todoApi' -import type { Todo, TodosResponse } from './todoApi' +} from './services/todos/api' +import { todosMutations, todosQueries } from './services/todos/queries' +import type { Todo, TodosResponse } from './services/todos/types' resetTodoApi() @@ -51,39 +50,38 @@ class TanstackLitQueryDemo extends LitElement { private nextTodoTitle = 'Add mutation assertion' private cacheSeedCount = 0 - private readonly todosQuery = createQueryController(this, { - queryKey: ['todos'], - queryFn: fetchTodosFromServer, - }) + private readonly todosQuery = createQueryController(this, todosQueries.list()) private readonly createTodoMutation = createMutationController(this, { - mutationKey: ['create-todo'], - mutationFn: addTodoOnServer, + ...todosMutations.create(), onSuccess: (createdTodo) => { - demoQueryClient.setQueryData(['todos'], (existing) => { - if (!existing) { + demoQueryClient.setQueryData( + todosQueries.list().queryKey, + (existing) => { + if (!existing) { + return { + items: [createdTodo], + requestCount: 0, + source: 'cache', + } + } + return { - items: [createdTodo], - requestCount: 0, + items: [...existing.items, createdTodo], + requestCount: existing.requestCount, source: 'cache', } - } - - return { - items: [...existing.items, createdTodo], - requestCount: existing.requestCount, - source: 'cache', - } - }) + }, + ) }, }) private readonly isFetching = useIsFetching(this, { - queryKey: ['todos'], + queryKey: todosQueries.all(), }) private readonly isMutating = useIsMutating(this, { - mutationKey: ['create-todo'], + mutationKey: todosMutations.create().mutationKey, }) protected override createRenderRoot(): HTMLElement | DocumentFragment { @@ -106,7 +104,7 @@ class TanstackLitQueryDemo extends LitElement { } private async invalidateTodos(): Promise { - await demoQueryClient.invalidateQueries({ queryKey: ['todos'] }) + await demoQueryClient.invalidateQueries({ queryKey: todosQueries.all() }) } private seedCacheOnlyTodo(): void { @@ -117,21 +115,24 @@ class TanstackLitQueryDemo extends LitElement { title: `Seeded cache todo ${this.cacheSeedCount}`, } - demoQueryClient.setQueryData(['todos'], (existing) => { - if (!existing) { + demoQueryClient.setQueryData( + todosQueries.list().queryKey, + (existing) => { + if (!existing) { + return { + items: [seedTodo], + requestCount: 0, + source: 'cache', + } + } + return { - items: [seedTodo], - requestCount: 0, + items: [...existing.items, seedTodo], + requestCount: existing.requestCount, source: 'cache', } - } - - return { - items: [...existing.items, seedTodo], - requestCount: existing.requestCount, - source: 'cache', - } - }) + }, + ) } private forceNextFetchFailure(): void { @@ -147,7 +148,7 @@ class TanstackLitQueryDemo extends LitElement { this.cacheSeedCount = 0 this.nextTodoTitle = 'Add mutation assertion' - await demoQueryClient.resetQueries({ queryKey: ['todos'] }) + await demoQueryClient.resetQueries({ queryKey: todosQueries.all() }) this.createTodoMutation.reset() } diff --git a/examples/lit/basic/src/mutation.ts b/examples/lit/basic/src/mutation.ts index 7853695797..1d80d4806f 100644 --- a/examples/lit/basic/src/mutation.ts +++ b/examples/lit/basic/src/mutation.ts @@ -5,8 +5,9 @@ import { createMutationController, createQueryController, } from '@tanstack/lit-query' -import { addTodoOnServer, fetchTodosFromServer, resetTodoApi } from './todoApi' -import type { TodosResponse } from './todoApi' +import { resetTodoApi } from './services/todos/api' +import { todosMutations, todosQueries } from './services/todos/queries' +import type { TodosResponse } from './services/todos/types' resetTodoApi() @@ -37,30 +38,29 @@ class MutationExample extends LitElement { private nextTitle = 'Created from mutation example' - private readonly todos = createQueryController(this, { - queryKey: ['todos'], - queryFn: fetchTodosFromServer, - }) + private readonly todos = createQueryController(this, todosQueries.list()) private readonly addTodo = createMutationController(this, { - mutationKey: ['create-todo'], - mutationFn: addTodoOnServer, + ...todosMutations.create(), onSuccess: (created) => { - queryClient.setQueryData(['todos'], (existing) => { - if (!existing) { + queryClient.setQueryData( + todosQueries.list().queryKey, + (existing) => { + if (!existing) { + return { + items: [created], + requestCount: 0, + source: 'cache', + } + } + return { - items: [created], - requestCount: 0, + items: [...existing.items, created], + requestCount: existing.requestCount, source: 'cache', } - } - - return { - items: [...existing.items, created], - requestCount: existing.requestCount, - source: 'cache', - } - }) + }, + ) }, }) diff --git a/examples/lit/basic/src/services/todos/api.ts b/examples/lit/basic/src/services/todos/api.ts new file mode 100644 index 0000000000..c25bfc466c --- /dev/null +++ b/examples/lit/basic/src/services/todos/api.ts @@ -0,0 +1,69 @@ +import type { Todo, TodosResponse } from './types' + +let todos: Array = [ + { id: 1, title: 'Ship lit-query alpha' }, + { id: 2, title: 'Write integration checks' }, +] + +let requestCount = 0 +let nextTodoId = 3 +let failNextFetch = false +let failNextMutation = false + +const delay = (ms: number): Promise => + new Promise((resolve) => { + setTimeout(resolve, ms) + }) + +export const todosAPI = { + getTodos: async (): Promise => { + await delay(90) + if (failNextFetch) { + failNextFetch = false + throw new Error('Forced fetch failure (test)') + } + requestCount += 1 + + return { + items: todos.map((todo) => ({ ...todo })), + requestCount, + source: 'server', + } + }, + createTodo: async (title: string): Promise => { + await delay(70) + if (failNextMutation) { + failNextMutation = false + throw new Error('Forced mutation failure (test)') + } + + const nextTodo: Todo = { + id: nextTodoId, + title, + } + + nextTodoId += 1 + todos = [...todos, nextTodo] + + return { ...nextTodo } + }, +} + +export function resetTodoApi(): void { + todos = [ + { id: 1, title: 'Ship lit-query alpha' }, + { id: 2, title: 'Write integration checks' }, + ] + requestCount = 0 + nextTodoId = 3 + failNextFetch = false + failNextMutation = false +} + +export function failNextFetchRequest(): void { + failNextFetch = true +} + +export function failNextMutationRequest(): void { + failNextMutation = true +} diff --git a/examples/lit/basic/src/services/todos/queries.ts b/examples/lit/basic/src/services/todos/queries.ts new file mode 100644 index 0000000000..fc65a71491 --- /dev/null +++ b/examples/lit/basic/src/services/todos/queries.ts @@ -0,0 +1,19 @@ +import { mutationOptions, queryOptions } from '@tanstack/lit-query' +import { todosAPI } from './api' + +export const todosQueries = { + all: () => ['todos'] as const, + list: () => + queryOptions({ + queryKey: [...todosQueries.all(), 'list'] as const, + queryFn: () => todosAPI.getTodos(), + }), +} + +export const todosMutations = { + create: () => + mutationOptions({ + mutationKey: [...todosQueries.all(), 'create'] as const, + mutationFn: (title: string) => todosAPI.createTodo(title), + }), +} diff --git a/examples/lit/basic/src/services/todos/types.ts b/examples/lit/basic/src/services/todos/types.ts new file mode 100644 index 0000000000..cc6faf393b --- /dev/null +++ b/examples/lit/basic/src/services/todos/types.ts @@ -0,0 +1,10 @@ +export type Todo = { + id: number + title: string +} + +export type TodosResponse = { + items: Array + requestCount: number + source: 'server' | 'cache' +} diff --git a/examples/lit/basic/src/todoApi.ts b/examples/lit/basic/src/todoApi.ts deleted file mode 100644 index 938973de53..0000000000 --- a/examples/lit/basic/src/todoApi.ts +++ /dev/null @@ -1,77 +0,0 @@ -export type Todo = { - id: number - title: string -} - -export type TodosResponse = { - items: Array - requestCount: number - source: 'server' | 'cache' -} - -let todos: Array = [ - { id: 1, title: 'Ship lit-query alpha' }, - { id: 2, title: 'Write integration checks' }, -] - -let requestCount = 0 -let nextTodoId = 3 -let failNextFetch = false -let failNextMutation = false - -const delay = (ms: number): Promise => - new Promise((resolve) => { - setTimeout(resolve, ms) - }) - -export async function fetchTodosFromServer(): Promise { - await delay(90) - if (failNextFetch) { - failNextFetch = false - throw new Error('Forced fetch failure (test)') - } - requestCount += 1 - - return { - items: todos.map((todo) => ({ ...todo })), - requestCount, - source: 'server', - } -} - -export async function addTodoOnServer(title: string): Promise { - await delay(70) - if (failNextMutation) { - failNextMutation = false - throw new Error('Forced mutation failure (test)') - } - - const nextTodo: Todo = { - id: nextTodoId, - title, - } - - nextTodoId += 1 - todos = [...todos, nextTodo] - - return { ...nextTodo } -} - -export function resetTodoApi(): void { - todos = [ - { id: 1, title: 'Ship lit-query alpha' }, - { id: 2, title: 'Write integration checks' }, - ] - requestCount = 0 - nextTodoId = 3 - failNextFetch = false - failNextMutation = false -} - -export function failNextFetchRequest(): void { - failNextFetch = true -} - -export function failNextMutationRequest(): void { - failNextMutation = true -}