DEV Community

Kacper Zawojski
Kacper Zawojski

Posted on

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

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: [/* ... */],
}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)