Skip to content

Commit 9c749c7

Browse files
authored
Merge pull request #1 from scality/useChainedMutations
Add useChainedMutations utility function
2 parents c5ae73f + bbd5c83 commit 9c749c7

File tree

7 files changed

+7789
-3493
lines changed

7 files changed

+7789
-3493
lines changed

.babelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env",
4+
"@babel/preset-react",
5+
"@babel/preset-typescript"
6+
]
7+
}

README.md

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
# react-chained-query
22

3-
## Feature
3+
## Features
44

5-
react-chained-query provide a `useChainedQuery` hook that consit of a wrapper on top of react-query `useQuery`. This `useChainedQuery` hook allow chaining queries instead of runnning them concurently, it aims to solve problems that may occurs when hitting a slow backend with too many requests.
5+
### useChainedQuery
6+
7+
`useChainedQuery` hook consit of a wrapper on top of react-query `useQuery`. This `useChainedQuery` hook allow chaining queries instead of runnning them concurently, it aims to solve problems that may occurs when hitting a slow backend with too many requests.
68

79
By managing a queue and executing the request one after another, it could give the capability for an application to display the information sequentially.
810

11+
### useChainedMutations
12+
13+
This `useChainedMutations` hook takes an array of mutations and a function to compute the variables for the next mutation in the chain. It returns an object containing a `mutate` function that triggers the chain of mutations, a `computeVariablesForNext` function that computes the variables for the next mutation, and an array of `mutationsWithRetry` that includes a retry function for each mutation.
14+
915
## Dependencies
1016

1117
```json
@@ -23,30 +29,32 @@ TBD
2329

2430
## Quickstart
2531

32+
### useChainedQuery
33+
2634
```js
27-
import { QueryClient, QueryClientProvider } from "react-query";
28-
import { ChainedQueryProvider, useChainedQuery } from "./useChainedQuery";
29-
import { useEffect, useState } from "react";
35+
import { QueryClient, QueryClientProvider } from 'react-query';
36+
import { ChainedQueryProvider, useChainedQuery } from './useChainedQuery';
37+
import { useEffect, useState } from 'react';
3038

3139
const queryClient = new QueryClient();
3240

3341
function Component1() {
3442
const { data } = useChainedQuery({
35-
queryKey: ["key", "arg"],
43+
queryKey: ['key', 'arg'],
3644
queryFn: async () => {
3745
await new Promise((resolve) => setTimeout(resolve, 2_000));
38-
return "1";
46+
return '1';
3947
},
4048
});
4149
return <>{data}</>;
4250
}
4351

4452
function Component2() {
4553
const { data } = useChainedQuery({
46-
queryKey: ["key", "arg1"],
54+
queryKey: ['key', 'arg1'],
4755
queryFn: async () => {
4856
await new Promise((resolve) => setTimeout(resolve, 1_000));
49-
return "2";
57+
return '2';
5058
},
5159
});
5260
return <>{data}</>;
@@ -69,6 +77,61 @@ export default function App() {
6977

7078
[A complete example here](https://codesandbox.io/s/use-chained-query-forked-j3mfed?file=/src/App.tsx)
7179

80+
### useChainedMutations
81+
82+
```js
83+
import { useMutation } from 'react-query';
84+
import { useChainedMutations } from './useChainedMutations';
85+
86+
const useUpdatePosts = () => {
87+
return useMutation({
88+
mutationFn: async (id: string) => {
89+
const res = await fetch(
90+
`https://jsonplaceholder.typicode.com/posts/${id}`,
91+
{
92+
headers: {
93+
'Content-type': 'application/json; charset=UTF-8',
94+
},
95+
method: 'PUT',
96+
body: JSON.stringify({
97+
id: 1,
98+
title: 'foo',
99+
body: 'bar',
100+
userId: id,
101+
}),
102+
},
103+
);
104+
105+
if (!res.ok) throw res.statusText;
106+
107+
return await res.json();
108+
},
109+
});
110+
};
111+
112+
const mutations = [{ ...useUpdatePosts, 'user1'}, { ...useUpdatePosts, 'user2'}];
113+
const { mutate } = useChainedMutations({
114+
mutations,
115+
computeVariablesForNext: {
116+
user1: () => {
117+
return 'user1';
118+
},
119+
user2: () => {
120+
return 'user2';
121+
}
122+
},
123+
});
124+
125+
export default function App() {
126+
return (
127+
<div className="App">
128+
<h2>Hello, useChainedMutations! </h2>
129+
<button onClick={() => mutate()}>
130+
</div>
131+
);
132+
}
133+
```
134+
72135
## Advanced Documentation
73136

74137
In order to use `useChainedQuery` in your component, it has be below `QueryClientProvider` and `ChainedQueryProvider`.

0 commit comments

Comments
 (0)