Skip to content

Sample code from the "User-defined functions in Fauna" episode of Learn With Jason on 25 May 2021.

License

Notifications You must be signed in to change notification settings

fauna-labs/learn-with-jason

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.


Learn With Jason

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).

Using this repository

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.

Order of progress

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.

  1. 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.

  2. Limited sum - Double the parameters but far more functionality! Demonstrates multiple parameters, branch with If, and throwing errors with Abort.

  3. 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!

  4. Reduced primitives - Refines the gated-primitive function to return results and handle errors.

GraphQL

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.

Next steps

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

About

Sample code from the "User-defined functions in Fauna" episode of Learn With Jason on 25 May 2021.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published