|
| 1 | +--- |
| 2 | +id: usage |
| 3 | +title: Usage |
| 4 | +sidebar_label: Usage |
| 5 | +description: usage |
| 6 | +slug: /getting-started/usage |
| 7 | +sidebar_position: 4 |
| 8 | +--- |
| 9 | + |
| 10 | +# Usage |
| 11 | + |
| 12 | +## 1. Create [`MobxQueryClient`](/api/mobx-query-client/overview) instance |
| 13 | + |
| 14 | +```ts title="@/shared/lib/tanstack-query/query-client.ts" |
| 15 | +import { hashKey } from '@tanstack/query-core'; |
| 16 | +import { MobxQueryClient } from 'mobx-tanstack-query'; |
| 17 | + |
| 18 | +const MAX_FAILURE_COUNT = 3; |
| 19 | + |
| 20 | +export const queryClient = new MobxQueryClient({ |
| 21 | + defaultOptions: { |
| 22 | + queries: { |
| 23 | + throwOnError: true, |
| 24 | + queryKeyHashFn: hashKey, |
| 25 | + refetchOnWindowFocus: 'always', |
| 26 | + refetchOnReconnect: 'always', |
| 27 | + staleTime: 5 * 60 * 1000, |
| 28 | + retry: (failureCount, error) => { |
| 29 | + if ('status' in error && Number(error.status) >= 500) { |
| 30 | + return MAX_FAILURE_COUNT - failureCount > 0; |
| 31 | + } |
| 32 | + return false; |
| 33 | + }, |
| 34 | + }, |
| 35 | + mutations: { |
| 36 | + throwOnError: true, |
| 37 | + }, |
| 38 | + }, |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +Or you can use [`QueryClient`](https://tanstack.com/query/v5/docs/reference/QueryClient) instance from TanStack Query core package. |
| 43 | +But you need to `mount` that instance of `QueryClient` |
| 44 | + |
| 45 | +```ts |
| 46 | +queryClient.mount(); // enable all subscriptions for online\offline and window focus/blur |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +## 2. Use |
| 51 | + |
| 52 | +```ts |
| 53 | +const petsListQuery = new MobxQuery({ |
| 54 | + queryClient, |
| 55 | + queryKey: ['pets'], |
| 56 | + queryFn: async ({ signal, queryKey }) => { |
| 57 | + const response = await petsApi.fetchPets({ signal }); |
| 58 | + return response.data; |
| 59 | + }, |
| 60 | +}); |
| 61 | + |
| 62 | +const addPetsMutation = new MobxMutation({ |
| 63 | + queryClient, |
| 64 | + mutationFn: async (payload: { petName: string }) => { |
| 65 | + const response = await petsApi.createPet(payload); |
| 66 | + return response.data; |
| 67 | + }, |
| 68 | + |
| 69 | + onSuccess: (data) => { |
| 70 | + rootStore.notifications.push({ |
| 71 | + type: 'success', |
| 72 | + title: `Pet created successfully with name ${data.name}`, |
| 73 | + }); |
| 74 | + petsListQuery.invalidate(); |
| 75 | + }, |
| 76 | + onError: (error) => { |
| 77 | + rootStore.notifications.push({ |
| 78 | + type: 'danger', |
| 79 | + title: 'Failed to create pet', |
| 80 | + }); |
| 81 | + } |
| 82 | +}); |
| 83 | + |
| 84 | +addPetsMutation.mutate({ petName: 'fluffy' }); |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | +# Another usage or `mobx-tanstack-query/preset` |
| 89 | + |
| 90 | +This sub folder is contains already configured [instance of QueryClient](/src/preset/query-client.ts) with [this configuration](/src/preset/configs/default-query-client-config.ts) and needed to reduce your boilerplate with more compact way. |
| 91 | +Every parameter in configuration you can override using this construction |
| 92 | +```ts |
| 93 | +import { queryClient } from "mobx-tanstack-query/preset"; |
| 94 | + |
| 95 | +const defaultOptions = queryClient.getDefaultOptions(); |
| 96 | +defaultOptions.queries!.refetchOnMount = true; |
| 97 | +queryClient.setDefaultOptions({ ...defaultOptions }) |
| 98 | +``` |
| 99 | + |
| 100 | + |
| 101 | +P.S. Overriding default options should be written before start whole application |
| 102 | + |
| 103 | + |
| 104 | +### `createQuery(queryFn, otherOptionsWithoutFn?)` |
| 105 | + |
| 106 | +This is alternative for `new MobxQuery()`. Example: |
| 107 | + |
| 108 | +```ts |
| 109 | +import { createQuery } from "mobx-tanstack-query/preset"; |
| 110 | + |
| 111 | +const query = createQuery(async ({ signal, queryKey }) => { |
| 112 | + const response = await petsApi.fetchPets({ signal }); |
| 113 | + return response.data; |
| 114 | +}, { |
| 115 | + queryKey: ['pets'], |
| 116 | +}) |
| 117 | +``` |
| 118 | + |
| 119 | +### `createMutation(mutationFn, otherOptionsWithoutFn?)` |
| 120 | + |
| 121 | +This is alternative for `new MobxMutation()`. Example: |
| 122 | + |
| 123 | +```ts |
| 124 | +import { createMutation } from "mobx-tanstack-query/preset"; |
| 125 | + |
| 126 | +const mutation = createMutation(async (payload: { petName: string }) => { |
| 127 | + const response = await petsApi.createPet(payload); |
| 128 | + return response.data; |
| 129 | +}) |
| 130 | +``` |
| 131 | + |
| 132 | + |
| 133 | +### `createInfiniteQuery(queryFn, otherOptionsWithoutFn?)` |
| 134 | + |
| 135 | +This is alternative for `new MobxInfiniteQuery()`. Example: |
| 136 | + |
| 137 | +```ts |
| 138 | +import { createInfiniteQuery } from "mobx-tanstack-query/preset"; |
| 139 | + |
| 140 | +const query = createInfiniteQuery(async ({ signal, queryKey }) => { |
| 141 | + const response = await petsApi.fetchPets({ signal }); |
| 142 | + return response.data; |
| 143 | +}) |
| 144 | +``` |
| 145 | + |
0 commit comments