Relate

Access Control & Security

In Relate, access control is declarative, non-leaking, and evaluated by the runtime on every read, traversal, and action.

Instead of authorization filters scattered across service endpoints, policies are part of the graph definition and compiled into its manifest. The host authenticates each caller and supplies their roles and claims; Relate decides what that principal may see or do.


Declaring Roles, Field Groups, and Claims

Use defineAccess to declare the vocabulary your policies use:

// access.ts
import { z } from 'zod';
import { defineAccess } from 'relate';

export const access = defineAccess({
  // Roles the host may assign to an authenticated principal.
  roles: ['employee', 'finance', 'account-manager'],
  // Sensitivity groups for properties. `ordinary` is the default group.
  fieldGroups: ['ordinary', 'financial'],
  // Attributes the host supplies for each principal.
  claims: { portfolio: z.string() },
});

This returns helpers used in definitions and policies:

  • access.role(name): a role gate for exactly one role.
  • access.groups.<name>: a field group to assign to properties.
  • access.claims.<name>: a claim to compare against property values.
  • access.actor.id: the calling principal's ID.

Assigning Properties to Field Groups

Each property can name the field group it belongs to:

// model.ts (excerpt)
import { z } from 'zod';
import { defineObject, defineSource, from, objectId, source } from 'relate';
import { access } from './access.js';

const { ordinary, financial } = access.groups;

export const crmCustomers = defineSource({
  id: 'crm.customers',
  idField: 'id',
  schema: z.object({
    id: z.string(),
    display_name: z.string(),
    portfolio: z.string(),
    status: z.string(),
    revenue: z.number(),
  }),
});

export const Customer = defineObject({
  id: 'business.customer',
  label: 'Customer',
  membership: source(crmCustomers),
  properties: {
    id: objectId({ id: 'business.customer.key', access: ordinary }),
    name: from(crmCustomers.fields.display_name, {
      id: 'business.customer.name',
      access: ordinary,
    }),
    portfolio: from(crmCustomers.fields.portfolio, {
      id: 'business.customer.portfolio',
      access: ordinary,
    }),
    status: from(crmCustomers.fields.status, {
      id: 'business.customer.status',
      access: ordinary,
    }),
    revenue: from(crmCustomers.fields.revenue, {
      id: 'business.customer.revenue',
      access: financial,
    }),
  },
});

Every object needs exactly one objectId() property, and from(...) fields must come from the object's membership source.


Attaching Policies to Objects

defineGraph requires an explicit policy for every object under policies:

// graph.ts
import { defineGraph } from 'relate';
import { access } from './access.js';
import { AccountReview, Customer, CustomerInvoices, Invoice } from './model.js';

export const graph = defineGraph({
  id: 'business.graph',
  objects: { Customer, Invoice, AccountReview },
  relationships: { CustomerInvoices },
  access,
  policies: {
    Customer: {
      read: {
        // Role gate: the caller must hold `employee`.
        gate: access.role('employee'),
        // Claim predicate: only customers in the caller's portfolio.
        where: { portfolio: { eq: access.claims.portfolio } },
        // Maximum age of the evidence used to evaluate `where`.
        evidenceMaxAgeMs: 30_000,
      },
      // Fields in the `financial` group also require `finance`.
      groups: { financial: access.role('finance') },
    },
    Invoice: {
      read: {
        gate: access.role('employee'),
        // Predicates can follow references to other objects.
        where: { customer: { portfolio: { eq: access.claims.portfolio } } },
        evidenceMaxAgeMs: 30_000,
      },
      groups: { financial: access.role('finance') },
    },
    // Relate-owned objects can also have a `create` rule.
    AccountReview: {
      read: {
        gate: access.role('employee'),
        where: { customer: { portfolio: { eq: access.claims.portfolio } } },
        evidenceMaxAgeMs: 30_000,
      },
      create: {
        gate: access.role('account-manager'),
        where: {
          customer: { portfolio: { eq: access.claims.portfolio } },
          author: { eq: access.actor.id },
        },
        evidenceMaxAgeMs: 30_000,
      },
    },
  },
});

