|
| 1 | +--- |
| 2 | +title: Expressions |
| 3 | +--- |
| 4 | + |
| 5 | +import { Callout } from '@theguild/components' |
| 6 | + |
| 7 | +# Expressions |
| 8 | + |
| 9 | +The Hive Router uses **expressions** written in |
| 10 | +[VRL (Vector Remap Language)](https://vector.dev/docs/reference/vrl/) to make dynamic decisions at |
| 11 | +runtime. VRL is a small, declarative language originally created by DataDog for the |
| 12 | +[Vector observability pipeline](https://vector.dev/) and is well‑suited for parsing and transforming |
| 13 | +structured data. In Hive Router, expressions let you: |
| 14 | + |
| 15 | +- [Route requests to different subgraph URLs](../guides/dynamic-subgraph-routing) based on HTTP |
| 16 | + headers, geographic region or other request metadata. |
| 17 | +- [Insert, modify or remove HTTP headers](../guides/header-manipulation) before forwarding a request |
| 18 | + to a subgraph or before sending a response back to the client. |
| 19 | + |
| 20 | +Although the concept of "expressions" appears in several parts of the configuration, the syntax and |
| 21 | +available APIs are consistent. This page explains **what variables and functions are available, when |
| 22 | +different variables can be used**, and links you to the official VRL documentation for the full |
| 23 | +language reference. |
| 24 | + |
| 25 | +## Syntax overview |
| 26 | + |
| 27 | +Every expression evaluates to a value, and you can build more complex logic using functions and |
| 28 | +control‑flow. For example: |
| 29 | + |
| 30 | +``` |
| 31 | +if .request.headers."x-feature-toggle" == "beta" { |
| 32 | + "https://beta.example.com/graphql" |
| 33 | +} else { |
| 34 | + .original_url |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +<Callout type="info"> |
| 39 | + You can break long expressions across multiple lines using the `|` indentation in YAML. |
| 40 | +</Callout> |
| 41 | + |
| 42 | +VRL supports [variables](#available-variables) (prefixed with a dot), built‑in |
| 43 | +[functions](#available-functions), simple arithmetic and comparisons, and control structures such as |
| 44 | +`if … else`. |
| 45 | + |
| 46 | +Multi‑line expressions are especially useful for readability and complex routing logic. |
| 47 | + |
| 48 | +## Available values |
| 49 | + |
| 50 | +Within an expression you interact with a few top‑level values. The most important ones are: |
| 51 | + |
| 52 | +### `.request` |
| 53 | + |
| 54 | +Represents the incoming HTTP request. It exposes nested fields such as: |
| 55 | + |
| 56 | +- `.request.headers` - a case‑insensitive map of request headers. For example, |
| 57 | + `.request.headers.authorization` gives you the value of the `Authorization` header. |
| 58 | +- `.request.method` - the HTTP method as a lowercase string (e.g. `"post"`). |
| 59 | +- `.request.url.host` - the request host (e.g. `api.example.com`). |
| 60 | +- `.request.url.port` - the request port (e.g. `443`). |
| 61 | +- `.request.url.path` - the request path (e.g. `/graphql`). |
| 62 | +- `.request.operation.name` - the name of the GraphQL operation being executed, if provided. |
| 63 | +- `.request.operation.type` - the type of GraphQL operation being executed (e.g. `"query"`, |
| 64 | + `"mutation"`, or `"subscription"`). |
| 65 | +- `.request.operation.query` - the full GraphQL query string being executed. |
| 66 | + |
| 67 | +These values allow you to branch logic based on who is calling your API and what data they send. |
| 68 | + |
| 69 | +### `.response` |
| 70 | + |
| 71 | +Available only in **response** header rules. It represents the HTTP response from a subgraph call. |
| 72 | +You can inspect `.response.url` or `.response.headers` and then set or override additional headers. |
| 73 | +For example, you could conditionally add a cache header based on the subgraph’s status code. This |
| 74 | +variable is not present when computing request headers or dynamic URLs, because the response does |
| 75 | +not yet exist. |
| 76 | + |
| 77 | +- `.response.headers` - a case‑insensitive map of response headers. |
| 78 | + |
| 79 | +### `.subgraph` |
| 80 | + |
| 81 | +Available in both **request** and **response** header rules. It provides metadata about the subgraph |
| 82 | +handling the current request, including: |
| 83 | + |
| 84 | +- `.subgraph.name` - the name of the subgraph as defined in your supergraph schema. |
| 85 | + |
| 86 | +### `.timestamp` |
| 87 | + |
| 88 | +While not a full object, the special variable `.timestamp` yields the current UNIX timestamp. The |
| 89 | +header manipulation guide demonstrates using `.timestamp` to add a `X‑Request‑Time` header. This is |
| 90 | +useful for debugging and tracing requests. |
| 91 | + |
| 92 | +### `.original_url` |
| 93 | + |
| 94 | +This variable is only available in [dynamic routing expressions](./override_subgraph_urls). It holds |
| 95 | +the default URL for the current subgraph, as declared in your supergraph schema. Because the |
| 96 | +expression must return a URL, you should always provide a fallback using `.original_url` so that |
| 97 | +requests are routed somewhere when no condition matches. The configuration reference emphasises this |
| 98 | +by showing a pattern where the expression returns `.original_url` in the `else` branch. |
| 99 | + |
| 100 | +## Available functions |
| 101 | + |
| 102 | +The Hive Router exposes the full set of |
| 103 | +[VRL built‑in functions](https://vector.dev/docs/reference/vrl/functions/). The configuration guides |
| 104 | +use a few of them in examples: |
| 105 | + |
| 106 | +- `replace(value, pattern, replacement)` - returns a new string with all matches of pattern |
| 107 | + replaced. In the header manipulation guide it's used to normalise an `Authorization` header by |
| 108 | + stripping existing prefixes. |
| 109 | +- `contains(value, substring)` - tests whether a string contains a substring. |
| 110 | +- `random_int(min, max)` - produces a random integer in the inclusive range. Combined with `if` to |
| 111 | + create percentage‑based canary routing of subgraphs. |
| 112 | +- `lowercase(value)`, `uppercase(value)`, `split(value, delimiter)`, `to_int(value)` - helpful for |
| 113 | + normalising and parsing header values. |
| 114 | + |
| 115 | +For a complete list of functions - including string manipulation, number conversion, JSON parsing, |
| 116 | +and more - see the [VRL function reference](https://vector.dev/docs/reference/vrl/functions/). When |
| 117 | +writing expressions, you can call any of these functions directly. |
| 118 | + |
| 119 | +<Callout type="warning"> |
| 120 | + Functions that could perform network I/O (e.g. HTTP requests) are intentionally not available, so |
| 121 | + expressions remain side‑effect‑free and deterministic. |
| 122 | +</Callout> |
| 123 | + |
| 124 | +## Troubleshooting |
| 125 | + |
| 126 | +When an expression doesn't work as expected, it can be due to a compilation error (syntax) or a |
| 127 | +runtime error (evaluation). The Hive Router provides detailed logs to help you diagnose the issue. |
| 128 | + |
| 129 | +### Compilation Errors |
| 130 | + |
| 131 | +Compilation errors occur if your expression has invalid VRL syntax. The router will fail to start |
| 132 | +and log an error message pointing to the problematic expression. Common syntax errors include |
| 133 | +mismatched parentheses, missing commas, or incorrect function names. |
| 134 | + |
| 135 | +### Runtime Errors |
| 136 | + |
| 137 | +Runtime errors happen when a syntactically correct expression fails during execution. These are |
| 138 | +logged by the router during a request. The router will also return an error response to the client. |
| 139 | + |
| 140 | +Here are some frequent causes of runtime errors in expressions: |
| 141 | + |
| 142 | +1. **Accessing an unavailable variable**: Expressions run in specific contexts. For example, trying |
| 143 | + to access `.response` in a request header rule will fail because the response from the subgraph |
| 144 | + doesn't exist yet. |
| 145 | +1. **Type Mismatches**: VRL is strongly typed. If you try to perform an operation on incompatible |
| 146 | + types, like comparing a string with an integer, you'll get a runtime error. |
| 147 | +1. **Handling `null` values**: Accessing a non-existent header or field returns `null`. Operations |
| 148 | + on `null` values can lead to errors. Always check for the existence of a value or provide a |
| 149 | + fallback. You can use the `||` operator to provide a default value (e.g., |
| 150 | + `.request.headers."x-custom-header" || "default-value"`). |
| 151 | + |
| 152 | +For a full list of error codes and their meanings, refer to the |
| 153 | +[VRL error reference](https://vector.dev/docs/reference/vrl/errors). |
0 commit comments