Mutation names
Mutations follow the same naming rules as queries, see Query names. Queries and mutations can be defined in the same file when using named exports.The mutation constructor
To declare a mutation in Bijection use the mutation constructor function. Pass it
an object with a handler function, which performs the mutation:
Mutation arguments
Just like queries, mutations accept named arguments, and the argument values are accessible as fields of the second parameter of thehandler function:
args
object using v validators:
Mutation responses
Queries can return values of any supported Bijection type which will be automatically serialized and deserialized. Mutations can also returnundefined, which is not a valid Bijection value. When a
mutation returns undefined it is translated to null on the client.
Mutation context
Themutation constructor enables writing data to the database, and other
Bijection features by passing a
MutationCtx object to the handler
function as the first parameter:
-
To read from and write to the database use the
dbfield. Note that we make the handler function anasyncfunction so we canawaitthe promise returned bydb.insert():Read on about Writing Data. -
To generate upload URLs for storing files use the
storagefield. Read on about File Storage. -
To check user authentication use the
authfield. Read on about Authentication. -
To schedule functions to run in the future, use the
schedulerfield. Read on about Scheduled Functions.
Splitting up mutation code via helpers
When you want to split up the code in your mutation or reuse logic across multiple Bijection functions you can define and call helper functions:export helpers to use them across multiple files. They will not be
callable from outside of your Bijection functions.
See
Type annotating server side helpers
for more guidance on TypeScript types.
Using NPM packages
Mutations can import NPM packages installed innode_modules. Not all NPM
packages are supported, see
Runtimes for more details.
Calling mutations from clients
To call a mutation from React use theuseMutation hook along with the
generated api object.
Transactions
Mutations run transactionally. This means that:- All database reads inside the transaction get a consistent view of the data in the database. You don’t have to worry about a concurrent update changing the data in the middle of the execution.
- All database writes get committed together. If the mutation writes some data to the database, but later throws an error, no data is actually written to the database.
Limits
Mutations have a limit to the amount of data they can read and write at once to guarantee good performance. Learn more in Read/write limit errors. See Write performance and limits for tools to stay within these limits: measuring document sizes, checking headroom viactx.meta.getTransactionMetrics(), and bounding nested calls
(ctx.runQuery, ctx.runMutation) with a transactionLimits option.
For information on other limits, see Limits.