RTK Query Sustainable Setup As Codebase Grows In Complexity #1295
-
I am loving RTK query for how powerful it is. My code, however, is getting really heavy. I feel as if many people would get turned off to RTK query solely for how complicated and dense the code ends up looking. Unless I am missing something, it seems like the convenience of TypeScript comes at the cost of everything needing to be defined in single object. It doesn't help that RTK Query feels like a fully fledged library itself and deserves its own separate documentation site. I am sure that the maintainers put A LOT of thought into this decision and have good reasons, especially since RTK query is internally built off of other parts of RTK. Regardless, the docs feel cluttered now. I have no problems understanding what is going on in both the docs and my code but I could imagine people who are newer to JS/TS/React/Redux being completely overwhelmed. I mean this in the most constructive possible way. This is my gut reaction to everything I have experienced so far. I never had this overwhelmed feeling when using other parts of RTK (like createSlice). I have been experimenting with splitting the endpoints up into separate files like this. The plan is to have one file per corresponding controller in the backend. This is my initial attempt: api/index.ts
api/campaign.ts
TS inference stuff works perfectly. Unfortunately cyclical dependencies bite me in the ass and the app crashes when I run it. The console says Object(...) is not a function since campaignEndpoints is not defined when createApi gets executed. I'm sure there are ways to restructure this to avoid circular dependencies. Does anyone have suggestions on a nice structure? Also if there is an elegant way to structure things to accommodate a growing codebase it may be worth adding as an example in the docs. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
My first quick thought is that you should be using the patterns shown in the "Code Splitting" page to split up the endpoint definitions, even if you're not actually trying to literally code-split: |
Beta Was this translation helpful? Give feedback.
-
Hello, I really like @agusterodin structure. The problem is that the utilities are only available on the api slice and not on the builder. An other thing that is annoying is that injectEndpoints does not modify the api slice in place so I end up doing something like: const withSomeEndpoints = addSomeEndpoints(baseApi);
const withOtherEndpoints = addOtherEndpoints(withSomeEndpoints);
...
export const fullApi = addAgainOtherEndpoints(withOtherEndpoints );
export {...} = fullApi;
... But maybe I missed something ? |
Beta Was this translation helpful? Give feedback.
My first quick thought is that you should be using the patterns shown in the "Code Splitting" page to split up the endpoint definitions, even if you're not actually trying to literally code-split:
https://redux-toolkit.js.org/rtk-query/usage/code-splitting