Skip to content

Commit b9a594d

Browse files
committed
docs: add docs for MobxQuery\MobxMutation
1 parent 2cd1c6e commit b9a594d

File tree

1 file changed

+232
-3
lines changed

1 file changed

+232
-3
lines changed

README.md

Lines changed: 232 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,239 @@ _**MobX** wrapper for [**Tanstack Query Core**](https://tanstack.com/query/lates
2020

2121
## What package supports
2222

23-
### [**Queries**](https://tanstack.com/query/latest/docs/framework/react/guides/queries) -> [**MobxQuery**](src/mobx-query.ts)
23+
# [**Queries**](https://tanstack.com/query/latest/docs/framework/react/guides/queries) -> [**MobxQuery**](src/mobx-query.ts)
24+
25+
Class wrapper for [@tanstack-query/core queries](https://tanstack.com/query/latest/docs/framework/react/guides/queries) with MobX reactivity
26+
27+
#### Usage
28+
29+
Create an instance of MobxQuery with [`queryKey`](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) and [`queryFn`](https://tanstack.com/query/latest/docs/framework/react/guides/query-functions) parameters
30+
```ts
31+
const query = new MobxQuery({
32+
queryClient,
33+
abortSignal, // Helps you to automatically clean up query
34+
queryKey: ['pets'],
35+
queryFn: async ({ signal, queryKey }) => {
36+
const response = await petsApi.fetchPets({ signal });
37+
return response.data;
38+
},
39+
});
40+
```
41+
42+
### Features
43+
44+
#### `enableOnDemand` option
45+
Query will be disabled until you request result for this query
46+
Example:
47+
```ts
48+
const query = new MobxQuery({
49+
//...
50+
enableOnDemand: true
51+
});
52+
// happens nothing
53+
query.result.data; // from this code line query starts fetching data
54+
```
55+
56+
#### dynamic `options`
57+
Options which can be dynamically updated for this query
58+
59+
```ts
60+
const query = new MobxQuery({
61+
// ...
62+
options: () => ({
63+
enabled: this.myObservableValue > 10,
64+
queryKey: ['foo', 'bar', this.myObservableValue] as const,
65+
}),
66+
queryFn: ({ queryKey }) => {
67+
const myObservableValue = queryKey[2];
68+
}
69+
});
70+
```
71+
72+
#### dynamic `queryKey`
73+
Works the same as dynamic `options` option but only for `queryKey`
74+
```ts
75+
const query = new MobxQuery({
76+
// ...
77+
queryKey: () => ['foo', 'bar', this.myObservableValue] as const,
78+
queryFn: ({ queryKey }) => {
79+
const myObservableValue = queryKey[2];
80+
}
81+
});
82+
```
83+
P.S. you can combine it with dynamic (out of box) `enabled` property
84+
```ts
85+
const query = new MobxQuery({
86+
// ...
87+
queryKey: () => ['foo', 'bar', this.myObservableValue] as const,
88+
enabled: ({ queryKey }) => queryKey[2] > 10,
89+
queryFn: ({ queryKey }) => {
90+
const myObservableValue = queryKey[2];
91+
}
92+
});
93+
```
94+
95+
#### method `update()`
96+
97+
Update options for query (Uses [QueryObserver](https://tanstack.com/query/latest/docs/reference/QueriesObserver).setOptions)
98+
99+
#### hook `onDone()`
100+
101+
Subscribe when query has been successfully fetched data
102+
103+
#### hook `onError()`
104+
105+
Subscribe when query has been failed fetched data
106+
107+
#### method `invalidate()`
108+
109+
Invalidate current query (Uses [queryClient.invalidateQueries](https://tanstack.com/query/latest/docs/reference/QueryClient/#queryclientinvalidatequeries))
110+
111+
#### method `reset()`
112+
113+
Reset current query (Uses [queryClient.resetQueries](https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientresetqueries))
114+
115+
#### method `setData()`
116+
117+
Set data for current query (Uses [queryClient.setQueryData](https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientsetquerydata))
118+
119+
#### property `isResultRequsted`
120+
Any time when you trying to get access to `result` property this field sets as `true`
121+
This field is needed for `enableOnDemand` option
122+
This property if **observable**
123+
124+
#### property `result`
125+
126+
**Observable** query result (The same as returns the [`useQuery` hook](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery))
127+
128+
129+
130+
### About `enabled`
131+
All queries are `enabled` (docs can be found [here](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery)) by default, but you can set `enabled` as `false` or use dynamic value like `({ queryKey }) => !!queryKey[1]`
132+
You can use `update` method to update value for this property or use dynamic options construction (`options: () => ({ enabled: !!this.observableValue })`)
133+
134+
135+
136+
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+
152+
153+
154+
155+
156+
157+
158+
159+
24160

25-
### [**Mutations**](https://tanstack.com/query/latest/docs/framework/react/guides/mutations) -> [**MobxMutation**](src/mobx-mutation.ts)
26161

27-
### [**InfiniteQueries**](https://tanstack.com/query/latest/docs/framework/react/guides/infinite-queries) -> [**MobxInfiniteQuery**](src/mobx-infinite-query.ts)
162+
163+
164+
165+
166+
167+
168+
169+
170+
171+
172+
173+
174+
175+
176+
177+
178+
179+
180+
181+
# [**Mutations**](https://tanstack.com/query/latest/docs/framework/react/guides/mutations) -> [**MobxMutation**](src/mobx-mutation.ts)
182+
183+
Class wrapper for [@tanstack-query/core mutations](https://tanstack.com/query/latest/docs/framework/react/guides/mutations) with MobX reactivity
184+
185+
#### Usage
186+
187+
Create an instance of MobxMutation with [`mutationFn`](https://tanstack.com/query/latest/docs/framework/react/guides/mutations) parameter
188+
```ts
189+
const mutation = new MobxMutation({
190+
queryClient,
191+
abortSignal, // Helps you to automatically clean up mutation
192+
mutationFn: async ({ signal, queryKey }) => {
193+
const response = await petsApi.createPet({ name: 'Fluffy' }, { signal });
194+
return response.data;
195+
},
196+
});
197+
```
198+
199+
### Features
200+
201+
### method `mutate(variables, options?)`
202+
203+
Runs the mutation. (Works the as `mutate` function in [`useMutation` hook](https://tanstack.com/query/latest/docs/framework/react/reference/useMutation))
204+
205+
#### hook `onDone()`
206+
207+
Subscribe when mutation has been successfully finished
208+
209+
#### hook `onError()`
210+
211+
Subscribe when mutation has been finished with failure
212+
213+
#### method `reset()`
214+
215+
Reset current mutation
216+
217+
#### property `result`
218+
219+
**Observable** mutation result (The same as returns the [`useMutation` hook](https://tanstack.com/query/latest/docs/framework/react/reference/useMutation))
220+
221+
222+
223+
224+
225+
226+
227+
228+
229+
230+
231+
232+
233+
234+
235+
236+
237+
238+
239+
240+
241+
242+
243+
244+
245+
246+
247+
248+
249+
250+
251+
252+
253+
# [**InfiniteQueries**](https://tanstack.com/query/latest/docs/framework/react/guides/infinite-queries) -> [**MobxInfiniteQuery**](src/mobx-infinite-query.ts)
254+
255+
[_See docs for MobxQuery_](https://github.com/js2me/mobx-tanstack-query?tab=readme-ov-file#queries---mobxquery)
28256

29257

30258

@@ -74,6 +302,7 @@ const addPetsMutation = new MobxMutation({
74302
const response = await petsApi.createPet(payload);
75303
return response.data;
76304
},
305+
77306
onSuccess: (data) => {
78307
rootStore.notifications.push({
79308
type: 'success',

0 commit comments

Comments
 (0)