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

queries.ts

28 lines784 Bytes

1import { mutationOptions, queryOptions } from '@tanstack/react-query'
2import { todosAPI } from './api'
3​
4export const todosQueries = {
5 all: () => ['todos'] as const,
6 lists: () => [...todosQueries.all(), 'list'] as const,
7 list: (filter: string) =>
8 queryOptions({
9 queryKey: [...todosQueries.lists(), { filter }] as const,
10 queryFn: ({ signal }) => todosAPI.getTodos({ filter, signal }),
11 }),
12 detail: (id: number) =>
13 queryOptions({
14 queryKey: [...todosQueries.all(), 'detail', id] as const,
15 queryFn: () => todosAPI.getTodo({ id }),
16 }),
18​
19export const todosMutations = {
20 create: () =>
21 mutationOptions({
22 mutationFn: todosAPI.createTodo,
23 }),
24 update: () =>
25 mutationOptions({
26 mutationFn: todosAPI.updateTodo,
27 }),