createApi is the core function of ngrx-rtk-query that generates an API slice with Angular-compatible hooks for queries and mutations. It wraps RTK Query’s buildCreateApi with Angular-specific hooks module.
Function Signature
Parameters
Configuration object for the API
Return Value
An API object with generated hooks and utilities
Usage Examples
Basic API Definition
Using Generated Hooks in Components
With Query Options
Integration with Store
The created API must be provided to Angular’s dependency injection system using eitherprovideStoreApi (with NgRx Store) or provideNoopStoreApi (standalone):
With NgRx Store
Without NgRx Store
Implementation Details
ThecreateApi function:
- Sets up dispatch mechanism: Creates a dispatch function that handles both regular actions and thunks, with access to Angular’s injector for dependency injection
-
Configures state selectors: Provides
getStateanduseSelectorfunctions that integrate with the store -
Builds the API: Uses RTK Query’s
buildCreateApiwith the core module and Angular hooks module -
Generates hooks: Automatically creates
useQuery,useLazyQuery,useMutation, anduseInfiniteQueryhooks for each endpoint - Initializes middleware: Sets up the RTK Query middleware with the configured dispatch and getState functions
Source Reference
Source:packages/ngrx-rtk-query/core/src/create-api.ts:19
See Also
- fetchBaseQuery - Base query function for HTTP requests
- buildHooks - Hook generation system
- useQuery Hook - Query hook reference
- useMutation Hook - Mutation hook reference