RTK Query - Allow injectEndpoints to also provide tagTypes

See original GitHub issue

Hello!

I’ve been using RTK query with quite a bit of success. The injectEndpoints API is pretty useful for splitting endpoint definitions across multiple modules and even across multiple projects. The project I’m working in has 2 apps with a shared common library, and injectEndpoints allows me to define a base API definition in the common library and then add app-specific endpoints in each app’s respective codebase.

However, it’s a bit odd that I still need to define all tagTypes at the root createApi call, as injectEndpoints has no option for that. I don’t see anything logically in the way of being able to do this, and I assume the use case simply hasn’t been suggested yet.

Please let me know if this is infeasible for an architectural reason. I can work around this for now, but it’s not exactly ideal. Thanks!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:8
  • Comments:20 (12 by maintainers)

github_iconTop GitHub Comments

29reactions
phryneascommented, Oct 28, 2021

@sazzer no, that works perfectly well as you can see in this TS Playground

import '@reduxjs/toolkit'

import {createApi, fetchBaseQuery} from '@reduxjs/toolkit/dist/query'

const emptyApi = createApi({
  baseQuery: fetchBaseQuery(),
  endpoints: () => ({})
})

const apiWithTag = emptyApi.enhanceEndpoints({addTagTypes: ['Foo']})

const _apiWithTaggedEndpoint = apiWithTag.injectEndpoints({
  endpoints: build => ({
    test: build.query({
       query: () => "test",
       providesTags: ['Foo']
    }), 
    test2: build.query({
       query: () => "test",
       // @ts-expect-error
       providesTags: ['Bar']
    })
  })
})
5reactions
phryneascommented, May 10, 2022

api.enhanceEndpoints({ addTagTypes: [“Admin”] })

const authApi = api.injectEndpoints({ endpoints: (builder) => ({




const authApi = api.enhanceEndpoints({ addTagTypes: ["Admin"] }).injectEndpoints({
endpoints: (builder) => ({
Read more comments on GitHub >

github_iconTop Results From Across the Web

Code Splitting - Redux Toolkit
Calling injectEndpoints will inject the endpoints into the original API, but also give you that same API with correct types for these ...
Read more >
API Slices: Code Splitting and Generation | Redux Toolkit
Any provided tag types or endpoint definitions will be merged into the existing endpoint definitions for this API slice. Unlike injectEndpoints ...
Read more >
Redux Essentials, Part 8: RTK Query Advanced Patterns
RTK Query allows multiple components to subscribe to the same data, and will ensure that each unique set of data is only fetched...
Read more >
createApi - Redux Toolkit
RTK Query > API: createApi reference. ... It allows you to define a set of "endpoints" that describe how to ... TagTypes extends...
Read more >
Customizing Queries - Redux Toolkit
RTK Query provides reasonable defaults expected to cover the majority of use cases, while also allowing room for customization to alter ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found