Function()

Signature

Function(function: String) => Any

Description

Calls a UDF by its name. You can pass arguments to the UDF. See Examples.

Errors

If you attempt to call a UDF that doesn’t exist, Fauna returns a query runtime error with an invalid_argument error code and a 400 HTTP status code:

invalid_argument

error: invalid argument `function`: No such user function `Foo`.
at *query*:1:9
  |
1 | Function("Foo")
  |         ^^^^^^^
  |

Comparison to <functionName>

Calling Function() is similar to accessing <functionName> directly, except Function() returns an Any value.

This difference only affects static typing, not runtime behavior.

In most cases, you should use <functionName>. However, Function() is useful if you need to iterate through a list of UDF calls.

Parameters

Parameter Type Required Description

function

String

true

Function to call

Return value

Type Description

Any

Results of the function call.

Examples

Get the function by passing the function name to Function():

// Call function named `getOrCreateCart`
Function("getOrCreateCart")
"[function getOrCreateCart]"

 

To call the function, pass arguments in parentheses:

// Calls the `getOrCreateCart()` function on
// a `Customer` document with an `id` of `111`.
Function("getOrCreateCart")('111')
{
  id: "412998994633949261",
  coll: Order,
  ts: Time("2024-10-28T14:23:03.966Z"),
  items: "hdW...",
  total: 5392,
  status: "cart",
  customer: Customer("111"),
  createdAt: Time("2024-10-28T14:23:03.795981Z"),
  payment: {}
}
\