type_bridge.expressions.functions¶
functions
¶
Function call expression support for TypeDB schema-defined functions.
FunctionCallExpr
dataclass
¶
Bases: Expression
Represents a call to a TypeDB function.
to_value_ast
¶
Convert to AST FunctionCallValue.
Source code in type_bridge/expressions/functions.py
to_typeql
¶
Deprecated: generate TypeQL function call syntax.
Source code in type_bridge/expressions/functions.py
ReturnType
dataclass
¶
Describes the return type of a TypeDB function.
Attributes:
| Name | Type | Description |
|---|---|---|
types |
list[str]
|
List of type names (e.g., ["integer"] or ["artifact", "integer"]) |
is_stream |
bool
|
True if function returns multiple rows (uses { } syntax) |
is_optional |
list[bool]
|
List of booleans indicating if each type is optional |
FunctionQuery
dataclass
¶
A TypeDB function call with query generation capabilities.
This class wraps a TypeDB schema function and provides methods to generate complete TypeQL queries for calling the function.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The TypeDB function name (e.g., "count-artifacts") |
args |
list[tuple[str, Any]]
|
Ordered list of (param_name, value) tuples |
return_type |
ReturnType
|
Description of what the function returns |
docstring |
str | None
|
Optional documentation for the function |
Example
Simple count function¶
fn = FunctionQuery( ... name="count-artifacts", ... return_type=ReturnType(["integer"], is_stream=False), ... ) fn.to_query() 'match let $result = count-artifacts(); fetch { "result": $result };'
Stream function with parameter¶
fn = FunctionQuery( ... name="get-neighbor-ids", ... args=[("$target_id", '"abc-123"')], ... return_type=ReturnType(["id"], is_stream=True), ... ) fn.to_query(limit=10) 'match let $id in get-neighbor-ids("abc-123"); limit 10; fetch { "id": $id };'
Composite return type¶
fn = FunctionQuery( ... name="count-artifacts-by-type", ... return_type=ReturnType(["artifact", "integer"], is_stream=True), ... ) fn.to_query() 'match let $artifact, $integer in count-artifacts-by-type(); fetch { "artifact": $artifact, "integer": $integer };'
to_call_ast
¶
Generate AST for function call.
Source code in type_bridge/expressions/functions.py
to_call
¶
Generate just the function call expression string.
to_match_let
¶
Generate the match let clause string for calling this function.
Source code in type_bridge/expressions/functions.py
to_fetch
¶
Generate the fetch clause string for the function results.
Source code in type_bridge/expressions/functions.py
to_query
¶
to_query(result_vars=None, fetch_keys=None, limit=None, offset=None, sort_var=None, sort_order='asc')
Generate a complete TypeQL query for calling this function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result_vars
|
list[str] | None
|
Optional custom variable names for results |
None
|
fetch_keys
|
list[str] | None
|
Optional custom keys for the fetch object |
None
|
limit
|
int | None
|
Optional limit on number of results |
None
|
offset
|
int | None
|
Optional offset for pagination |
None
|
sort_var
|
str | None
|
Optional variable to sort by |
None
|
sort_order
|
str
|
Sort order ("asc" or "desc") |
'asc'
|
Returns:
| Type | Description |
|---|---|
str
|
Complete TypeQL query string |
Example
fn.to_query(limit=10, offset=5) 'match let $id in get-ids(); offset 5; limit 10; fetch { "id": $id };'
Source code in type_bridge/expressions/functions.py
to_reduce_query
¶
Generate a query that returns the raw reduce result.
Useful for single-value functions like count() where you just want the number without fetch overhead.
Returns:
| Type | Description |
|---|---|
str
|
Query using reduce syntax |
Source code in type_bridge/expressions/functions.py
with_args
¶
Create a new FunctionQuery with the given arguments.
This is useful for parameterized functions where you want to create a bound version with specific argument values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Argument values keyed by parameter name (without $) |
{}
|
Returns:
| Type | Description |
|---|---|
FunctionQuery[T]
|
New FunctionQuery with the arguments set |
Example
fn = get_neighbor_ids.with_args(target_id="abc-123") fn.to_query()