Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Unmanageable API files #210

Closed
tompeace opened this issue Apr 17, 2021 · 1 comment
Closed

Unmanageable API files #210

tompeace opened this issue Apr 17, 2021 · 1 comment

Comments

@tompeace
Copy link

I find myself having large API files and having to use the following "composition" helper to separate different domains into separate files:

const withBuilder = (...arr) => builder => arr.reduce((acc, cur) => ({
    ...acc,
    ...cur(builder)
}), {})

createApi then looks like this:

// user.js
const users = builder => ({ getUsers: builder.query(...) })

// products.js
const products = builder => ({ getProducts: builder.query(...) })

//api.js
export const API = createApi({
  reducerPath: 'api',
  entityTypes: ['Users', 'Products'],
  baseQuery: fetchBaseQuery({ baseUrl: 'http://my.endpoint.com' }),
  endpoints: withBuilder(products, users)
})

I don't know if this should be included in the codebase, as it could simply be mentioned in the docs, but I definitely see this becoming a common pattern for many as their apps grow larger

@phryneas
Copy link
Collaborator

You can also use our recommended Code Splitting approach. Would that help?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants