RTK Query: baseUrl, scaling, and microservices #3317
-
I understand Redux team's recommendation is to have as few api slices as possible; ideally, just one. I've also heard that the recommended way of growing an api slice is via the In view of this, I would like your advice on how to scale the api slice to talk to various microservices. Consider the following four examples: Example 1Given two services, one at
Example 2Same two services as in example one above; but now hosted at different hostnames: one at Example 3Two services, but now they are speaking different json dialects: one, at Example 4A combination of example 1 and example 3. Four services now, with two being roughly restful ( |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Example 1I would set baseUrl to Example 2It depends on how related the data returned might be. The main reason you'd want to have as few API instances as possible is that you can't use You can provide a full URL (e.g. You could also decide that they're completely unrelated, and you're okay with having them in separate instances. Example 3Normally you would need separate API instances for separate base queries, yes. However, I wrote a recipe over at #3161 that allows you to create a "polymorphic" base query that discerns which base query to call based on the arguments provided to it. I use this at my work to use the same API instance for both REST fetch requests and AWS Amplify GraphQL queries. Example 4Essentially the same as above, the answer is "it's up to you". If they were all related data that I wanted to be able to invalidate each other, then I would use one API instance and a polymorphic base query. If the GraphQL endpoints were all entirely unrelated to the REST ones, then maybe it would be okay to have separate API instances for them. I'm not sure I would ever want 4 API instances for that setup, though. In general I've found that the benefits of keeping it all in one API instance have outweighed the downside of the necessary workarounds to keep it that way (absolute URLs, polymorphic base query). |
Beta Was this translation helpful? Give feedback.
Example 1
I would set baseUrl to
'/api'
. You'd then have separate endpoints with'/service-one'
and'/service-two'
.Example 2
It depends on how related the data returned might be. The main reason you'd want to have as few API instances as possible is that you can't use
providesTags
andinvalidatesTags
for automated refetching between API instances - it only applies for the given endpoint's API slice. There are of course also (mild) performance concerns when you have more API instances, since that's more middleware to run and more reducers.You can provide a full URL (e.g.
'https://service-one/api'
) for a given endpoint, and baseUrl will not be used (joinUrls
,isAbsoluteUrl
). You can use t…