Skip to content

Clarification Required on How to Get Response Header Derived Values #3619

@3redrubies

Description

@3redrubies

Question

Context

Hey team,

We're discussing internally whether we should return a value through HTTP response header or whether this value would be better returned as part of the GraphQL response body.

Before we commit to an approach, we want to clarify how Apollo’s interceptor chain interacts with TaskLocal propagation, especially when the value we need is only known after the HTTP response is received after next(request).

Our understanding is that this mechanism seems primarily suited for setting context (e.g., headers) rather than extracting response context upward.

Example

Our goal is to make a response-header–derived value available to the caller of
apolloClient.fetch(...). Since GraphQLResponse does not expose HTTP headers, we explored whether @TaskLocal could be used to propagate a value upward in the chain.

public final class HTTPResponseHeaderInterceptor: HTTPInterceptor {
    @TaskLocal
    static var someValue: String?

    public func intercept(
        request: URLRequest,
        next: @Sendable (URLRequest) async throws -> Apollo.HTTPResponse
    ) async throws -> Apollo.HTTPResponse {
        let response = try await next(request)
        let someValue = response.response.allHeaderFields["Some-Value"] as! String

        // Is there a way to make this value available to subsequent interceptors in the chain?
        HTTPResponseHeaderInterceptor.$someValue.withValue(someValue) { }

        return response
    }
}

Because next(request) must be called before we know the value, nothing in the interceptor chain can actually see this task-local value since we don’t have a hook to continue the chain inside the withValue scope.

Question

Is there a supported mechanism to grab response header field values to return back to the caller? Or whether the recommended approach is simply to return these values inside the GraphQL response body instead?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that have a question which should be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions