'Splitting API Definitions with RTK Query

I have three API definitions in my current scenario.

  • Groups
  • Group Members
  • Group Invitations

These routes relate to each other, in that accepting a group invitation will invalidate the list of groups a user has. As a result, I have defined them under the same API as they need to invalidate shared tags.

However, this results in quite a bloated file, as each of those definitions have their own endpoints associated with them. Reading the documentation for Code Splitting with RTK Query suggests using injectEndpoints. This looks like it will suit my purposes, but I've the documentation has no examples that I can find of how it should be implemented with multiple 'sub' endpoints.

I have a couple questions;

  1. If I was to utilize injectEndpoints to extend the Groups API with GroupInvitations & GroupMembers seperately, which API should be included in the call to configureScore?
  2. Is there a way to inject both endpoints into Groups at once, resulting in a single combinedApi?


Solution 1:[1]

  1. You always only configure your one emptyApi in your store setup. All the other "apis" you get by calling injectEndpoints are just a reference to that same api - but with additional types for autocomplete.

  2. Sure, just add multiple endpoints to the endpoints value of the injectEndpoints call.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 phry