Globals

⚠️ Only available in EdgeDB 2.0 or later.

Schemas can contain scalar-typed global variables.


1. global current_user_id -> uuid;

These provide a useful mechanism for specifying session-level data that can be referenced in queries with the global keyword.


1. select User {
2. id,
3. posts: { title, content }
4. }
5. filter .id = global current_user_id;

As in the example above, this is particularly useful for representing the notion of a session or “current user”.

Setting global variables

Global variables are set when initializing a client. The exact API depends on which client library you’re using.

TypeScript

Python

Go


1. import createClient from 'edgedb';

3. const baseClient = createClient()
4. const clientWithGlobals = baseClient.withGlobals({
5. current_user_id: '2141a5b4-5634-4ccc-b835-437863534c51',
6. });

8. await clientWithGlobals.query(`select global current_user_id;`);


1. from edgedb import create_client

3. client = create_client().with_globals({
4. 'current_user_id': '580cc652-8ab8-4a20-8db9-4c79a4b1fd81'
5. })

7. result = client.query("""
8. select global current_user_id;
9. """)
10. print(result)


1. package main

3. import (
4. "context"
5. "fmt"
6. "log"

8. "github.com/edgedb/edgedb-go"
9. )

11. func main() {
12. ctx := context.Background()
13. client, err := edgedb.CreateClient(ctx, edgedb.Options{})
14. if err != nil {
15. log.Fatal(err)
16. }
17. defer client.Close()

19. id, err := edgedb.ParseUUID("2141a5b4-5634-4ccc-b835-437863534c51")
20. if err != nil {
21. log.Fatal(err)
22. }

24. var result edgedb.UUID
25. err = client.
26. WithGlobals(map[string]interface{}{"current_user": id}).
27. QuerySingle(ctx, "SELECT global current_user;", &result)
28. if err != nil {
29. log.Fatal(err)
30. }

32. fmt.Println(result)
33. }

The .withGlobals/.with_globals method returns a new Client instance that stores the provided globals and sends them along with all future queries.

Cardinality

Global variables can be marked required; in this case, you must specify a default value.


1. required global one_string -> str {
2. default := "Hi Mom!"
3. };

Computed globals

Global variables can also be computed. The value of computed globals are dynamically computed when they are referenced in queries.


1. required global random_global := datetime_of_transaction();

The provided expression will be computed at the start of each query in which the global is referenced. There’s no need to provide an explicit type; the type is inferred from the computed expression.

Computed globals are not subject to the same constraints as non-computed ones; specifically, they can be object-typed and have a multi cardinality.


1. global current_user_id -> uuid;

3. # object-typed global
4. global current_user := (
5. select User filter .id = global current_user_id
6. );

8. # multi global
9. global current_user_friends := (global current_user).friends;

Usage in schema

Unlike query parameters, globals can be referenced inside your schema declarations.


1. type User {
2. property name -> str;
3. property is_self := (.id = global current_user_id)
4. };

This is particularly useful when declaring access policies.


1. type Person {
2. required property name -> str;
3. access policy my_policy allow all using (.id = global current_user_id);
4. }

Refer to Access Policies for complete documentation.

See also
SDL > Globals
DDL > Globals