我应该使用 RTK Query per base URL 还是 Resource?

Should I use RTK Query per base URL or Resource?

我在一个大项目中工作,有很多 base URLresources 我们可以理解为许多不同的集成服务。

按照资源行(独立)在我的应用程序上插入 RTK,每个资源我都有一个 createApi()。

我走这条路是因为每个资源都有所有 CRUD 方法和一些自定义方法,假设我们有 10 个方法(调用)来管理每个资源并且,使用单个创建 API,我可以在单个 createApi/reducerPath.

上拥有 200 个方法

示例:(我认为这解决了我的问题)

// users-api.ts
 export const usersApi = createApi({
    reducerPath: 'usersApi',
    baseQuery: httpClientBaseQuery({
        baseUrl: 'http://localhost:3000/' // (same base url)
    })
  // getUsers(), getUser(), getManyUsers() createUser(), updateUser(), ... 
 })

// cards-api.ts
 export const cardsApi = createApi({
    reducerPath: 'cardsApi ',
    baseQuery: httpClientBaseQuery({
        baseUrl: 'http://localhost:3000/' // (same base url)
    })
  // ...
 })

// accounts-api.ts
 export const accountsApi = createApi({
    reducerPath: 'accountsApi ',
    baseQuery: httpClientBaseQuery({
        baseUrl: 'http://localhost:3000/' // (same base url)
    })
  // ...
 })

// preferences-api.ts
 export const preferencesApi = createApi({
    reducerPath: 'preferencesApi',
    baseQuery: httpClientBaseQuery({
        baseUrl: 'http://localhost:3000/' // (same base url)
    })
  // ...
 })

// templates-api.ts
 export const templatesApi = createApi({
    reducerPath: 'templatesApi',
    baseQuery: httpClientBaseQuery({
        baseUrl: 'http://localhost:3000/' // (different base url)
    })
  // ...
 })

...

来自 RTK 文档:

"createApi():RTK 查询功能的核心。它允许您定义一组端点,描述如何从一系列端点检索数据,包括如何获取和转换该数据的配置。在大多数情况下在某些情况下,您应该为每个应用程序使用一次,根据经验,“每个基础 URL 一个 API 切片”。“

问题是,这样使用 RTK 可以吗(每个资源)?我会遇到性能问题,还是方法不对?

我们建议您应该为每个互连数据源一个 createApi - 通常每个域一个。如果将其拆分为多个 createApi 调用,这些 api 不会相互影响,因此一个端点的突变无法自动触发从另一个 createApi.[= 重新获取横向影响的数据。 17=]

即使您只有一个 createApi 调用,您仍然可以使用 injectEndpoints.

Code splitting 方法将其拆分为多个文件
// a central, potentially empty api definition
import { emptySplitApi } from './emptySplitApi'

const extendedApi = emptySplitApi.injectEndpoints({
  endpoints: (build) => ({
    // adding only this endpoint in this file
    example: build.query({
      query: () => 'test',
    }),
  }),
  overrideExisting: false,
})

// and exporting the hook to be used from components
export const { useExampleQuery } = extendedApi