spring-projects / spring-graphql

Spring Integration for GraphQL
https://spring.io/projects/spring-graphql
Apache License 2.0
1.54k stars 306 forks source link

Accessing response headers in SyncGraphQlClientInterceptor #1022

Closed morohon closed 4 months ago

morohon commented 4 months ago

Hi all! Please tell me is it possible to somehow get information about response headers in SyncGraphQlClientInterceptor? The problem we are trying to solve: There is a certain graphql server provider that returns apollo-trace-id in the response headers. On our side, we need to log this traceID to improve observability. We can get the response header at the ClientHttpRequestInterceptor level, but we want to log this identifier in two cases:

  1. If the provider's server returned an incorrect HTTP status.
  2. If the provider’s server returned incorrect data in the response (for example, there are some errors in the errors block of the response that do not allow this response to be considered valid).

And if the first point can be solved quite simply using ClientHttpRequestInterceptor, then the second point can be more difficult to solve using it. The problem is that ClientHttpRequestInterceptordoes not have a deserialized response body and there is no easy way to parse it. And if we take SyncGraphQlClientInterceptor, then there is no access to response headers due to the lack of such data in GraphQlResponse and the transfer of this information to HttpSyncGraphQlTransport. There are essentially two options:

  1. Create a certain context for storing apollo-trace-id at the ClientHttpRequestInterceptor level (a class with the ThreadLocal field) and then pick it up at the SyncGraphQlClientInterceptor level.
  2. Additionally deserialize the response body at the ClientHttpRequestInterceptor level and log information about traceID there.

But both options, to be honest, look so-so. Maybe there are some better options that we haven't noticed?

bclozel commented 4 months ago

I've replied to your question on StackOverflow. Thanks!