Support explicit depth-2 relations in the REST API via include
BO
24m ago+714+714+012345678901234567890123456789-46-46-01234567890123456789
Fixes twentyhq/core-team-issues#1674
Problem
REST relations stop at depth=1. The old depth-2 implementation pulled in every relation at both levels (75 relations for people on a seeded workspace), so it timed out and ignored field-level permissions. There was no way to ask for a specific relation two levels deep.
Changes
- New
includequery param on the record endpoints, e.g.GET /rest/people?include=company.people,pointOfContactForOpportunities. Each path can be up to 2 relations deep and expands only what it names. includecombines withdepth: withdepth=1, every direct relation is expanded and theincludepaths add their nested relations. Included relations are returned even when they aren't listed infields.- Permissions are checked at every level against unreadable fields and readable objects. Unknown and unreadable relations return the same 400, so the error doesn't reveal fields hidden by permissions.
- Selection lives in
CommonSelectFieldsBuilder.buildFromDepth(newincludeTreeoption), next torequestedFields. The REST handlers only parse the param and pass it through. - The "is this relation readable" check is shared between depth and include (
getReadableRelationTargetFlatObjectMetadata). getIsFlatFieldAJoinColumnandgetIsFlatFieldAJunctionRelationFieldnow takePick<FlatFieldMetadata, 'name' | 'settings'>. This fixes two type errors on main inbuildRelationSelectionFromDepth, which passes a narrowed field type to them.- OpenAPI spec and
developers/extend/api.mdxdocumentinclude.
Performance
The existing runner guards still apply: a one-to-many nested in another one-to-many is rejected, and the query complexity limit is enforced. Unlike depth, an explicit include can expand timeline activities, since it's opt-in.
Testing
- Unit tests for the parser and the include selection builder; the existing
engine/apiunit tests pass (1,007). - Integration tests: include in find-many (nested selection, combined with depth, unknown relation, too-deep path), and in the depth field-permissions suite (a restricted nested relation returns 400, an unrestricted one returns the nested records). Both suites pass locally.
- Typecheck, oxlint and oxfmt are clean.