diff --git a/docs/advanced/intercepting-http-request-response.md b/docs/advanced/intercepting-http-request-response.md new file mode 100644 index 00000000..75e859bb --- /dev/null +++ b/docs/advanced/intercepting-http-request-response.md @@ -0,0 +1,23 @@ +# Response Instrumentation +The DGS framework internally uses [GraphQL Java] and [Spring for GraphQL]. + +If you need to modify the HTTP request and response headers, you can leverage the `WebGraphQlInterceptor` in [Spring for GraphQL] (https://docs.spring.io/spring-graphql/reference/transports.html#server.interception) to accomplish this. +This provides a hook to update the request and response headers based using the `GraphQLContext` + +#### Example: + +```java +@Component +public class MyInterceptor implements WebGraphQlInterceptor { + @Override + public Mono intercept(WebGraphQlRequest request, Chain chain) { + String value = request.getHeaders().getFirst("myHeader"); + request.configureExecutionInput((executionInput, builder) -> + builder.graphQLContext(Collections.singletonMap("myHeader", value)).build()); + return chain.next(request).doOnNext((response) -> { + String value = response.getExecutionInput().getGraphQLContext().get("myContext"); + response.getResponseHeaders().add("MyContext", value); + }); + } +} +``` diff --git a/docs/advanced/response-instrumentation.md b/docs/advanced/response-instrumentation.md deleted file mode 100644 index 7e0c7a25..00000000 --- a/docs/advanced/response-instrumentation.md +++ /dev/null @@ -1,33 +0,0 @@ -# Response Instrumentation -The DGS framework internally uses [GraphQL Java]. -GraphQL Java supports the concept of `instrumentation` that can be used to perform additional operations (tracing, logging), and instrument the graphql query and response as needed. -The DGS framework makes it easy to add one or more instrumentation classes by implementing the `graphql.execution.instrumentation.Instrumentation` interface and registering the class as `@Component`. -The most common method to implement an `Instrumentation` interface is to extend `graphql.execution.instrumentation.SimpleInstrumentation`. - -Leveraging the concept of an instrumentation class, the framework offers the ability to add response headers to the outgoing response via setting the extensions field in the `ExecutionResult` for MVC. -This provides a hook to update the response headers based on the result of the `graphql` query execution. -The instrumentation class provides access to `graphql` specific state related to the query. -Simply add a map of headers using `DgsRestController.DGS_RESPONSE_HEADERS_KEY` to the extensions field of the result, and the framework handles adding those to the outgoing response. -This is a special key and is consumed by the framework, and thus will not appear in the final `extensions` field as part of the result. - - -#### Example: - -```java -@Component -public class MyInstrumentation extends SimpleInstrumentation { - @Override - public CompletableFuture instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) { - HashMap extensions = new HashMap<>(); - if(executionResult.getExtensions() != null) { - extensions.putAll(executionResult.getExtensions()); - } - - Map responseHeaders = new HashMap<>(); - responseHeaders.put("myHeader", "hello"); - extensions.put(DgsRestController.DGS_RESPONSE_HEADERS_KEY, responseHeaders); - - return super.instrumentExecutionResult(new ExecutionResultImpl(executionResult.getData(), executionResult.getErrors(), extensions), parameters); - } -} -``` diff --git a/docs/spring-graphql-integration.md b/docs/spring-graphql-integration.md index 500011c1..12521812 100644 --- a/docs/spring-graphql-integration.md +++ b/docs/spring-graphql-integration.md @@ -148,6 +148,11 @@ You can turn on async behavior by setting the `dgs.graphql.spring.webmvc.asyncdi It is worth noting that with the Spring GraphQL integration, your MockMVC test set up does need to be updated. Since web request processing is now based on async dispatching mechanism, we now [require explicit handling for this](https://docs.spring.io/spring-framework/reference/testing/spring-mvc-test-framework/async-requests.html) in the test setup. +### Modifying Response headers +Previously, the DGS Framework offered a mechanism to add custom response headers based on the result of processing the GraphQL query using a special `DgsRestController.DGS_RESPONSE_HEADERS_KEY` key. +This is no longer supported. +The recommended way forward is to use the `WebGraphQlInterceptor` in Spring for GraphQL as described [here](./advanced/intercepting-http-request-response.md)) + ## Known Gaps and Limitations At this time, we are lacking support for SSE based subscriptions which is available in the original DGS Framework. This is on the roadmap and will be made available in the near future depending on support in spring-graphql. diff --git a/mkdocs.yml b/mkdocs.yml index efd17fc1..46d520a2 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -31,7 +31,7 @@ nav: - Subscriptions: advanced/subscriptions.md - Interfaces and Unions: advanced/type-resolvers-for-abstract-types.md - Instrumentation (Tracing, Metrics): advanced/instrumentation.md - - Response Instrumentation for MVC: advanced/response-instrumentation.md + - Intercepting Http request and Response for MVC: advanced/intercepting-http-request-response.md - GraphQLContext : advanced/graphqlcontext-leveraging.md - Data Fetching Context: advanced/custom-datafetcher-context.md - Federated Testing: advanced/federated-testing.md