Aliases​
You can think of aliases as a way to give schema names to arbitrary EdgeQL expressions. You can later refer to aliases in queries and in other aliases.
Aliases are functionally equivalent to expression aliases defined in EdgeQL statements in with block, but are available to all queries using the schema and can be introspected.
Like computed properties, the aliased expression is evaluated on the fly whenever the alias is referenced.
Scalar alias​
# in your schema:
alias digits := {0,1,2,3,4,5,6,7,8,9};
Later, in some query:
select count(digits);
Object type alias​
The name of a given object type (e.g. User
) is itself a pointer to the set
of all User objects. After declaring the alias below, you can use User
and
UserAlias
interchangeably:
alias UserAlias := User;
Object type alias with computeds​
Object type aliases can include a shape that declares additional computed properties or links:
type Post {
required title: str;
}
alias PostWithTrimmedTitle := Post {
trimmed_title := str_trim(.title)
}
Later, in some query:
select PostWithTrimmedTitle {
trimmed_title
};
Arbitrary expressions​
Aliases can correspond to any arbitrary EdgeQL expression, including entire queries.
# Tuple alias
alias Color := ("Purple", 128, 0, 128);
# Named tuple alias
alias GameInfo := (
name := "Li Europan Lingues",
country := "Iceland",
date_published := 2023,
creators := (
(name := "Bob Bobson", age := 20),
(name := "Trina Trinadóttir", age := 25),
),
);
type BlogPost {
required title: str;
required is_published: bool;
}
# Query alias
alias PublishedPosts := (
select BlogPost
filter .is_published = true
);
All aliases are reflected in the database's built-in GraphQL schema.
Defining aliases​
Syntax​
Define a new alias corresponding to the more explicit DDL commands.
alias alias-name := alias-expr ;
alias alias-name "{"
using alias-expr;
[ annotation-declarations ]
"}" ;
Where:
- alias-name
-
The name (optionally module-qualified) of an alias to be created.
- alias-expr
-
The aliased expression. Must be a Stable EdgeQL expression.
The valid SDL sub-declarations are listed below:
- annotation-declarations
-
Set alias annotation to a given value.
DDL commands​
This section describes the low-level DDL commands for creating and dropping aliases. You typically don't need to use these commands directly, but knowing about them is useful for reviewing migrations.
Create alias​
Define a new alias in the schema.
[ with with-item [, ...] ]
create alias alias-name := alias-expr ;
[ with with-item [, ...] ]
create alias alias-name "{"
using alias-expr;
[ create annotation attr-name := attr-value; ... ]
"}" ;
where with-item is:
[ module-alias := ] module module-name
Parameters​
Most sub-commands and options of this command are identical to the SDL alias declaration, with some additional features listed below:
- [ module-alias := ] module module-name
-
An optional list of module alias declarations to be used in the alias definition.
- create annotation annotation-name := value;
-
An optional list of annotation values for the alias. See
create annotation
for details.