Skip to content

Commit 98098c0

Browse files
committed
chore: Added migration docs and new test for payload
1 parent 63c12ec commit 98098c0

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

docs/docs/Migrations/v0.x.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Migrating to `1.0.0`
2+
3+
If you are using `@ryfylke-react/rtk-query-loader@0.3.51` or earlier, these docs will help you migrate your existing codebase over to `1.0.0`.
4+
5+
## Input format for data
6+
7+
Previously, queries have been passed using two arguments:
8+
9+
- **`queries`**
10+
- **`deferredQueries`**
11+
12+
These two functions used to return a `readonly UseQueryResult<T>[]`.
13+
14+
In version `1.0.0`, these arguments have now been joined into one, called `useQueries`.
15+
16+
```typescript {3-10,15-23}
17+
// Previously
18+
const loader = createLoader({
19+
queries: (arg: string) => {
20+
const pokemonQuery = useGetPokemon(arg);
21+
return [pokemonQuery] as const;
22+
},
23+
deferredQueries: () => {
24+
const otherQuery = useOtherQuery();
25+
return [otherQuery] as const;
26+
},
27+
});
28+
29+
// In version 1:
30+
const loader = createLoader({
31+
useQueries: (arg: string) => {
32+
const pokemonQuery = useGetPokemon(arg);
33+
const otherQuery = useOtherQuery();
34+
35+
return {
36+
queries: { pokemonQuery },
37+
deferredQueries: { otherQuery },
38+
};
39+
},
40+
});
41+
```
42+
43+
:::info
44+
Previously, you _had_ to use `transform` as well to expose the deferred queries to your consumer. This is no longer required.
45+
:::
46+
47+
## Output format for data
48+
49+
This also means that the output format of the loader has changed. Typescript will help you out here, but for the most part **what you send in is what you get out**.
50+
51+
```typescript
52+
const loader = createLoader({
53+
useQueries: () => ({
54+
queries: {
55+
pokemons: useGetPokemons(),
56+
},
57+
}),
58+
});
59+
60+
type LoaderData = InferLoaderData<typeof loader>;
61+
// {
62+
// queries: {
63+
// pokemons: UseQueryResult<Pokemon[]>
64+
// }
65+
// }
66+
```
67+
68+
This output format change will affect your consumer components, as well as the (now single) argument passed to `transform`.
69+
70+
You can still of course optionally [transform](../Features/transforming.md) the output.
71+
72+
:::tip You should know
73+
This I/O interface change is the **only breaking change** from the previous versions.
74+
:::
75+
76+
:::note
77+
Through rewriting the tests and lots of local testing, I can assure you that this refactor should be relatively straight forward and easy to do. If you fix the loaders first by changing and `queries` and `deferredQueries` to now use `useQueries`, you can let Typescript help you out in the consumers afterwards.
78+
:::
79+
80+
## New feature: `payload`
81+
82+
You can now send _any_ data you want through the loader. This is useful for when your loader contains a lot of logic, or if you want to pass some handlers down to the consumer to change the arguments of your queries.
83+
84+
```typescript
85+
const loader = createLoader({
86+
useQueries: () => {
87+
const [pokemonName, setPokemonName] = useState("charizard");
88+
const pokemon = useGetPokemon(pokemonName);
89+
90+
return {
91+
queries: {
92+
pokemon,
93+
},
94+
payload: {
95+
changePokemon: setPokemonName,
96+
},
97+
};
98+
},
99+
});
100+
101+
const Consumer = withLoader((props, data) => {
102+
const { payload, queries } = data;
103+
// ...
104+
}, loader);
105+
```
106+
107+
Previously, if you wanted to do something like this, you had to use [useCreateQuery](../Exports/use-create-query.md), or a wrapper component. If you did, you can now refactor your code to use `payload` instead, which should be a lot more clean and flexible.
108+
109+
## Reporting bugs
110+
111+
If you find any bugs or issues with this new update, I strongly encourage you to [file a bug report](https://github.com/ryfylke-react-as/rtk-query-loader/issues/new?assignees=&labels=&template=bug_report.md&title=) on the Github repo.
112+
113+
I hope you are happy with the changes coming in version `1`. They have been thoroughly thought out and brewed on for a couple of months. Although it's never fun to push a breaking change, I hope you agree that these changes add a lot more flexibility to the loaders, and less confusion around I/O format. I'm personally very glad that I'm no longer required to rembember to use `as const` to ensure I get the correct types in my loader.

docs/docs/intro.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ npm i @ryfylke-react/rtk-query-loader
2929
:::tip Getting Started
3030
Get started with our recommended best practises by following the [**Quick guide**](/Quick%20guide).
3131
:::
32+
33+
:::danger These docs are for version 1.0.0 and up
34+
If you are using version `0.3.51` or earlier, please refer to the [**migration guide**](./Migrations/v0.x).
35+
:::

testing-app/src/tests.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,23 @@ describe("withLoader", () => {
298298
expect(screen.getByText("Success")).toBeVisible();
299299
});
300300

301+
test("Can send static payload to loader", async () => {
302+
const Component = withLoader(
303+
(_, loader) => {
304+
return <div>{loader.payload.foo}</div>;
305+
},
306+
createLoader({
307+
useQueries: () => ({
308+
payload: {
309+
foo: "bar" as const,
310+
},
311+
}),
312+
})
313+
);
314+
render(<Component />);
315+
expect(screen.getByText("bar")).toBeVisible();
316+
});
317+
301318
describe(".extend()", () => {
302319
test("Can extend onLoading", async () => {
303320
render(<ExtendedLoaderComponent />);

0 commit comments

Comments
 (0)