You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/rtk-query/api/created-api/code-splitting.mdx
+38-26
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Each API slice allows [additional endpoint definitions to be injected at runtime
13
13
14
14
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/code-generation.mdx), allowing you to add additional custom behavior and configuration to a set of automatically-generated endpoint definitions.
15
15
16
-
Each API slice object has `injectEndpoints`and `enhanceEndpoints` functions to support these use cases.
16
+
Each API slice object has `injectEndpoints`, `addTagTypes`and `enhanceEndpoint` functions to support these use cases.
17
17
18
18
## `injectEndpoints`
19
19
@@ -39,29 +39,44 @@ Endpoints will not be overridden unless `overrideExisting` is set to `true`. If
39
39
40
40
This method is primarily useful for code splitting and hot reloading.
Accepts a number of new tags to add to the API slice.
53
+
54
+
Returns an updated and enhanced version of the API slice object, with the new tags added.
55
+
56
+
This is primarily useful for chaining before `injectEndpoints` or `enhanceEndpoint`, to add tags which can then be used by new/enhanced endpoints.
57
+
58
+
## `enhanceEndpoint`
59
+
60
+
#### Signature
61
+
62
+
```ts no-transpile
63
+
const enhanceEndpoint = (
64
+
endpointName:string,
65
+
partialDefinition:
66
+
|Partial<EndpointDefinition>
67
+
| ((definition:EndpointDefinition) =>void)
68
+
) =>EnhancedApiSlice
54
69
```
55
70
56
71
#### Description
57
72
58
-
Any provided tag types or endpoint definitions will be merged into the existing endpoint definitions for this API slice. Unlike `injectEndpoints`, the partial endpoint definitions will not _replace_ existing definitions, but are rather merged together on a per-definition basis (ie,`Object.assign(existingEndpoint, newPartialEndpoint)`).
73
+
Provided partial definition will be merged into the existing endpoint definition for this API slice. Unlike `injectEndpoints`, the partial endpoint definition will not _replace_the existing definition, but is rather merged together (i.e.`Object.assign(existingEndpoint, newPartialEndpoint)`).
59
74
60
75
Returns an updated and enhanced version of the API slice object, containing the combined endpoint definitions.
61
76
62
77
This is primarily useful for taking an API slice object that was code-generated from an API schema file like OpenAPI, and adding additional specific hand-written configuration for cache invalidation management on top of the generated endpoint definitions.
63
78
64
-
For example, `enhanceEndpoints` can be used to modify caching behavior by changing the values of `providesTags`, `invalidatesTags`, and `keepUnusedDataFor`:
79
+
For example, `enhanceEndpoint` can be used to modify caching behavior by changing the values of `providesTags`, `invalidatesTags`, and `keepUnusedDataFor`:
65
80
66
81
```ts
67
82
// file: api.ts noEmit
@@ -91,20 +106,17 @@ export const api = createApi({
91
106
// file: enhanceEndpoints.ts
92
107
import { api } from'./api'
93
108
94
-
const enhancedApi =api.enhanceEndpoints({
95
-
addTagTypes: ['User'],
96
-
endpoints: {
97
-
getUserByUserId: {
98
-
providesTags: ['User'],
99
-
},
100
-
patchUserByUserId: {
101
-
invalidatesTags: ['User'],
102
-
},
103
-
// alternatively, define a function which is called with the endpoint definition as an argument
104
-
getUsers(endpoint) {
105
-
endpoint.providesTags= ['User']
106
-
endpoint.keepUnusedDataFor=120
107
-
},
108
-
},
109
-
})
109
+
const enhancedApi =api
110
+
.addTagTypes('User')
111
+
.enhanceEndpoint('getUserByUserId', {
112
+
providesTags: ['User'],
113
+
})
114
+
.enhanceEndpoint('patchUserByUserId', {
115
+
invalidatesTags: ['User'],
116
+
})
117
+
// alternatively, define a function which is called with the endpoint definition as an argument
@@ -114,7 +120,7 @@ Each API slice allows [additional endpoint definitions to be injected at runtime
114
120
115
121
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/code-generation.mdx), allowing you to add additional custom behavior and configuration to a set of automatically-generated endpoint definitions.
116
122
117
-
Each API slice object has `injectEndpoints`and `enhanceEndpoints` functions to support these use cases.
123
+
Each API slice object has `injectEndpoints`, `addTagTypes`and `enhanceEndpoint` functions to support these use cases.
Copy file name to clipboardexpand all lines: docs/rtk-query/usage/code-generation.mdx
+1-1
Original file line number
Diff line number
Diff line change
@@ -69,7 +69,7 @@ That will result in all generated endpoints having `providesTags`/`invalidatesTa
69
69
70
70
Note that this will only result in string tags with no ids, so it might lead to scenarios where too much is invalidated and unneccessary requests are made on mutation.
71
71
72
-
In that case it is still recommended to manually specify tags by using [`enhanceEndpoints`](../api/created-api/code-splitting.mdx) on top of the generated api and manually declare `providesTags`/`invalidatesTags`.
72
+
In that case it is still recommended to manually specify tags by using [`addTagTypes` and `enhanceEndpoint`](../api/created-api/code-splitting.mdx) on top of the generated api and manually declare `providesTags`/`invalidatesTags`.
0 commit comments