Add response middlewares to the network queryers - #2
Merged
Merged
Conversation
NetworkMiddleware lets callers see every outgoing request, but nothing lets them see the response. SingleRequestQueryer and MultiOpQueryer both parse the full response object and then decode only "data" into the receiver, so anything else the service returned, notably top-level "extensions", is dropped before the caller can look at it. ResponseMiddleware is the response-side counterpart: a function that is handed the parsed response and the query's context, after parsing and before "data" is decoded. Both queryers accept a list of them through WithResponseMiddlewares, mirroring WithMiddlewares. In MultiOpQueryer the middlewares run once per bundled query, with that query's own context and its own entry of the batched response. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
greenmato
approved these changes
Sep 15, 2026
Collaborator
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
The Content API team has a feature in its GraphQL API that requires both custom directives to work (already fixed) and
extensionssupport in GQL responses (this PR + one in thegatewaylibrary fork + one in our GQL gateway). Specifically, we want to forward theextensions.relationskey that Content API returns for queries carrying the@capiRelationMapdirective.Problem
Both network queryers parse the full downstream response and then decode only
datainto the receiver.SingleRequestQueryer.Query(queryerNetwork.go):Notice that
extensionsis ignored and thrown away.MultiOpQueryer.Query(queryerMultiOp.go) has the same problem.Solution
There is a request middleware in the repo already,
NetworkMiddleware func(*http.Request) error, but there's no equivalent for responses, so we'll add one:The middlewares run after the response is parsed and before
datais decoded, with the full top-level object (data,errors,extensions, ...). Returning an error aborts the query with that error, like aNetworkMiddlewaredoes on the request side.The library makes no assumptions about what a middleware does with the response. Our use is to collect
extensionsper operation in the GQL gateway (https://github.com/amboss-mededu/graphql-gateway/pull/695), but it is equally usable for logging, metrics or tracing on the response side.Tests cover the single request queryer (JSON and multipart, error propagation) and the multi-op queryer (each bundled query sees its own entry).
This change is written to be upstreamable to
nautilus/graphqlas is.