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';
  2. const baseClient = createClient()
  3. const clientWithGlobals = baseClient.withGlobals({
  4. current_user_id: '2141a5b4-5634-4ccc-b835-437863534c51',
  5. });
  6. await clientWithGlobals.query(`select global current_user_id;`);
  1. from edgedb import create_client
  2. client = create_client().with_globals({
  3. 'current_user_id': '580cc652-8ab8-4a20-8db9-4c79a4b1fd81'
  4. })
  5. result = client.query("""
  6. select global current_user_id;
  7. """)
  8. print(result)
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "github.com/edgedb/edgedb-go"
  7. )
  8. func main() {
  9. ctx := context.Background()
  10. client, err := edgedb.CreateClient(ctx, edgedb.Options{})
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. defer client.Close()
  15. id, err := edgedb.ParseUUID("2141a5b4-5634-4ccc-b835-437863534c51")
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. var result edgedb.UUID
  20. err = client.
  21. WithGlobals(map[string]interface{}{"current_user": id}).
  22. QuerySingle(ctx, "SELECT global current_user;", &result)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. fmt.Println(result)
  27. }

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;
  2. # object-typed global
  3. global current_user := (
  4. select User filter .id = global current_user_id
  5. );
  6. # multi global
  7. 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