DEV Community

Kacper Zawojski
Kacper Zawojski

Posted on

Payload depth: how many relationship levels expand — at runtime, not in schema

depth controls how many levels of relationships Payload expands — and it's a runtime decision, not something baked into your schema.

The same document can come back "flat" or with its relationships populated, driven by a single query parameter:

// depth: 0 -> author is just an ID
{ id: '1', title: 'Hi', author: '64a...' }

// depth: 1 -> author is a full object
{ id: '1', title: 'Hi', author: { id: '64a...', name: 'Ada' } }
Enter fullscreen mode Exit fullscreen mode
await payload.findByID({ collection: 'posts', id, depth: 2 })
Enter fullscreen mode Exit fullscreen mode

And when you want to limit which fields get populated as a document is expanded from another document, reach for defaultPopulate on the collection — so you're not dragging entire heavy documents along for the ride.

Tune depth per query and you control your payload size without touching the data model.

Top comments (0)