diff --git a/docs/router/custom-modules.mdx b/docs/router/custom-modules.mdx index 94e2a52c..f74835c0 100644 --- a/docs/router/custom-modules.mdx +++ b/docs/router/custom-modules.mdx @@ -78,6 +78,45 @@ c.OperationHash() // 81671788718 c.Content() // query MyOperation { ... } ``` +### Access sha256Hash + +You can access the sha256Hash of the operation using the following: + +```go +ctx.Operation().Sha256Hash() // ctx core.RequestContext +``` + +However, the sha256Hash is not computed by default and is only computed if it is required by the router. Some of the scenarios where it will be required by the router are: +* When the sha256Hash is used in expressions +* When the sha256Hash is added as a custom attribute for telemetry or access logs +* When using Persisted Operations +* If `persisted_operations.log_unknown` or `persisted_operations.safelist.enabled` is set to `true` + +However, if you are only using the sha256Hash in custom modules, you will need to explicitly force it to be computed. You can do this by calling `SetForceSha256Compute()` in the `RouterOnRequestHandler` hook, as it's the only hook that runs before the sha256Hash is computed by the router: + +```go +ctx.SetForceSha256Compute() // ctx core.RequestContext +``` + +You can verify whether the sha256Hash has already been computed by the router by checking if the value is empty, without calling the `SetForceSha256Compute()` method above. + +The following example shows how you can access and use the `sha256Hash` in custom modules: + +```go +// This runs before the sha256Hash is computed, and SetForceSha256Compute should only be called here +func (m *Sha256VerifierModule) RouterOnRequest(ctx core.RequestContext, next http.Handler) { + // Tell the router that the sha256Hash is required, in case it's not computed via other means + ctx.SetForceSha256Compute() + next.ServeHTTP(ctx.ResponseWriter(), ctx.Request()) +} + +func (m *Sha256VerifierModule) Middleware(ctx core.RequestContext, next http.Handler) { + // Print the sha256Hash + fmt.Println(ctx.Operation().Sha256Hash()) + next.ServeHTTP(ctx.ResponseWriter(), ctx.Request()) +} +``` + ### Access query plan information In Middleware, you can access some stats about the query plan that will be used for the operation.