FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

For more details, review the migration guide. Contact support@fauna.com with any questions.

Call a function

Creating and updating a function requires:

Problem

You need to execute a function within the current database.

Solution

Use the Call function:

client.query(
  q.Call(
    q.Function('create_post'),
    [ 'First Post Title', 'This is my first blog post!' ],
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  ref: Ref(Collection("posts"), "300221794335326720"),
  ts: 1622572664480000,
  data: { title: 'First Post Title', body: 'This is my first blog post!' }
}
result = client.query(
  q.call(
    q.function("create_post"),
    ["First Post Title", "This is my first blog post!"]
  )
)
print(result)
{'ref': Ref(id=300221798539067904, collection=Ref(id=posts, collection=Ref(id=collections))), 'ts': 1622572668490000, 'data': {'title': 'First Post Title', 'body': 'This is my first blog post!'}}
result, err := client.Query(
	f.Call(
		f.Function("create_post"),
		f.Arr{"First Post Title", "This is my first blog post!"},
	))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:map[body:This is my first blog post! title:First Post Title] ref:{300221752288477696 0xc00009b8f0 0xc00009b8f0 <nil>} ts:1622572624380000]
try
{
    Value result = await client.Query(
        Call(
            Function("create_post"),
            Arr("First Post Title", "This is my first blog post!")
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "300221738176741888", collection = RefV(id = "posts", collection = RefV(id = "collections"))),ts: LongV(1622572610930000),data: ObjectV(title: StringV(First Post Title),body: StringV(This is my first blog post!)))
Call(
  Function('create_post'),
  [ 'First Post Title', 'This is my first blog post!' ],
)
{
  ref: Ref(Collection("posts"), "302044131605086720"),
  ts: 1624310580730000,
  data: { title: 'First Post Title', body: 'This is my first blog post!' }
}
Query metrics:
  •    bytesIn:   98

  •   bytesOut:  232

  • computeOps:    1

  •    readOps:    0

  •   writeOps:    1

  •  readBytes:    0

  • writeBytes:  250

  •  queryTime: 21ms

  •    retries:    0

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!