Mutations and Queries
An App declares an operation for each thing a caller can ask of it: a Mutation to write, and a Query to read. An operation has a key, a handler, an input and an output. A caller runs it through its handler, and so does another handler of the App.
Builder calls
Section titled “Builder calls”addMutation
Section titled “addMutation”app.addMutation(key: string, handler: MutationHandler): MutationBuilderapp.addMutation( key: string, options: MutationOptions, handler: MutationHandler,): MutationBuilderhandler( input: any, current: WriteAppInstance, ctx: WriteRuntimeInstance,): Promise<any>mutation.input: InputBuildermutation.output: OutputBuilderDeclares a Mutation: an operation that writes. The handler gets three arguments. input is what the caller sent, as mutation.input declares it. current is the App that declares the Mutation, and ctx is the context of the write execution the call runs in. What the handler returns goes to the caller, as mutation.output declares it.
| Argument | Type | Description |
|---|---|---|
key |
string |
A lowercase letter, then letters and digits, 64 characters at most. An App uses a Mutation key once. |
handler |
MutationHandler |
The function the call runs: the third signature above. |
options |
MutationOptions |
The short form: the input and the output, as the options below. |
| Option | Description |
|---|---|
input |
The values of the input, each under its key, by the name of a data type: 'boolean', 'id', 'integer', 'number' or 'string'. 'id!' and 'string!' declare a mandatory value. |
output |
One of those five names for a value, a Template for an Item of it, or a list of Templates for an Item of any of them. |
Returns the Mutation. With options, { input: { id: 'id!', title: 'string!' }, output: book } declares two mandatory values and an Item of book.
const request = app.addMutation('request', async (input: RequestInput, current, ctx) => { const workspace = await ctx.getWorkspaceItem(); return await workspace.createChild(current.templates.leaveRequest, { properties: { days: input.days, reason: input.reason }, }); }); request.input.addProperty('reason', 'string').mandatory(); request.input.addProperty('days', 'integer').mandatory(); request.output.item(leaveRequest).mandatory();Behavior
- What the handler writes is saved when the write execution of the call ends, and a later execution reads it.
- When the handler throws, the call throws that error. A write execution that ends with it saves nothing the Mutation wrote.
See: Queries and Mutations, Add queries and mutations, Tutorial: the lifecycle, AppBuilder.addMutation.
addQuery
Section titled “addQuery”app.addQuery(key: string, handler: QueryHandler): QueryBuilderapp.addQuery( key: string, options: QueryOptions, handler: QueryHandler,): QueryBuilderhandler( input: any, current: ReadAppInstance, ctx: ReadRuntimeInstance,): Promise<any>query.input: InputBuilderquery.output: OutputBuilderDeclares a Query: an operation that reads. Its handler gets the three arguments a Mutation gets, of types that leave out what writes. current is a ReadAppInstance, which has queries and no mutations. ctx is a ReadRuntimeInstance. An Item that either gives is a ReadItem, which has no updateProperties and no createChild.
| Argument | Type | Description |
|---|---|---|
key |
string |
A key as for a Mutation. An App uses a Query key once. |
handler |
QueryHandler |
The function the call runs: the third signature above. |
options |
QueryOptions |
The short form, with the options of addMutation. |
Returns the Query. The sample declares no input, and its output is a list of Items.
const list = app.addQuery('list', async (_input: unknown, current) => { const requests = current.templates.leaveRequest.selectItems(); const page = await requests.sort(['$createdAt asc']).execute(); return page.results; }); list.output.item(leaveRequest).array();Behavior
- A Query calls the Queries of its App through
current. - In a read execution a Query cannot write, whatever its handler calls: a write throws.
See: Queries and Mutations, Add queries and mutations, Tutorial: the lifecycle, AppBuilder.addQuery.
input.addProperty
Section titled “input.addProperty”input.addProperty( key: string, type: PrimitiveDataType | TemporalDataType,): DataTypeInputBuildervalue.mandatory(value?: boolean): DataTypeInputBuilderDeclares a value of the input of an operation, under a key, of one data type. input is mutation.input or query.input. The handler reads the value under that key of its own input.
| Argument | Type | Description |
|---|---|---|
key |
string |
The key of the value in the input. |
type |
PrimitiveDataType | TemporalDataType |
A data type of the table below, by its name. |
| Data type | Holds | A caller sends, and the handler reads |
|---|---|---|
string |
Text. | string |
integer |
A whole number. | number |
number |
A number, whole or not. | number |
boolean |
True or false. | boolean |
id |
An Identifier, as of an Item. | string |
date |
A day of the calendar. | string, as '2020-02-03' |
duration |
A length of time in ISO 8601. | string, as 'P14D' |
Returns the value, which is optional until its modifier is called.
| Modifier | What it declares |
|---|---|
mandatory |
The caller must send the value. Pass false to make the value optional again. Returns the value. |
lend.input.addProperty('id', 'id').mandatory(); lend.input.addProperty('borrower', 'string').mandatory(); lend.input.addProperty('copies', 'integer'); lend.input.addProperty('due', 'date'); lend.input.addProperty('fee', 'number'); lend.input.addProperty('length', 'duration'); lend.input.addProperty('renewable', 'boolean');Behavior
- The handler reads each value as the caller sent it, of the kind the table gives for its data type.
- A key the input does not declare does not reach the handler, and an optional value the caller does not send reads
undefined. - A mandatory value that is missing, or of the wrong kind for its data type, throws before the handler runs.
See: Queries and Mutations, Add queries and mutations, InputBuilder.addProperty.
input.addArray
Section titled “input.addArray”input.addArray(key: string): ArrayInputBuilderarray.property( type: PrimitiveDataType | TemporalDataType,): DataTypeInputBuilderarray.object(): ObjectInputBuilderarray.mandatory(value?: boolean): ArrayInputBuilderDeclares a list in the input of an operation, under a key. The list it returns declares what each element is. In the sample addAll takes a list of books, each an object with a title and a number of copies, and adds a book for each.
| Argument | Type | Description |
|---|---|---|
key |
string |
The key of the list in the input. |
| Modifier | What it declares |
|---|---|
property |
Each element is a value of the data type type, as for input.addProperty. Returns that value. |
object |
Each element is an object. Returns the object, which declares its members as input.addObject does. |
mandatory |
The caller must send the list. Returns the list. |
const addAll = app.addMutation('addAll', async (input: Books, current, ctx) => { for (const row of input.books) { await current.mutations.add.handler(row, current, ctx); } return input.books.length; }); const rows = addAll.input.addArray('books').mandatory().object(); rows.addProperty('title', 'string').mandatory(); rows.addProperty('copies', 'integer');Behavior
- The handler reads the list as the caller sent it, element by element.
- A mandatory list throws before the handler runs when the caller sends none, or sends a value that is not a list.
- A mandatory member that one element lacks throws before the handler runs, so nothing is saved for the elements before it.
See: Queries and Mutations, Add queries and mutations, InputBuilder.addArray.
input.addObject
Section titled “input.addObject”input.addObject(key: string): ObjectInputBuilderobject.addProperty( key: string, type: PrimitiveDataType | TemporalDataType,): DataTypeInputBuilderobject.mandatory(value?: boolean): ObjectInputBuilderDeclares an object in the input of an operation, under a key. The object it returns declares its members, each under a key of its own. The handler reads the object under its key, as input.place in the sample, where shelve saves the aisle and the shelf of a book.
| Argument | Type | Description |
|---|---|---|
key |
string |
The key of the object in the input. |
| Modifier | What it declares |
|---|---|
addProperty |
A member of the object: a value of one data type under a key, as for input.addProperty. Returns that value. |
mandatory |
The caller must send the object. Returns the object. |
const shelve = app.addMutation('shelve', async (input: ShelveInput, current) => { const item = await current.templates.book.getItem(input.id); if (item === undefined) throw new Error('Book not found'); return await item.updateProperties(input.place); }); shelve.input.addProperty('id', 'id').mandatory(); const place = shelve.input.addObject('place').mandatory(); place.addProperty('aisle', 'string').mandatory(); place.addProperty('shelf', 'integer'); shelve.output.item(book).mandatory();Behavior
- The handler reads each member the caller sent. An optional member that is missing reads
undefined. - A mandatory member that is missing throws before the handler runs, where the caller sends the object.
- A mandatory object throws when the caller sends none, or sends a value that is not an object.
See: Queries and Mutations, Add queries and mutations, InputBuilder.addObject.
output.item
Section titled “output.item”output.item( templateOrUnion: OutputBuilderItemType | OutputBuilderItemType[],): ItemOutputBuilderresult.array(value?: boolean): ItemOutputBuilderresult.cursor(): ItemOutputBuilderresult.page(): ItemOutputBuilderresult.mandatory(value?: boolean): ItemOutputBuilderDeclares that an operation returns an Item. output is mutation.output or query.output, and the last call made on it is the output of the operation.
| Argument | Type | Description |
|---|---|---|
templateOrUnion |
OutputBuilderItemType | OutputBuilderItemType[] |
The Template of the Item, or a Union. A list of them declares an Item of any one. |
Returns the Item output. Each modifier returns it too.
| Modifier | What it declares |
|---|---|
array |
The handler returns a list of Items. |
cursor |
The handler returns what execute() of a selector gives: the Items under results, and a cursor for the next page. |
page |
The handler returns what execute() gives after paginate: the Items under results, with page, pageCount, limit and totalCount. |
mandatory |
The handler always returns an Item. |
const recent = app.addQuery('recent', async (input: RecentInput, current) => { const books = current.templates.book.selectItems(); return await books.limit(2).cursor(input.cursor).execute(); }); recent.input.addProperty('cursor', 'string'); recent.output.item(book).cursor();Behavior
- The caller gets the Item the handler returns, where the output names its Template, a list that holds it, or a Union it is in.
- A
mandatoryItem of any other Template throws: the caller gets no Item. - An Item the handler creates through
currentandctxreaches the caller only where a Grant on its Template shows it to them. Without one the handler runs, and the caller getsundefined. - With
cursor, a call that sends the cursor back gets the Items after the ones it got. Withpage, the caller gets the page of that number.
See: Queries and Mutations, Add queries and mutations, Tutorial: who can do what, OutputBuilder.item.
output.value
Section titled “output.value”output.value(value: DataType): DataValueOutputBuilderresult.array(value?: boolean): DataValueOutputBuilderresult.mandatory(value?: boolean): DataValueOutputBuilderDeclares that an operation returns a value that is not an Item, such as a count or a title. The handler returns the value itself, or a list of values where the output declares one. In the sample first returns the title of the first book, and declares that it always returns one.
| Argument | Type | Description |
|---|---|---|
value |
DataType |
The data type of the value, by its name, as 'string' or 'integer'. |
Returns the value output. Each modifier returns it too.
| Modifier | What it declares |
|---|---|
array |
The handler returns a list of values. |
mandatory |
The handler always returns a value. |
const first = app.addQuery('first', async (_input: unknown, current) => { const books = current.templates.book.selectItems(); const found = await books.sort(['$createdAt asc']).first(); return found?.properties.title; }); first.output.value('string').mandatory();Behavior
- The caller gets the value the handler returns, or the list of values.
- A
mandatoryvalue throws when the handler returnsundefined.
See: Queries and Mutations, Add queries and mutations, OutputBuilder.value.
output.object, output.void
Section titled “output.object, output.void”output.object(): ObjectInputBuilderoutput.void(): OutputBuilderoutput.object declares that an operation returns an object. It returns the object that input.addObject returns, and addProperty on it declares a member. In the sample summary returns the number of books and the number of copies.
output.void declares that an operation returns no value, and returns the output. An operation with no call on its output declares the same.
const summary = app.addQuery('summary', async (_input: unknown, current, ctx) => { const books: unknown = await current.queries.count.handler({}, current, ctx); const found = await current.templates.book.selectItems().execute(); const copies = found.results.map((item) => Number(item.properties.copies)); return { books, copies: copies.reduce((total, next) => total + next, 0) }; }); const shape = summary.output.object().mandatory(); shape.addProperty('books', 'integer').mandatory(); shape.addProperty('copies', 'integer').mandatory();Behavior
- The caller gets the object the handler returns.
- A Mutation that declares no value saves what its handler writes.
See: Queries and Mutations, Add queries and mutations, OutputBuilder.object, OutputBuilder.void.
Calling an operation
Section titled “Calling an operation”handler
Section titled “handler”current.mutations: Record<string, MutationHandlerInstance>current.queries: Record<string, QueryHandlerInstance>mutation.handler: MutationHandlerquery.handler: QueryHandlerRuns an operation. An App holds its Mutations under mutations and its Queries under queries, by key. Call handler with the input, that App and ctx. The call returns the Item or the value that the output declares.
The sample reaches the App through ctx.apps, in a write execution that as runs as one person.
const weekend = await as(people.alice, async (ctx) => { const leave = ctx.apps.leave; const input = { days: 3, reason: 'Long weekend' }; return await leave.mutations.request.handler(input, leave, ctx);});Behavior
- The Runtime refuses a person who holds no Role with a Grant on the operation, before the input is read or the handler runs.
- An operation with no Grant refuses every person.
- A Mutation calls the Mutations of its App through
current, and what they write is saved with what it writes. - When one of them throws and the Mutation does not catch the error, its own call throws it, and nothing is saved.
See: Queries and Mutations, Add queries and mutations, Tutorial: who can do what, Run it as a server, MutationHandlerInstance.handler.

