Skip to content

Commit 12b09a9

Browse files
committed
added initial internal overview.mdx
1 parent a3a80a0 commit 12b09a9

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

docs/rtk-query/internal/overview.mdx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# RTKQ internal
2+
3+
## Overview
4+
When `createApi()` is called it takes the options provided and calls internally the `buildCreateApi()` function passing into it two modules:
5+
6+
*Modules are RTK-Query's method of customizing how the `createApi` method handles endpoints.*
7+
8+
- `coreModule()` - responsible for the majority of the internal handling using core redux logic i.e. slices, reducers, asyncThunks.
9+
- `reactHooksModule()` - a module that generates react hooks from endpoints using react-redux
10+
11+
## Core Module
12+
13+
The core module takes the `api` and the options passed to `createApi()`. In turn an internal set of "build" methods are called. Each of these build methods create a set of functions which are assigned to either `api.util` or `api.internalActions` and/or passed to a future "build" step.
14+
15+
### buildThunks
16+
RTK-Query's internal functionality operates using the same `asyncThunk` exposed from RTK. In the first "build" method, a number of thunks are generated for the core module to use:
17+
18+
- `queryThunk`
19+
- `mutationThunk`
20+
- `patchedQueryData`
21+
- `updateQueryData`
22+
- `upsertQueryData`
23+
- `prefetch`
24+
- `buildMatchThunkActions`
25+
26+
### buildSlice
27+
RTK-Query uses a very familiar redux-centric architecture. Where the `api` is a slice of your store, the `api` has its own slices created within it. These slices are where the majority of the RTKQ magic happens.
28+
29+
The slices built inside this "build" are:
30+
*Some of which have their own actions*
31+
- querySlice
32+
- mutationSlice
33+
- invalidationSlice
34+
- subscriptionSlice (used as a dummy slice to generate actions internally)
35+
- internalSubscriptionsSlice
36+
- configSlice (internal tracking of focus state, online state, hydration etc)
37+
38+
buildSlice also exposes the core action `resetApiState` which is subsequently added to the `api.util`
39+
40+
### buildMiddleware
41+
RTK-Query has a series of custom middlewares established within its store to handle additional responses in addition to the core logic established within the slices from buildSlice.
42+
43+
Each middleware built during this step is referred to internally as a "Handler" and are as follows:
44+
45+
- `buildDevCheckHandler
46+
- `buildCacheCollectionHandler
47+
- `buildInvalidationByTagsHandler
48+
- `buildPollingHandler
49+
- `buildCacheLifecycleHandler
50+
- `buildQueryLifecycleHandler
51+
52+
### buildSelectors
53+
build selectors is a crucial step that exposes to the `api` and utils:
54+
55+
- `buildQuerySelector
56+
- `buildMutationSelector
57+
- `selectInvalidatedBy
58+
- `selectCachedArgsForQuery
59+
60+
### return
61+
Finally each endpoint passed into the `createApi()` is iterated over and assigned either the query or the mutation selectors, initiators and match cases.

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
"label": "Internal Docs",
150150
"collapsed": true,
151151
"items": [
152+
"rtk-query/internal/overview",
152153
"rtk-query/internal/queryThunk",
153154
{
154155
"type": "category",

0 commit comments

Comments
 (0)