Compose apps
Split behavior across Apps by giving each one a canonical name and importing it where it is needed. Use this guide when two Apps share data or when one App reacts to another’s Events.
Before you start
Section titled “Before you start”- Two App specs, each an
AppSpecfunction, as in Your first application. - A Template in the imported App that the importer relates to or listens on.
-
Name the imported App. In its spec, call
app.configure(key)before any other declaration. The key is the App’s canonical name: it appears in every PRN the App owns, such asprn::people:template/person, and it is the key underctx.appsat run time. Declare the Templates, Roles and Events the importer needs on this App.examples/compose-apps/people.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';export const peopleApp: AppSpec = (app) => {app.configure('people');app.addRole('admin', { label: 'People administrator' });const person = app.addTemplate('person');person.addProperty('name', 'string');person.addProperty('email', 'string');person.addEvent('joined');}; -
Import it with
addDependency(key, spec). In the root App, the first argument is the dependency key, a name local to the importer; the second is the imported spec. The return value is aDependencyAppBuilderexposing the imported App’stemplates,roles,events,unionsandsearches. Importing the same spec from several Apps yields one shared App in the Definition, never a copy per importer.examples/compose-apps/onboarding.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';import { peopleApp } from './people';interface JoinInput {readonly email: string;readonly name: string;}interface PersonInput {readonly personId: string;}export const onboardApp: AppSpec = (app) => {app.configure('onboarding');const people = app.addDependency('directory', peopleApp);const checklist = app.addTemplate('checklist');checklist.addProperty('title', 'string');checklist.addProperty('complete', 'boolean');checklist.addRelation('owner', 'many-to-one', people.templates.person, { toKey: 'checklists' });people.templates.person.events.joined.addListener('createChecklist', async (_event, person, _current, ctx) => {const name = person.properties.name;const created = await ctx.apps.onboarding.templates.checklist.createItem(person, {properties: { complete: false, title: typeof name === 'string' ? `Welcome ${name}` : 'Welcome' },});const owner = created.relations.owner;if (owner.type === 'one') await owner.set(person);});const join = app.addMutation('join', async (input: JoinInput, _current, ctx) => {const workspace = await ctx.getWorkspaceItem();const person = await ctx.apps.people.templates.person.createItem(workspace, {properties: { email: input.email, name: input.name },});await person.events.joined.publish({});return person;});join.input.addProperty('name', 'string').mandatory();join.input.addProperty('email', 'string').mandatory();join.output.item(people.templates.person).mandatory();join.addGrant(people.roles.admin);const checklistFor = app.addQuery('checklistFor', async (input: PersonInput, _current, ctx) => {const person = await ctx.apps.people.templates.person.getItem(input.personId);const checklists = person?.relations.checklists;if (checklists === undefined || checklists.type !== 'many') return undefined;return (await checklists.select().first())?.underlyingItem;});checklistFor.input.addProperty('personId', 'id').mandatory();checklistFor.output.item(checklist);}; -
Relate to an imported Template. Pass
people.templates.personwherever a Template is expected: as the target ofaddRelation, as a Union member, or as an operation output. The Relation belongs to the importer, and the imported Template gains the reverse side undertoKey.examples/compose-apps/onboarding.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';import { peopleApp } from './people';interface JoinInput {readonly email: string;readonly name: string;}interface PersonInput {readonly personId: string;}export const onboardApp: AppSpec = (app) => {app.configure('onboarding');const people = app.addDependency('directory', peopleApp);const checklist = app.addTemplate('checklist');checklist.addProperty('title', 'string');checklist.addProperty('complete', 'boolean');checklist.addRelation('owner', 'many-to-one', people.templates.person, { toKey: 'checklists' });people.templates.person.events.joined.addListener('createChecklist', async (_event, person, _current, ctx) => {const name = person.properties.name;const created = await ctx.apps.onboarding.templates.checklist.createItem(person, {properties: { complete: false, title: typeof name === 'string' ? `Welcome ${name}` : 'Welcome' },});const owner = created.relations.owner;if (owner.type === 'one') await owner.set(person);});const join = app.addMutation('join', async (input: JoinInput, _current, ctx) => {const workspace = await ctx.getWorkspaceItem();const person = await ctx.apps.people.templates.person.createItem(workspace, {properties: { email: input.email, name: input.name },});await person.events.joined.publish({});return person;});join.input.addProperty('name', 'string').mandatory();join.input.addProperty('email', 'string').mandatory();join.output.item(people.templates.person).mandatory();join.addGrant(people.roles.admin);const checklistFor = app.addQuery('checklistFor', async (input: PersonInput, _current, ctx) => {const person = await ctx.apps.people.templates.person.getItem(input.personId);const checklists = person?.relations.checklists;if (checklists === undefined || checklists.type !== 'many') return undefined;return (await checklists.select().first())?.underlyingItem;});checklistFor.input.addProperty('personId', 'id').mandatory();checklistFor.output.item(checklist);}; -
Listen on an imported Event. Call
addListener(key, handler)onpeople.templates.person.events.joined. The Listener receives the Event, the Item it was published against, the App that owns the Event, and a write context. Reach your own App throughctx.apps.onboardingto create Items or run Mutations from inside the Listener.examples/compose-apps/onboarding.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';import { peopleApp } from './people';interface JoinInput {readonly email: string;readonly name: string;}interface PersonInput {readonly personId: string;}export const onboardApp: AppSpec = (app) => {app.configure('onboarding');const people = app.addDependency('directory', peopleApp);const checklist = app.addTemplate('checklist');checklist.addProperty('title', 'string');checklist.addProperty('complete', 'boolean');checklist.addRelation('owner', 'many-to-one', people.templates.person, { toKey: 'checklists' });people.templates.person.events.joined.addListener('createChecklist', async (_event, person, _current, ctx) => {const name = person.properties.name;const created = await ctx.apps.onboarding.templates.checklist.createItem(person, {properties: { complete: false, title: typeof name === 'string' ? `Welcome ${name}` : 'Welcome' },});const owner = created.relations.owner;if (owner.type === 'one') await owner.set(person);});const join = app.addMutation('join', async (input: JoinInput, _current, ctx) => {const workspace = await ctx.getWorkspaceItem();const person = await ctx.apps.people.templates.person.createItem(workspace, {properties: { email: input.email, name: input.name },});await person.events.joined.publish({});return person;});join.input.addProperty('name', 'string').mandatory();join.input.addProperty('email', 'string').mandatory();join.output.item(people.templates.person).mandatory();join.addGrant(people.roles.admin);const checklistFor = app.addQuery('checklistFor', async (input: PersonInput, _current, ctx) => {const person = await ctx.apps.people.templates.person.getItem(input.personId);const checklists = person?.relations.checklists;if (checklists === undefined || checklists.type !== 'many') return undefined;return (await checklists.select().first())?.underlyingItem;});checklistFor.input.addProperty('personId', 'id').mandatory();checklistFor.output.item(checklist);}; -
Grant an imported Role. Pass
people.roles.admintoaddGranton an operation so principals holding that Role may call it. A Role keeps its owning App’s PRN, so the same Role secures operations in every App that imports it.examples/compose-apps/onboarding.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';import { peopleApp } from './people';interface JoinInput {readonly email: string;readonly name: string;}interface PersonInput {readonly personId: string;}export const onboardApp: AppSpec = (app) => {app.configure('onboarding');const people = app.addDependency('directory', peopleApp);const checklist = app.addTemplate('checklist');checklist.addProperty('title', 'string');checklist.addProperty('complete', 'boolean');checklist.addRelation('owner', 'many-to-one', people.templates.person, { toKey: 'checklists' });people.templates.person.events.joined.addListener('createChecklist', async (_event, person, _current, ctx) => {const name = person.properties.name;const created = await ctx.apps.onboarding.templates.checklist.createItem(person, {properties: { complete: false, title: typeof name === 'string' ? `Welcome ${name}` : 'Welcome' },});const owner = created.relations.owner;if (owner.type === 'one') await owner.set(person);});const join = app.addMutation('join', async (input: JoinInput, _current, ctx) => {const workspace = await ctx.getWorkspaceItem();const person = await ctx.apps.people.templates.person.createItem(workspace, {properties: { email: input.email, name: input.name },});await person.events.joined.publish({});return person;});join.input.addProperty('name', 'string').mandatory();join.input.addProperty('email', 'string').mandatory();join.output.item(people.templates.person).mandatory();join.addGrant(people.roles.admin);const checklistFor = app.addQuery('checklistFor', async (input: PersonInput, _current, ctx) => {const person = await ctx.apps.people.templates.person.getItem(input.personId);const checklists = person?.relations.checklists;if (checklists === undefined || checklists.type !== 'many') return undefined;return (await checklists.select().first())?.underlyingItem;});checklistFor.input.addProperty('personId', 'id').mandatory();checklistFor.output.item(checklist);}; -
Reach the imported App at run time through
ctx.apps.people. Handler code addresses Apps by canonical name, not by dependency key:ctx.apps.directorydoes not exist,ctx.apps.peopledoes. Itstemplates,unions,roles,queriesand, in a write execution,mutationsandeventsbehave exactly as oncurrent.examples/compose-apps/onboarding.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';import { peopleApp } from './people';interface JoinInput {readonly email: string;readonly name: string;}interface PersonInput {readonly personId: string;}export const onboardApp: AppSpec = (app) => {app.configure('onboarding');const people = app.addDependency('directory', peopleApp);const checklist = app.addTemplate('checklist');checklist.addProperty('title', 'string');checklist.addProperty('complete', 'boolean');checklist.addRelation('owner', 'many-to-one', people.templates.person, { toKey: 'checklists' });people.templates.person.events.joined.addListener('createChecklist', async (_event, person, _current, ctx) => {const name = person.properties.name;const created = await ctx.apps.onboarding.templates.checklist.createItem(person, {properties: { complete: false, title: typeof name === 'string' ? `Welcome ${name}` : 'Welcome' },});const owner = created.relations.owner;if (owner.type === 'one') await owner.set(person);});const join = app.addMutation('join', async (input: JoinInput, _current, ctx) => {const workspace = await ctx.getWorkspaceItem();const person = await ctx.apps.people.templates.person.createItem(workspace, {properties: { email: input.email, name: input.name },});await person.events.joined.publish({});return person;});join.input.addProperty('name', 'string').mandatory();join.input.addProperty('email', 'string').mandatory();join.output.item(people.templates.person).mandatory();join.addGrant(people.roles.admin);const checklistFor = app.addQuery('checklistFor', async (input: PersonInput, _current, ctx) => {const person = await ctx.apps.people.templates.person.getItem(input.personId);const checklists = person?.relations.checklists;if (checklists === undefined || checklists.type !== 'many') return undefined;return (await checklists.select().first())?.underlyingItem;});checklistFor.input.addProperty('personId', 'id').mandatory();checklistFor.output.item(checklist);};
Verify
Section titled “Verify”The test below joins a person through the root App’s Mutation, confirms the Item carries the imported App’s PRN, drives the reactor, and reads the checklist the Listener created.
import { createHarnessInstance } from '@pyn-backend/platform-harness-public';import { createTestPlatformService } from '@pyn-backend/platform-service-test';import { createTestCronService } from '@pyn-backend/service-cron-test';import { createTestSystemService } from '@pyn-backend/service-system-test';import { createTestLoggerService } from '@pyn-backend/test-services-logger';import { describe, expect, it } from 'vitest';
import { onboardApp } from './onboarding';
describe('compose apps', () => { it('reaches the imported App by canonical name and reacts to its Event', async () => { const system = createTestSystemService(); const logger = createTestLoggerService(); const cron = createTestCronService({ system }); const platform = createTestPlatformService({ cron, logger, system }); const harness = await createHarnessInstance({ cron, logger, platform, system }, onboardApp); const workspace = await harness.createWorkspace();
// The harness runs every execution as system, so the Grant on `join` is not exercised here. const personId = await workspace.write(async (ctx) => { const current = ctx.apps.onboarding; const person = await current.mutations.join.handler({ email: 'grace@example.com', name: 'Grace Hopper' }, current, ctx); expect(person.templatePrn).toBe('prn::people:template/person'); return person.id; });
await workspace.read(async (ctx) => { const current = ctx.apps.onboarding; expect(await current.queries.checklistFor.handler({ personId }, current, ctx)).toBeUndefined(); });
await harness.tickAll();
await workspace.read(async (ctx) => { const current = ctx.apps.onboarding; const checklist = await current.queries.checklistFor.handler({ personId }, current, ctx); expect(checklist?.templatePrn).toBe('prn::onboarding:template/checklist'); expect(checklist?.parentId).toBe(personId); expect(checklist?.properties.title).toBe('Welcome Grace Hopper'); }); expect.assertions(5); });});From the repository root, run:
pnpm run cli test alpynehq/alpyne/website/examples/compose-apps/You should see:
Test Files 1 passed (1) Tests 1 passed (1)The checklist is absent before harness.tickAll() and present after it, because publish appends the Event and the reactor runs the Listener after the write commits. The harness runs as system, so the Grant on join is declared but not exercised by this test.
Options
Section titled “Options”app.addDependencyreturns the existing builder when the same dependency key is added twice, so a spec may import a shared App from a helper without guarding against repeats.- Every Definition has one root App: the spec passed to the harness or to the Runtime. An App configured with the same canonical name as an existing import resolves to that import.
setDescription(text)afterconfiguredescribes the App in generated assets.
Related
Section titled “Related”- Application model for root Apps, imported Apps, PRNs and generated types.
- Apps and Definitions for the rules on canonical names and dependency resolution.
- Handle events for Listener contexts,
publishversusimmediate, and testing withtickAll. - Grant access for assigning the imported Role to a principal.
AppBuilder.addDependencyandAppBuilder.configurefor the two calls this guide relies on.

