Support explicit depth-2 relations in the REST API via include

BO
24m ago
+714-46

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 include query 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.
  • include combines with depth: with depth=1, every direct relation is expanded and the include paths add their nested relations. Included relations are returned even when they aren't listed in fields.
  • 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 (new includeTree option), next to requestedFields. The REST handlers only parse the param and pass it through.
  • The "is this relation readable" check is shared between depth and include (getReadableRelationTargetFlatObjectMetadata).
  • getIsFlatFieldAJoinColumn and getIsFlatFieldAJunctionRelationField now take Pick<FlatFieldMetadata, 'name' | 'settings'>. This fixes two type errors on main in buildRelationSelectionFromDepth, which passes a narrowed field type to them.
  • OpenAPI spec and developers/extend/api.mdx document include.

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/api unit 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.