For object and relationship declarations, see Graph Modeling. The complete portfolio-based model used here is in the runnable connected sources example, including native reviews, actions, and policies.


Policy Mechanics

1. Role Gates (gate)

A gate names a single role. If the caller does not hold that role, the object is denied. To let several kinds of user read an object, give those principals a shared role such as employee.

2. Claim Predicates (where)

Predicates compare property values with the caller's claims or actor ID:

// Inside a read or create rule:
where: { portfolio: { eq: access.claims.portfolio } },

They can also follow references:

// Inside a read or create rule:
where: { customer: { portfolio: { eq: access.claims.portfolio } } },

A rule is either a gate alone, or a gate with both where and evidenceMaxAgeMs; the two must be set together.

If a record fails the gate or the predicate, Relate behaves as if it does not exist. A direct read returns { status: 'not-found' }, and traversals leave the record out. A denied record is indistinguishable from a missing one.

Role gates also decide which traversals exist for a caller. A traversal whose start or destination type the caller cannot read, or whose link field is in a field group the caller lacks, is not listed by discovery. Calling it throws the same unknown-traversal ReadError as a misspelled traversal name, so the caller cannot tell a hidden traversal from a missing one.

3. Field Groups (groups)

When a caller can read an object but lacks the role for one of its field groups, the read still succeeds. Fields in that group are left out of data, and their evidence reports them as forbidden:

const result = await objects.Customer.get(id, {
  select: ['name', 'revenue'],
});
// For an `employee` without `finance`:
// result.data          → { name: 'Mothlight' }
// result.meta.fields   → { revenue: { status: 'forbidden' } } (compact default)
// result.meta.completeness → 'partial'

The caller learns only that it lacks access to the field, not why, and you don't need separate DTOs for each role. The ordinary group has no gate beyond the object's read rule.

4. Evidence Age (evidenceMaxAgeMs)

Policy decisions can depend on source data that changes, such as a customer moving to another portfolio. If the stored observation used to evaluate where is older than evidenceMaxAgeMs, Relate fetches the record from its source again before deciding.

This limits the age of permission evidence only. The age of returned data is controlled separately by the read's maxAgeMs option (default 60 seconds); see Querying & Traversal.

5. Create Rules (create)

create rules apply only to objects with nativeMembership(), which Relate owns and actions write. They are checked against the values an action creates, so the AccountReview rule above only accepts reviews for customers in the caller's portfolio, authored by the caller. Source-backed objects such as Customer cannot have a create rule. See Actions, Mutations & Receipts.


Development Principals

In production your host authenticates each caller and supplies their roles and claims. During development you can declare a few named identities in the graph so relate dev can run the graph as them:

export const graph = defineGraph({
  id: 'business',
  objects,
  relationships,
  access,
  policies,
  principals: {
    alice: { roles: ['employee'], claims: { portfolio: 'north' } },
    auditor: { roles: ['employee', 'finance'], claims: { portfolio: 'south' } },
  },
});
  • Roles must come from access.roles and each claim value must pass its defineAccess schema. An unknown role (principal.unknown-role), an unknown claim (principal.unknown-claim) or a value the schema rejects (principal.invalid-claim) is a compile error, reported like any other model issue. Names may use letters, digits, ., _ and -.

  • compile(graph) returns them as model.principals, outside the Manifest:

    const model = compile(graph);
    
    model.principals.alice; // { roles: ['employee'], claims: { portfolio: 'north' } }
  • They are not part of definitionRevision. Adding, removing or editing a development principal never changes the revision, so it never requires a migration of a persistent store.

  • At run time a principal is { id: <name>, roles, claims }. Outside relate dev nothing reads principals; your host still authenticates real callers. Do not treat these entries as accounts or credentials.

The Inspector lets you choose which principal to act as; the first declared one is the default.

On this page