VOOZH about

URL: https://dev.to/zawoj/payload-v4-forceselect-is-now-a-select-function-and-its-a-subtle-trap-42h8

⇱ Payload v4: forceSelect is now a select() function — and it's a subtle trap - DEV Community


In Payload v3, forceSelect was a static object that deep-merged with the caller's select. In v4 it became a function — and that function replaces the caller's select instead of adding to it:

export const Posts: CollectionConfig = {
 slug: 'posts',
 // Careful: you must spread the caller's select yourself, or you'll overwrite it!
 select: ({ operation, req, select }) =>
 select ? { ...select, title: true } : undefined,
 fields: [/* ... */],
}

Returning undefined means "leave the caller's select untouched." You also get operation and req, so you can hand back a different select for the REST API than for the admin panel.

One important detail: there's no document data here — select runs before read.