Plugin Execution and Lifecycle
Control Flow
Short-Circuit Responses
In many of the hooks mentioned above, you can short-circuit the request processing by providing a custom response.
For example, to return an early error response in certain conditions:
The end_with_response method on payload provides a custom HTTP response. Several helper methods
are also available to simplify creating early responses.
Helpers for early responses
To make it easier to create early responses, Hive Router provides some helper methods on the hook payloads.
end_with_response(response: Response)- Accepts aserde-serializable body, automatically serializes it to JSON, and sets the appropriate headers. To simplify the above example:
end_with_graphql_error(error: GraphQLError, status: StatusCode)- Accepts aGraphQLErrordirectly and constructs the appropriate GraphQL error response.
end_with_graphql_errors(errors: Vec<GraphQLError>, status: StatusCode)- Likeend_with_graphql_error, but terminates the request with multiple GraphQL errors in a single response. Since a GraphQL response can carry anerrorsarray, this lets a plugin report several violations at once instead of dropping all but the first.
Overriding Default Behavior
Rather than short-circuiting the entire request and returning an early response, you may want to override the default behavior at a specific stage.
For example, when implementing automatic persisted queries (APQ), you need to replace the query
field in the GraphQLParams struct with the actual query string, resolved from the hash provided by
the client.
Integrate with the Router
Performance Considerations
When creating custom plugins, it’s important to understand that your plugin will be part of the request’s hot path. The hot path refers to the critical path that affects the performance of the router.
When implementing hooks in your plugin, you should be mindful of the performance implications of your code.
Avoid performing heavy computations or blocking operations in request lifecycle hooks, as they can significantly impact the latency of your GraphQL requests.
Prefer loading data in the background and caching it when possible, instead of fetching it during a GraphQL request.
Background Tasks
Hive Router provides a simple mechanism to run background tasks using the
tokio runtime.
As a plugin developer, you don’t need to deal with threads or async runtimes - you can use the Hive Router core runtime to register and manage the lifecycle of your background tasks.
Background tasks can be registered during the
on_plugin_init hook. Here is a simple example
of a background task that runs periodically:
Logging
Hive Router uses the tracing crate for its internal
logging. Logs emitted from plugins are correlated with the request being processed.
Use the built-in macros (debug!(...), info!(...), warn!(...), and error!(...)) to emit log
lines, which will appear based on the Router’s logger configuration.
OpenTelemetry Traces
Hive Router supports OpenTelemetry at its core, and uses the
tracing crate for collecting, processing, and exporting
OpenTelemetry trace information.
Use the tracing API to create spans and
enrich the exported trace data with custom spans: