Template Expressions Overview
Template Expressions are a robust mechanism for accessing information related to requests, responses, and other key aspects of the router’s operation. They enable conditional feature activation and allow for extracting and configuring values dynamically. An example use case involves conditionally blocking mutations:Template Language
The expressions are based on the expr-lang template language. This language is characterized by its:- Safety – Expressions are evaluated securely.
- Speed – The language is optimized for performance.
- Side-effect-free operation – Expressions are read-only and do not alter state.
Naming Conventions
- Fields: Use camelCase (e.g.,
request.auth.claims
). - Methods: Use PascalCase (e.g.,
request.header.Get("Content-Type")
). - Utility Functions: camelCase (e.g. trim) for the full list see here.
Expression Context
Request Object
Therequest
object is a read-only entity that provides details about the incoming client request. It is accessible at all times during the lifecycle of the request.
Available Fields:
request.method
request.url.host
request.url.path
request.url.port
request.trace.sampled
request.error
Operation Object
request.operation
request.operation.name
request.operation.type
(possible values:mutation
orquery
)request.operation.hash
Example expressions
Client Object
request.client
request.client.name
request.client.version
Example expressions
Header Access:
request.header.Get('Content-Type')
– Header retrieval is case-insensitive.
Example expressions
Body Object
The body object holds information related to the request body. However, if used in telemetry attribute expressions, it will be empty because the router hasn’t read the body at that stage of processing yet. To optimize performance, the raw request body (raw.body
) is only included in the expression context if at least one expression explicitly references it.
Available Fields:
request.body.raw
Example expressions
This example returns the raw body only when an error occurs, which is useful for debugging when used in access log expressions.Authentication Object
Theauth
object contains authentication-related information for the request. It becomes available after authentication is complete.
Available Fields:
request.auth
request.auth.isAuthenticated
request.auth.type
request.auth.claims
request.auth.scopes
Example expressions
Subgraph Object
The Subgraph object provides information about the current subgraph being accessed in the request. Please note that any expression that uses the subgraph object will only have correct values set in the lifecycle of a subgraph request. It contains the following properties:Properties
subgraph.id
(string): The unique identifier of the subgraphsubgraph.name
(string): The name of the subgraphsubgraph.request
(SubgraphRequest): Contains information about the subgraph request
SubgraphRequest Properties
subgraph.request.error
(error): Any error that occurred during the subgraph requestsubgraph.request.clientTrace
(ClientTrace): Contains tracing information about the client connection
ClientTrace Properties
subgraph.request.clientTrace.connAcquireDuration
(float64): The duration in seconds it took to acquire the connection to the subgraph
subgraph
identifier. For example, you can access the subgraph name using subgraph.name
or check for errors using subgraph.request.error
.
Example expressions
Additional Notes
- A Request for Comments (RFC) is open for feedback on the complete API specification. Future implementations will be driven by customer requirements.
- The Expr language project offers a playground for testing and developing custom expressions, simplifying the process of constructing and validating expressions.