|
| 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. |
0 commit comments