Skip to content

Commit 38f4796

Browse files
authored
Merge pull request #1015 from reduxjs/feature/rtkq-docs-integration
2 parents b74a529 + 6d9d69e commit 38f4796

33 files changed

+853
-380
lines changed

docs/api/configureStore.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ const store = configureStore({ reducer: rootReducer })
150150

151151
### Full Example
152152

153-
```ts
153+
```ts no-transpile
154154
// file: todos/todosReducer.ts noEmit
155155
import { Reducer } from '@reduxjs/toolkit'
156156
declare const reducer: Reducer<{}>
@@ -175,29 +175,29 @@ import visibilityReducer from './visibility/visibilityReducer'
175175
176176
const reducer = {
177177
todos: todosReducer,
178-
visibility: visibilityReducer
178+
visibility: visibilityReducer,
179179
}
180180
181181
const preloadedState = {
182182
todos: [
183183
{
184184
text: 'Eat food',
185-
completed: true
185+
completed: true,
186186
},
187187
{
188188
text: 'Exercise',
189-
completed: false
190-
}
189+
completed: false,
190+
},
191191
],
192-
visibilityFilter: 'SHOW_COMPLETED'
192+
visibilityFilter: 'SHOW_COMPLETED',
193193
}
194194
195195
const store = configureStore({
196196
reducer,
197-
middleware: getDefaultMiddleware => getDefaultMiddleware().concat(logger),
197+
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(logger),
198198
devTools: process.env.NODE_ENV !== 'production',
199199
preloadedState,
200-
enhancers: [reduxBatch]
200+
enhancers: [reduxBatch],
201201
})
202202
203203
// The store has been created with these options:

query-old/docs/api/ApiProvider.md renamed to docs/api/rtk-query/ApiProvider.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ hide_title: true
77

88
# `ApiProvider`
99

