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

queries.ts

32 lines935 Bytes

1import { mutationOptions, queryOptions } from '@tanstack/solid-query'
2import { moviesAPI } from './api'
3​
4export const moviesQueries = {
5 all: () => ['movies'] as const,
6 list: () =>
7 queryOptions({
8 queryKey: [...moviesQueries.all(), 'list'] as const,
9 queryFn: () => moviesAPI.getMovies(),
10 }),
11 detail: (id: string) =>
12 queryOptions({
13 queryKey: [...moviesQueries.all(), 'detail', id] as const,
14 queryFn: () => moviesAPI.getMovie(id),
15 }),
17​
18export const moviesMutations = {
19 update: () =>
20 mutationOptions({
21 mutationFn: async (
22 { id, comment }: { id: string; comment: string },
23 context,
24 ) => {
25 // to avoid clashes with our optimistic update when an offline mutation continues
26 await context.client.cancelQueries({
27 queryKey: moviesQueries.detail(id).queryKey,
28 })
29 return moviesAPI.updateMovie(id, comment)
30 },
31 }),