> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Functions

> Queries and mutations: their spec keys, the frame, and how one function calls another.

A function is a [query](https://docs.bijection.com/functions/queries.md) or a [mutation](https://docs.bijection.com/functions/mutations.md).
A query reads and commits nothing, so a client can watch it. A mutation runs
as one transaction under one label and commits all of its writes, submits and
grave acts together, or none of them.

Both take a spec and a handler. A function with no spec keys takes the
handler alone.

```ts bijection/notes.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, query, role, t, table } from "@bijectionhq/bijection";

export const notes = table({ text: t.string, written: t.instant }, { index: [["written"]] });

export const list = query({ allow: [role.anyone] }, (ctx) =>
  ctx.list(notes.by("written").descending().first(20)),
);

export const add = mutation(
  { allow: [role.anyone], args: { text: t.string }, refuses: { NoteText: null } },
  async (ctx, { text }) => {
    if (!text.trim()) return ctx.refuse("NoteText", "Enter a note.");
    return await ctx.insert(notes, { text, written: ctx.frame.now.entry });
  },
);
```

A function has no name of its own. Its name is its path in the
[app tree](https://docs.bijection.com/mountable-apps): placed as `app({ notes })`, these functions are
`notes.list` and `notes.add`.

## Spec keys

| Key | Meaning |
| - | - |
| `allow` | The roles that may enter the function from outside. Without it the function is internal. `allow: []` is refused: leave the key out instead. |
| `under` | The label the function runs under: a query answers under it and a mutation commits under it. A [family](https://docs.bijection.com/database/family-tables.md) takes the member from the caller's frame. Without it the function runs at `label.public`. |
| `args` | The argument value types, as an object of `t.*` types. |
| `returns` | The value type of the answer. Without it the type is inferred from the handler. |
| `refuses` | The app codes the function may refuse with: PascalCase, and never equal to a [host code](https://docs.bijection.com/functions/errors.md#host-codes). |
| `on` | A mutation only: the standing source it [reacts to](https://docs.bijection.com/functions/reactions.md). |
| `roles` | A reaction only: the roles the installation holds when it runs. |

`allow` decides who may call a function. Labels decide what it may read and
write. The two are separate: an entry `allow`ed to `role.anyone` that runs
`under` a label still needs a frame that clears that label. `role.anyone` and
`role.signedIn` name principal classes: the program issues each class a grant
at install that enters the functions listing it, and the lock prints that
grant. A class grant clears only the public label, so a non-public entry open
only to a class refuses `Disallowed` with reason `NotCleared` when it lacks
clearance. Give its callers a role that
[clears the label](https://docs.bijection.com/access/grants.md) instead.

## The frame

`ctx.frame` is the host's read-only record of this invocation.

| Field | Meaning |
| - | - |
| `as` | Who acts: the verified `Principal` (`id`, `kind`, `email`, `phone`). Each contact is `null` unless verified. `as.kind` is `"anonymous"` for a signed-out caller, and `as.id` its own `anonymous:<token>`. |
| `grants` | The grants that admitted this invocation: at each position (`credential`, `actor`, `code`), the live grants that survived every rule, each with its `id`, `role` and role `chain`. Test a role with `ctx.frame.grants.has(clerk)`; iterating yields the actor's survivors. |
| `under` | The label the function runs under. |
| `in` | The world: `null` at the head, else a branch or a retained version. |
| `at` | The named valid instant, or `null` for the present. |
| `on` | `{ kind: "source", source }` for a reaction, `{ kind: "task", task }` for a performer or continuation, `{ kind: "landing", branch }` during replay, or `null` for a direct call. |
| `now` | The admitted frame clock, a `Now` value. |
| `within` | The transaction's deadline: the span after which it is `Expired`. It commits by `now + within`. |
| `version` | The version this transaction reads. |
| `caller` | The function whose `ctx.call` entered this one, or `null`. Compare it by identity and use `pathOf(fn)` for display. |

Read time from `ctx.frame.now.entry`. Durations and instants are `Temporal` values
everywhere in the SDK. See [Frames and trials](https://docs.bijection.com/clients/frames.md) for world
selection and valid-time reads.

## Call another function

`ctx.call(fn, args)` enters another function inside this transaction. A query
calls queries. A mutation calls queries and mutations, and a called mutation
joins the transaction. The callee needs no `allow`: internal functions are
reached this way.

```ts bijection/notes.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const rename = mutation({ args: { note: t.key(notes), text: t.string } }, async (ctx, { note, text }) => {
  await ctx.patch(notes(note), { text });
});

export const tidy = mutation({ allow: [role.signedIn], args: { note: t.key(notes) } }, async (ctx, { note }) => {
  const fact = await ctx.get(notes(note));
  if (fact !== null) await ctx.call(rename, { note, text: fact.text.trim() });
});
```

A callee's refusal refuses the caller's transaction, and a callee's throw
refuses it `Failed`. `ctx.try(fn, args)` answers a [`Result`](https://docs.bijection.com/functions/errors)
instead: a callee that refused or threw leaves no writes, submits or acts,
and the caller goes on. No refusal is sticky; a write guard, judged at
commit, is the one `ctx.try` cannot catch
([details](https://docs.bijection.com/functions/errors.md#call-a-function-and-go-on)). A query callee
also takes `{ in, at }` to read another world or valid instant.

Plain helpers need no function boundary. A helper that uses the context takes
`ctx` first and uses its verbs. Type a helper that only reads `Base`, which
every function's context is, a query's or a mutation's; type one that writes
`MutationCtx`. Pass the caller's `ctx` as it is, without a cast. The only
type argument is the codes the helper refuses with: `Base<"Missing">` lets
it refuse `Missing`. A context does not depend on the function's `under`:
`bijection check` checks the helper's reads and writes under the `under` of
each function that hands it its context.

## Entry points

| Entry | What it holds |
| - | - |
| `@bijectionhq/bijection` | Declarations (`app`, `table`, `series`, `timeline`, `label`, `role`, `release`, `forbid`, `secret`, `target`, `query`, `mutation`), target execution and sources (`http`, `every`, `cron`, `route`), receivers, `sys.files`, `members`, `t`, `sys`, `act`, `Result` and the types an app or library names |
| `@bijectionhq/bijection/client` | `connect` and the client types |
| `@bijectionhq/bijection/react` | `<Bijection>`, `useWatch`, `usePages`, `useMutation` |
| `@bijectionhq/bijection/test` | `kernel(app, { now })` |

## Next

* [Effects](https://docs.bijection.com/functions/effects.md): targets, tasks and continuations.
* [Reactions](https://docs.bijection.com/functions/reactions.md): `every`, `cron`, `route.post` and change sources.
* [Grave acts](https://docs.bijection.com/functions/grave-acts.md): the acts `bijection check` lists by name.
* [Results and refusals](https://docs.bijection.com/functions/errors.md): `Result` and the host codes.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.