Search code examples
reactjscachingnext.jsredux-toolkitrtk-query

How do you create tagTypes for multiple endpoints in RTK Query?


I need to code split my endpoints in RTK Query, but in all the examples I've seen, none give a use case where each endpoint has its own tagType for invalidation and refetching.

I also wasn't able to find anything on this in their docs, but maybe I just missed it.

I kind of found something from a forum but it feels more like a hack. https://github.com/reduxjs/redux-toolkit/issues/1510

I just started learning RTK Query and caching in general so maybe I am just not understanding something. Maybe having multiple tagTypes isn't even necessary? I'm just not sure.


Solution

  • That's not "a forum", that's me (the author of RTK Query) in the official issue tracker. ;)

    So yes, you can just run

    const injectedApi = emptyApi
        .enhanceEndpoints({ addTagTypes: ['Foo'] })
        .injectEndpoints({
            // ...
        })
    

    And yes, you can totally have multiple tag types. And either you inject them dynamically this way, or you have all your tag types at the central api definition. Both have their benefits and drawbacks - that's up to you to decide.