Author: Lewis King
Date: December 19, 2019
Originally posted on the Fauna blog.
We’re pleased to announce the release of user-defined function (UDF) support in the FaunaDB Console! While UDFs have been available in FQL for some time, they can now be easily managed using the Console GUI.
Generally speaking, a UDF is comparable to a stored procedure or server-side function that has its parameters set by a user and often is seen as a “programming shortcut”. In FaunaDB, UDFs are FQL Lambda functions (which can be anonymous), and they can be exposed through the GraphQL API, by using the @resolver directive on fields in the Query and Mutation types. You can learn more about FaunaDB GraphQL UDFs in our documentation.
Creating UDFs in the Console is quite simple. From the homepage, click on one of your Databases. In the left-hand navigation bar, click on the “Functions” button and then click the green “New Function button”:
You’ll then see the New Function screen. For this example, let’s create a function that simply doubles the input for a query (more advanced examples are given here). This query adds the variable “x” to itself, thereby doubling it.
Let’s call the function “double”, and specify the function body as:
Query( Lambda('x', Add(Var('x'), Var('x'))) )
Once saved, you should see the function listed in the Functions list:
Note: you can use the Role field to apply a role’s privileges to a UDF, which is how you specify permissions in FaunaDB.
You can create more functions at your whim, and delete functions when needed:
Using your new function is straightforward as well. Simply wrap the function name in quotes in a
And that’s about it! When it comes to UDFs, the only limit is your creativity 😉
With our latest cloud release, users now have the ability to create and manage UDFs for a database directly in the FaunaDB Console.