Skip to content

Update documentation to show setting response headers with value from @SchemaMapping method #428

New issue

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

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

Already on GitHub? # to your account

Closed
hanyunmin opened this issue Jun 30, 2022 · 5 comments
Assignees
Labels
type: documentation A documentation task
Milestone

Comments

@hanyunmin
Copy link

As the title suggests, I am trying to set cookie in signin using graphql.

I am using spring mvc and I know that the existing cookie setting method using HttpServletResponse is not available.

Is there no way to set cookie in graphql?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 30, 2022
@hanyunmin hanyunmin changed the title can use set-cookie can use set-cookie? Jun 30, 2022
@bclozel bclozel self-assigned this Jun 30, 2022
@bclozel
Copy link
Member

bclozel commented Jun 30, 2022

From where would you like to set this cookie value? A Servlet filter? A @SchemaMapping controller method?
Does the interception section of the reference documentation help?

@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label Jun 30, 2022
@hanyunmin
Copy link
Author

I want to use it in @schemamapping controller method.
The interception section of the reference documentation doesn't help in my situation.

@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 Jul 1, 2022
@bclozel
Copy link
Member

bclozel commented Jul 1, 2022

Spring for GraphQL is transport-agnostic, meaning that the @SchemaMapping annotated controllers can work on top of HTTP, RSocket, or other transports that don't involve HTTP at all. Once we're in the ExecutionGraphQlService, there is no concept tied to the transport.

The interception section is probably still the best way to achieve this: you can set a value in the GraphQL context and later use it to set response headers.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2022
@bclozel bclozel added status: invalid An issue that we don't feel is valid and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Jul 1, 2022
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Jul 1, 2022

I guess the example shows how to take a value from the request, put it in the GraphQLContext, and then access it as an @ContextValue from the controller method. You can also do the reverse, by saving a value in the GraphQLContext from a controller method:

@Controller
class MyController {

    @QueryMapping
    Person person(GraphQLContext context) {
            context.put("cookie", "123");
    }
}

And then access that from an WebGraphQlInterceptor to set the cookie on the response:

class ResponseHeaderInterceptor implements WebGraphQlInterceptor {

	@Override
	public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
		return chain.next(request).doOnNext(response -> {
			String value = response.getExecutionInput().getGraphQLContext().get("cookie");
			ResponseCookie cookie = ResponseCookie.from("cookieName", value).build();
			response.getResponseHeaders().add(HttpHeaders.SET_COOKIE, cookie.toString());
		});
	}
}

I think we could improve a bit the example in the docs based on this.

@rstoyanchev rstoyanchev reopened this Jul 1, 2022
@rstoyanchev rstoyanchev added type: documentation A documentation task and removed status: invalid An issue that we don't feel is valid labels Jul 1, 2022
@rstoyanchev rstoyanchev self-assigned this Jul 1, 2022
@rstoyanchev rstoyanchev added this to the 1.0.1 milestone Jul 1, 2022
@rstoyanchev rstoyanchev changed the title can use set-cookie? Update documentation to show setting response headers with value from @SchemaMapping method Jul 1, 2022
@hanyunmin
Copy link
Author

@rstoyanchev
This is a good example.
thank you.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

4 participants