10-
[summary](docblock://react-hooks/ApiProvider.tsx?token=ApiProvider)
10+
[summary](docblock://query/react/ApiProvider.tsx?token=ApiProvider)
1111

12-
[examples](docblock://react-hooks/ApiProvider.tsx?token=ApiProvider)
12+
[examples](docblock://query/react/ApiProvider.tsx?token=ApiProvider)
1313

1414
:::danger
15-
Using this together with an existing redux store will cause them to conflict with each other. If you are already using Redux, please use follow the instructions as shown in the [Getting Started guide](../introduction/getting-started).
15+
Using this together with an existing Redux store will cause them to conflict with each other. If you are already using Redux, please use follow the instructions as shown in the [Getting Started guide](../../introduction/getting-started).
1616
:::
1717

1818
### Example

query-old/docs/api/createApi.md renamed to docs/api/rtk-query/createApi.md

Lines changed: 86 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,31 @@ hide_title: true
77

88
# `createApi`
99

10-
The main point where you will define a service to use in your application.
10+
`createApi` is the core of RTK Query's functionality. It allows you to define a set of endpoints describe how to retrieve data from a series of endpoints, including configuration of how to fetch and transform that data. It generates [an "API slice" structure](./created-api/overview.md) that contains Redux logic (and optionally React hooks) that encapsulate the data fetching and caching process for you.
11+
12+
```ts title="Example: src/services/pokemon.ts"
13+
// Need to use the React-specific entry point to allow generating React hooks
14+
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/react'
15+
16+
// highlight-start
17+
// Define a service using a base URL and expected endpoints
18+
export const pokemonApi = createApi({
19+
reducerPath: 'pokemonApi',
20+
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
21+
endpoints: (builder) => ({
22+
getPokemonByName: builder.query({
23+
query: (name: string) => `pokemon/${name}`,
24+
}),
25+
}),
26+
})
27+
//highlight-end
28+
29+
// highlight-start
30+
// Export hooks for usage in function components, which are
31+
// auto-generated based on the defined endpoints
32+
export const { useGetPokemonByNameQuery } = pokemonApi
33+
// highlight-end
34+
```
1135

1236
## Parameters
1337

@@ -27,74 +51,102 @@ The main point where you will define a service to use in your application.
2751

2852
### `baseQuery`
2953

30-
[summary](docblock://createApi.ts?token=CreateApiOptions.baseQuery)
54+
[summary](docblock://query/createApi.ts?token=CreateApiOptions.baseQuery)
3155

32-
[examples](docblock://createApi.ts?token=CreateApiOptions.baseQuery)
56+
[examples](docblock://query/createApi.ts?token=CreateApiOptions.baseQuery)
3357

3458
### `tagTypes`
3559

36-
[summary](docblock://createApi.ts?token=CreateApiOptions.tagTypes)
60+
[summary](docblock://query/createApi.ts?token=CreateApiOptions.tagTypes)
3761

3862
### `reducerPath`
3963

40-
[summary](docblock://createApi.ts?token=CreateApiOptions.reducerPath)
64+
[summary](docblock://query/createApi.ts?token=CreateApiOptions.reducerPath)
4165

42-
[examples](docblock://createApi.ts?token=CreateApiOptions.reducerPath)
66+
[examples](docblock://query/createApi.ts?token=CreateApiOptions.reducerPath)
4367

4468
### `serializeQueryArgs`
4569

46-
[summary](docblock://createApi.ts?token=CreateApiOptions.serializeQueryArgs)
70+
[summary](docblock://query/createApi.ts?token=CreateApiOptions.serializeQueryArgs)
4771

4872
Defaults to:
4973

5074
```ts no-compile
51-
export const defaultSerializeQueryArgs: SerializeQueryArgs<any> = ({ endpoint, queryArgs }) => {
75+
export const defaultSerializeQueryArgs: SerializeQueryArgs<any> = ({
76+
endpoint,
77+
queryArgs,
78+
}) => {
5279
// Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
53-
return `${endpoint}(${JSON.stringify(queryArgs, Object.keys(queryArgs || {}).sort())})`;
54-
};
80+
return `${endpoint}(${JSON.stringify(
81+
queryArgs,
82+
Object.keys(queryArgs || {}).sort()
83+
)})`
84+
}
5585
```
5686

5787
### `endpoints`
5888

59-
[summary](docblock://createApi.ts?token=CreateApiOptions.endpoints)
89+
[summary](docblock://query/createApi.ts?token=CreateApiOptions.endpoints)
6090

6191
#### Anatomy of an endpoint
6292

6393
- `query` _(required)_
64-
- [summary](docblock://endpointDefinitions.ts?token=EndpointDefinitionWithQuery.query)
94+
- [summary](docblock://query/endpointDefinitions.ts?token=EndpointDefinitionWithQuery.query)
6595
- `transformResponse` _(optional)_
6696

67-
- [summary](docblock://endpointDefinitions.ts?token=EndpointDefinitionWithQuery.transformResponse)
97+
- [summary](docblock://query/endpointDefinitions.ts?token=EndpointDefinitionWithQuery.transformResponse)
6898
- ```js title="Unpack a deeply nested collection"
69-
transformResponse: (response) => response.some.nested.collection;
99+
transformResponse: (response) => response.some.nested.collection
70100
```
71101
- ```js title="Normalize the response data"
72102
transformResponse: (response) =>
73103
response.reduce((acc, curr) => {
74-
acc[curr.id] = curr;
75-
return acc;
76-
}, {});
104+
acc[curr.id] = curr
105+
return acc
106+
}, {})
77107
```
78108

79109
- `provides` _(optional)_
80110

81-
[summary](docblock://endpointDefinitions.ts?token=QueryExtraOptions.provides)
111+
[summary](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.provides)
82112

83113
- `invalidates` _(optional)_
84114

85-
[summary](docblock://endpointDefinitions.ts?token=MutationExtraOptions.invalidates)
115+
[summary](docblock://query/endpointDefinitions.ts?token=MutationExtraOptions.invalidates)
86116

87-
- `onStart`, `onError` and `onSuccess` _(optional)_ - Available to both [queries](../concepts/queries) and [mutations](../concepts/mutations)
88-
- Can be used in `mutations` for [optimistic updates](../concepts/optimistic-updates).
117+
- `onStart`, `onError` and `onSuccess` _(optional)_ - Available to both [queries](../../usage/rtk-query/queries.md) and [mutations](../../usage/rtk-query/mutations.md)
118+
- Can be used in `mutations` for [optimistic updates](../../usage/rtk-query/optimistic-updates.md).
89119
- ```ts title="Mutation lifecycle signatures"
90-
function onStart(arg: QueryArg, mutationApi: MutationApi<ReducerPath, Context>): void;
91-
function onError(arg: QueryArg, mutationApi: MutationApi<ReducerPath, Context>, error: unknown): void;
92-
function onSuccess(arg: QueryArg, mutationApi: MutationApi<ReducerPath, Context>, result: ResultType): void;
120+
function onStart(
121+
arg: QueryArg,
122+
mutationApi: MutationApi<ReducerPath, Context>
123+
): void
124+
function onError(
125+
arg: QueryArg,
126+
mutationApi: MutationApi<ReducerPath, Context>,
127+
error: unknown
128+
): void
129+
function onSuccess(
130+
arg: QueryArg,
131+
mutationApi: MutationApi<ReducerPath, Context>,
132+
result: ResultType
133+
): void
93134
```
94135
- ```ts title="Query lifecycle signatures"
95-
function onStart(arg: QueryArg, queryApi: QueryApi<ReducerPath, Context>): void;
96-
function onError(arg: QueryArg, queryApi: QueryApi<ReducerPath, Context>, error: unknown): void;
97-
function onSuccess(arg: QueryArg, queryApi: QueryApi<ReducerPath, Context>, result: ResultType): void;
136+
function onStart(
137+
arg: QueryArg,
138+
queryApi: QueryApi<ReducerPath, Context>
139+
): void
140+
function onError(
141+
arg: QueryArg,
142+
queryApi: QueryApi<ReducerPath, Context>,
143+
error: unknown
144+
): void
145+
function onSuccess(
146+
arg: QueryArg,
147+
queryApi: QueryApi<ReducerPath, Context>,
148+
result: ResultType
149+
): void
98150
```
99151

100152
#### How endpoints get used
@@ -140,14 +192,14 @@ By default, the payload from the server is returned directly.
140192
141193
```ts
142194
function defaultTransformResponse(baseQueryReturnValue: unknown) {
143-
return baseQueryReturnValue;
195+
return baseQueryReturnValue
144196
}
145197
```
146198
147199
To change it, provide a function that looks like:
148200
149201
```ts
150-
transformResponse: (response) => response.some.deeply.nested.property;
202+
transformResponse: (response) => response.some.deeply.nested.property
151203
```
152204
153205
```ts title="GraphQL transformation example"
@@ -172,24 +224,24 @@ export const api = createApi({
172224
transformResponse: (response) => response.posts.data,
173225
}),
174226
}),
175-
});
227+
})
176228
```
177229
178230
### `keepUnusedDataFor`
179231
180-
[summary](docblock://createApi.ts?token=CreateApiOptions.keepUnusedDataFor)
232+
[summary](docblock://query/createApi.ts?token=CreateApiOptions.keepUnusedDataFor)
181233
182234
### `refetchOnMountOrArgChange`
183235
184-
[summary](docblock://createApi.ts?token=CreateApiOptions.refetchOnMountOrArgChange)
236+
[summary](docblock://query/createApi.ts?token=CreateApiOptions.refetchOnMountOrArgChange)
185237
186238
:::note
187239
You can set this globally in `createApi`, but you can also override the default value and have more granular control by passing `refetchOnMountOrArgChange` to each individual hook call or when dispatching the [`initiate`](#initiate) action.
188240
:::
189241
190242
### `refetchOnFocus`
191243
192-
[summary](docblock://createApi.ts?token=CreateApiOptions.refetchOnFocus)
244+
[summary](docblock://query/createApi.ts?token=CreateApiOptions.refetchOnFocus)
193245
194246
:::note
195247
You can set this globally in `createApi`, but you can also override the default value and have more granular control by passing `refetchOnFocus` to each individual hook call or when dispatching the [`initiate`](#initiate) action.
@@ -199,7 +251,7 @@ If you specify `track: false` when manually dispatching queries, RTK Query will
199251
200252
### `refetchOnReconnect`
201253
202-
[summary](docblock://createApi.ts?token=CreateApiOptions.refetchOnReconnect)
254+
[summary](docblock://query/createApi.ts?token=CreateApiOptions.refetchOnReconnect)
203255
204256
:::note
205257
You can set this globally in `createApi`, but you can also override the default value and have more granular control by passing `refetchOnReconnect` to each individual hook call or when dispatching the [`initiate`](#initiate) action.

query-old/docs/api/created-api/cache-management.md renamed to docs/api/rtk-query/created-api/cache-management.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ hide_title: true
77

88
# API Slices: Cache Management Utilities
99

10-
The API slice object includes cache management utilities that are used for implementing [optimistic updates](../../concepts/optimistic-updates.md). These are included in a `util` field inside the slice object.
10+
The API slice object includes cache management utilities that are used for implementing [optimistic updates](../../../usage/rtk-query/optimistic-updates.md). These are included in a `util` field inside the slice object.
1111

1212
### `updateQueryResult`
1313

query-old/docs/api/created-api/code-splitting.md renamed to docs/api/rtk-query/created-api/code-splitting.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ hide_title: true
77

88
# API Slices: Code Splitting and Generation
99

10-
Each API slice allows [additional endpoint definitions to be injected at runtime](../../concepts/code-splitting.md) after the initial API slice has been defined. This can be beneficial for apps that may have _many_ endpoints.
10+
Each API slice allows [additional endpoint definitions to be injected at runtime](../../../usage/rtk-query/code-splitting.md) after the initial API slice has been defined. This can be beneficial for apps that may have _many_ endpoints.
1111

12-
The individual API slice endpoint definitions can also be split across multiple files. This is primarily useful for working with API slices that were [code-generated from an API schema file](../../concepts/code-generation.md), allowing you to add additional custom behavior and configuration to a set of automatically-generated endpoint definitions.
12+
The individual API slice endpoint definitions can also be split across multiple files. This is primarily useful for working with API slices that were [code-generated from an API schema file](../../../usage/rtk-query/code-generation.md), allowing you to add additional custom behavior and configuration to a set of automatically-generated endpoint definitions.
1313

1414
Each API slice object has `injectEndpoints` and `enhanceEndpoints` functions to support these use cases.
1515

@@ -18,11 +18,12 @@ Each API slice object has `injectEndpoints` and `enhanceEndpoints` functions to
1818
#### Signature
1919

2020
```ts
21-
const injectEndpoints = (endpointOptions: InjectedEndpointOptions) => EnhancedApiSlice;
21+
const injectEndpoints = (endpointOptions: InjectedEndpointOptions) =>
22+
EnhancedApiSlice
2223

2324
interface InjectedEndpointOptions {
24-
endpoints: (build: EndpointBuilder) => NewEndpointDefinitions;
25-
overrideExisting?: boolean;
25+
endpoints: (build: EndpointBuilder) => NewEndpointDefinitions
26+
overrideExisting?: boolean
2627
}
2728
```
2829

@@ -41,11 +42,12 @@ This method is primarily useful for code splitting and hot reloading.
4142
#### Signature
4243

4344
```ts
44-
const enhanceEndpoints = (endpointOptions: EnhanceEndpointsOptions) => EnhancedApiSlice;
45+
const enhanceEndpoints = (endpointOptions: EnhanceEndpointsOptions) =>
46+
EnhancedApiSlice
4547

4648
interface EnhanceEndpointsOptions {
47-
addTagTypes?: readonly string[];
48-
endpoints?: Record<string, Partial<EndpointDefinition>>;
49+
addTagTypes?: readonly string[]
50+
endpoints?: Record<string, Partial<EndpointDefinition>>
4951
}
5052
```
5153

0 commit comments

Comments
 (0)