This repository contains unofficial patterns, sample code, or tools to help developers build more effectively with Fauna. All Fauna Labs repositories are provided “as-is” and without support. By using this repository or its contents, you agree that this repository may never be officially supported and moved to the Fauna organization.
This repository contains the sample code from the 25 May 2021 episode of Learn With Jason, "User-defined functions in Fauna." The code provides increasingly complex and realistic examples of user-defined functions (UDFs) implemented in the Fauna Query Language (FQL).
You can deploy the functions in this repository into your own account using the Fauna dashboard. These instructions assume you have already created a database.
Note: Be sure to check Pre-populate with demo data when creating your database!
Open the Fauna dashboard and select the Shell tab. Copy the code of the function you wish to deploy, paste it into the code editor, and choose the Run Query button. Fauna runs the CreateFunction statement and adds the function to your database. You can confirm this by selecting the Functions tab.
To run your function, copy the sample function call from the function definition file, return to the Shell tab, and paste the function call into the code editor. Be sure to remove any leading slashes //
, then choose the Run Query button. Fauna invokes your function and displays the results.
To learn more about UDFs in Fauna, visit the sample functions in the following order. Each file contains the function definition and samples for invoking the function in the Fauna dashboard.
-
Hello world - The classic "Hello, world!" function implemented in FQL! Start here to learn the syntax for defining and invoking UDFs. Accepts a single variable, concatenates some strings, and returns the result.
-
Limited sum - Double the parameters but far more functionality! Demonstrates multiple parameters, branch with
If
, and throwing errors withAbort
. -
Gated primitives - Demonstrates wrapping FQL functionality, which is useful for restricting privileges, performing attribute-based authentication control (ABAC), unit testing your UDFs, and injecting failures. Also introduces function roles, which allow the UDF to run with permissions different from the invoking user or resource.
WARNING: Do not use this function in production - it does not properly handle results!
-
Reduced primitives - Refines the
gated-primitive
function to return results and handle errors.
A simple GraphQL schema is provided showing how to reference UDFs as custom resolvers. Sample queries for the hello world and limited sum UDFs are also provided.
Learn more about FQL, including UDFs by following this series of tutorials.
Copyright Fauna, Inc. or its affiliates. All rights reserved. SPDX-License-Identifier: MIT-0