Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation samples on access to HTTP headers from controller #350

Closed
Flash1232 opened this issue Apr 13, 2022 · 2 comments
Closed
Assignees
Labels
type: documentation A documentation task
Milestone

Comments

@Flash1232
Copy link

Flash1232 commented Apr 13, 2022

What would be the intended way to access request extensions (which we use to populate the Authorization header) set via WebGraphQlInterceptor (https://docs.spring.io/spring-graphql/docs/current-SNAPSHOT/reference/html/#web-interception) from inside a controller method?

I picked up from this conversation and the docs that this option exists and was curious about the best solution to this. For now I am trying to access the Reactor Context's ServerWebExchange object to get the headers. This doesn't seem as the "right way" to go about this though.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 13, 2022
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Apr 14, 2022

One option is to extract whatever you need and put it in the GraphQLContext. The interceptor gives you access to the headers:

class MyInterceptor implements WebGraphQlInterceptor {

    @Override
    public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
        List<String> fooHeaderValue = request.getHeaders().get("fooHeader");
        request.configureExecutionInput((executionInput, builder) ->
                builder.graphQLContext(Collections.singletonMap("fooHeader", fooHeaderValue)).build());
        return chain.next(request);
    }
}

Then in your controller access that:

@Controller
class MyController {

    @QueryMapping
    Foo getFoo(@ContextValue String fooHeader) {
            // ...
    }
}

We could improve the documentation example in the Server Interception section with this example instead, which seems a more likely use case.

@rstoyanchev rstoyanchev added the status: waiting-for-feedback We need additional information before we can continue label Apr 14, 2022
@Flash1232
Copy link
Author

Thanks for your quick feedback!

This makes a lot of sense now and also for the documentation. As far as I understand, Extensions are primarily used for response error handling and as such are not exposed to the Controller methods.

This resolves the issue perfectly.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 14, 2022
rstoyanchev added a commit that referenced this issue Apr 19, 2022
@rstoyanchev rstoyanchev changed the title Accessing Extensions from Controller method Improve documentation samples on access to HTTP headers from controller Apr 19, 2022
@rstoyanchev rstoyanchev self-assigned this Apr 19, 2022
@rstoyanchev rstoyanchev added type: documentation A documentation task and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Apr 19, 2022
@rstoyanchev rstoyanchev added this to the 1.0.0-RC1 milestone Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants