Customize your router by writing just a few lines of Go code and compiling it with a single command. Eliminate the complexities associated with writing scripts and use the existing Go ecosystem.
core.RouterOnRequestHandler
Implements a custom middleware that runs before most internal middleware in the router for each client request. Most importantly this is called before tracing and authentication logic for each request. Use case: Custom Authentication Logic, Custom Tracing Logic, Early return, Request Validation.
core.RouterMiddlewareHandler
Implements a custom middleware on the router. The middleware is called for every client request. It allows you to modify the request before it is processed by the GraphQL engine. Use case: Logging, Caching, Early return, Request Validation, Header manipulation.
core.EnginePreOriginHandler
Implements a custom handler that is executed before the request is sent to the subgraph. This handler is called for every subgraph request. Use case: Logging, Header manipulation.
core.EnginePostOriginHandler
Implement a custom handler executed after the request to the subgraph but before the response is passed to the GraphQL engine. This handler is called for every subgraph response. Use cases: Logging, Caching.
core.Provisioner
Implements a Module lifecycle hook that is executed when the module is instantiated. Use it to prepare your module and validate the configuration.
core.Cleaner
Implements a Module lifecycle hook that is executed after the server is shutdown. Use it to close connections gracefully or for any other cleanup.
*OriginHandler
handlers are called concurrently when your GraphQL operation results in multiple subgraph requests. Due to that circumstance, you should handle the initial router request/response objects ctx.Request()
and ctx.ResponseWriter()
as read-only objects. Any modification without protecting them from concurrent writes, e.g., by a mutex, results in a race condition.RouterOnRequestHander
is only available since Router 0.188.0Priority
option. Modules with lower priority numbers are loaded first. Below is an example configuration:
myModule
has a priority of 1, meaning it will be loaded before modules with higher priority values.
core.RequestContext.Authentication()
auth.SetScopes()
to manually change the authentication’s scopes.
.SetScopes()
overwrites the existing scopes. If you’d like to append/preserve the built-in scopes, you can first use auth.Claims()
to get the existing scopes, and incorporate that into the updates scopes.
ctx.SetAuthenticationScopes(scopes []string)
that, if the Authentication is not set, it will initialize it with an empty object and set the scopes. If the authentication is already set, it will just override the scopes.
SetScopes()
.
Middleware
runs after the authentication of the request. However, sometimes you might want to run authentication related logic before the authentication actually happens. For example, let’s say that your client sends the Authorization
header without the Bearer
part in the header and you want to add Bearer
to the header, for this you can use the RouterOnRequestHandler
hook.
core.WriteResponseError
to return an error. It ensures that the request is properly tracked for tracing and metrics.
Value
with the value 1
. You can also validate your config in the core.Provisioner
handler.
.env.example
to .env
and fill in all required values.go run cmd/custom/main.go
to start your router or use your IDE to start the debug mode.