Skip to content

Commit 66b3c82

Browse files
committed
Add cookies to WebGraphQlRequest
Closes gh-626
1 parent 2e229fe commit 66b3c82

File tree

14 files changed

+100
-42
lines changed

14 files changed

+100
-42
lines changed

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/WebGraphQlHandlerGraphQlTransport.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,14 +77,10 @@ public CodecConfigurer getCodecConfigurer() {
7777

7878
@Override
7979
protected Mono<ExecutionGraphQlResponse> executeInternal(ExecutionGraphQlRequest executionRequest) {
80-
8180
String id = idGenerator.generateId().toString();
8281
Map<String, Object> body = executionRequest.toMap();
83-
84-
WebGraphQlRequest webExecutionRequest =
85-
new WebGraphQlRequest(this.url, this.headers, body, id, null);
86-
87-
return this.graphQlHandler.handleRequest(webExecutionRequest).cast(ExecutionGraphQlResponse.class);
82+
WebGraphQlRequest webRequest = new WebGraphQlRequest(this.url, this.headers, null, body, id, null);
83+
return this.graphQlHandler.handleRequest(webRequest).cast(ExecutionGraphQlResponse.class);
8884
}
8985

9086
}

spring-graphql/src/main/java/org/springframework/graphql/server/WebGraphQlRequest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,9 +22,13 @@
2222

2323
import org.springframework.graphql.ExecutionGraphQlRequest;
2424
import org.springframework.graphql.support.DefaultExecutionGraphQlRequest;
25+
import org.springframework.http.HttpCookie;
2526
import org.springframework.http.HttpHeaders;
2627
import org.springframework.lang.Nullable;
2728
import org.springframework.util.Assert;
29+
import org.springframework.util.CollectionUtils;
30+
import org.springframework.util.LinkedMultiValueMap;
31+
import org.springframework.util.MultiValueMap;
2832
import org.springframework.util.StringUtils;
2933
import org.springframework.web.server.ServerWebInputException;
3034
import org.springframework.web.util.UriComponents;
@@ -42,21 +46,39 @@
4246
*/
4347
public class WebGraphQlRequest extends DefaultExecutionGraphQlRequest implements ExecutionGraphQlRequest {
4448

49+
private static final MultiValueMap<String, HttpCookie> EMPTY_COOKIES =
50+
CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>());
51+
52+
4553
private final UriComponents uri;
4654

4755
private final HttpHeaders headers;
4856

57+
private final MultiValueMap<String, HttpCookie> cookies;
58+
59+
60+
/**
61+
* Create an instance.
62+
* @deprecated as of 1.1.3 in favor of the constructor with cookies
63+
*/
64+
@Deprecated
65+
public WebGraphQlRequest(URI uri, HttpHeaders headers, Map<String, Object> body, String id, @Nullable Locale locale) {
66+
this(uri, headers, null, body, id, locale);
67+
}
4968

5069
/**
5170
* Create an instance.
5271
* @param uri the URL for the HTTP request or WebSocket handshake
5372
* @param headers the HTTP request headers
73+
* @param cookies the request cookies
5474
* @param body the deserialized content of the GraphQL request
5575
* @param id an identifier for the GraphQL request
5676
* @param locale the locale from the HTTP request, if any
77+
* @since 1.1.3
5778
*/
5879
public WebGraphQlRequest(
59-
URI uri, HttpHeaders headers, Map<String, Object> body, String id, @Nullable Locale locale) {
80+
URI uri, HttpHeaders headers, @Nullable MultiValueMap<String, HttpCookie> cookies,
81+
Map<String, Object> body, String id, @Nullable Locale locale) {
6082

6183
super(getKey("query", body), getKey("operationName", body), getKey("variables", body),
6284
getKey("extensions", body), id, locale);
@@ -66,6 +88,7 @@ public WebGraphQlRequest(
6688

6789
this.uri = UriComponentsBuilder.fromUri(uri).build(true);
6890
this.headers = headers;
91+
this.cookies = (cookies != null ? cookies : EMPTY_COOKIES);
6992
}
7093

7194
@SuppressWarnings("unchecked")

spring-graphql/src/main/java/org/springframework/graphql/server/WebSocketGraphQlRequest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,9 +21,11 @@
2121
import java.util.Locale;
2222
import java.util.Map;
2323

24+
import org.springframework.http.HttpCookie;
2425
import org.springframework.http.HttpHeaders;
2526
import org.springframework.lang.Nullable;
2627
import org.springframework.util.Assert;
28+
import org.springframework.util.MultiValueMap;
2729

2830

2931
/**
@@ -38,20 +40,34 @@ public class WebSocketGraphQlRequest extends WebGraphQlRequest {
3840
private final WebSocketSessionInfo sessionInfo;
3941

4042

43+
/**
44+
* Create an instance.
45+
* @deprecated as of 1.1.3 in favor of the constructor with cookies
46+
*/
47+
@Deprecated
48+
public WebSocketGraphQlRequest(
49+
URI uri, HttpHeaders headers, Map<String, Object> body, String id, @Nullable Locale locale,
50+
WebSocketSessionInfo sessionInfo) {
51+
52+
this(uri, headers, null, body, id, locale, sessionInfo);
53+
}
54+
4155
/**
4256
* Create an instance.
4357
* @param uri the URL for the HTTP request or WebSocket handshake
4458
* @param headers the HTTP request headers
59+
* @param cookies the request cookies
4560
* @param body the deserialized content of the GraphQL request
4661
* @param id the id from the GraphQL over WebSocket {@code "subscribe"} message
4762
* @param locale the locale from the HTTP request, if any
4863
* @param sessionInfo the WebSocket session id
64+
* @since 1.1.3
4965
*/
5066
public WebSocketGraphQlRequest(
51-
URI uri, HttpHeaders headers, Map<String, Object> body, String id, @Nullable Locale locale,
52-
WebSocketSessionInfo sessionInfo) {
67+
URI uri, HttpHeaders headers, @Nullable MultiValueMap<String, HttpCookie> cookies,
68+
Map<String, Object> body, String id, @Nullable Locale locale, WebSocketSessionInfo sessionInfo) {
5369

54-
super(uri, headers, body, id, locale);
70+
super(uri, headers, cookies, body, id, locale);
5571
Assert.notNull(sessionInfo, "WebSocketSessionInfo is required");
5672
this.sessionInfo = sessionInfo;
5773
}

spring-graphql/src/main/java/org/springframework/graphql/server/webflux/GraphQlHttpHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -74,7 +74,8 @@ public Mono<ServerResponse> handleRequest(ServerRequest serverRequest) {
7474
return serverRequest.bodyToMono(MAP_PARAMETERIZED_TYPE_REF)
7575
.flatMap(body -> {
7676
WebGraphQlRequest graphQlRequest = new WebGraphQlRequest(
77-
serverRequest.uri(), serverRequest.headers().asHttpHeaders(), body,
77+
serverRequest.uri(), serverRequest.headers().asHttpHeaders(),
78+
serverRequest.cookies(), body,
7879
serverRequest.exchange().getRequest().getId(),
7980
serverRequest.exchange().getLocaleContext().getLocale());
8081
if (logger.isDebugEnabled()) {

spring-graphql/src/main/java/org/springframework/graphql/server/webflux/GraphQlWebSocketHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -148,7 +148,8 @@ public Mono<Void> handle(WebSocketSession session) {
148148
return GraphQlStatus.close(session, GraphQlStatus.INVALID_MESSAGE_STATUS);
149149
}
150150
WebSocketGraphQlRequest request = new WebSocketGraphQlRequest(
151-
handshakeInfo.getUri(), handshakeInfo.getHeaders(), payload, id, null, sessionInfo);
151+
handshakeInfo.getUri(), handshakeInfo.getHeaders(), handshakeInfo.getCookies(),
152+
payload, id, null, sessionInfo);
152153
if (logger.isDebugEnabled()) {
153154
logger.debug("Executing: " + request);
154155
}

spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlHttpHandler.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,8 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24-
2524
import jakarta.servlet.ServletException;
25+
import jakarta.servlet.http.Cookie;
2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
2828
import reactor.core.publisher.Mono;
@@ -31,10 +31,13 @@
3131
import org.springframework.core.ParameterizedTypeReference;
3232
import org.springframework.graphql.server.WebGraphQlHandler;
3333
import org.springframework.graphql.server.WebGraphQlRequest;
34+
import org.springframework.http.HttpCookie;
3435
import org.springframework.http.MediaType;
3536
import org.springframework.util.AlternativeJdkIdGenerator;
3637
import org.springframework.util.Assert;
3738
import org.springframework.util.IdGenerator;
39+
import org.springframework.util.LinkedMultiValueMap;
40+
import org.springframework.util.MultiValueMap;
3841
import org.springframework.web.HttpMediaTypeNotSupportedException;
3942
import org.springframework.web.server.ServerWebInputException;
4043
import org.springframework.web.servlet.function.ServerRequest;
@@ -86,8 +89,9 @@ public GraphQlHttpHandler(WebGraphQlHandler graphQlHandler) {
8689
public ServerResponse handleRequest(ServerRequest serverRequest) throws ServletException {
8790

8891
WebGraphQlRequest graphQlRequest = new WebGraphQlRequest(
89-
serverRequest.uri(), serverRequest.headers().asHttpHeaders(), readBody(serverRequest),
90-
this.idGenerator.generateId().toString(), LocaleContextHolder.getLocale());
92+
serverRequest.uri(), serverRequest.headers().asHttpHeaders(), initCookies(serverRequest),
93+
readBody(serverRequest), this.idGenerator.generateId().toString(),
94+
LocaleContextHolder.getLocale());
9195

9296
if (logger.isDebugEnabled()) {
9397
logger.debug("Executing: " + graphQlRequest);
@@ -107,6 +111,16 @@ public ServerResponse handleRequest(ServerRequest serverRequest) throws ServletE
107111
return ServerResponse.async(responseMono);
108112
}
109113

114+
private static MultiValueMap<String, HttpCookie> initCookies(ServerRequest serverRequest) {
115+
MultiValueMap<String, Cookie> source = serverRequest.cookies();
116+
MultiValueMap<String, HttpCookie> target = new LinkedMultiValueMap<>(source.size());
117+
source.values().forEach(cookieList -> cookieList.forEach(cookie -> {
118+
HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
119+
target.add(cookie.getName(), httpCookie);
120+
}));
121+
return target;
122+
}
123+
110124
private static Map<String, Object> readBody(ServerRequest request) throws ServletException {
111125
try {
112126
return request.body(MAP_PARAMETERIZED_TYPE_REF);

spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlWebSocketHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -204,7 +204,7 @@ private void handleInternal(WebSocketSession session, TextMessage webSocketMessa
204204
Assert.notNull(uri, "Expected handshake url");
205205
HttpHeaders headers = session.getHandshakeHeaders();
206206
WebSocketGraphQlRequest request =
207-
new WebSocketGraphQlRequest(uri, headers, payload, id, null, state.getSessionInfo());
207+
new WebSocketGraphQlRequest(uri, headers, null, payload, id, null, state.getSessionInfo());
208208
if (logger.isDebugEnabled()) {
209209
logger.debug("Executing: " + request);
210210
}

spring-graphql/src/test/java/org/springframework/graphql/data/query/QuerydslDataFetcherTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,8 +50,8 @@
5050
import org.springframework.graphql.data.query.QuerydslDataFetcher.Builder;
5151
import org.springframework.graphql.data.query.QuerydslDataFetcher.QuerydslBuilderCustomizer;
5252
import org.springframework.graphql.execution.RuntimeWiringConfigurer;
53-
import org.springframework.graphql.server.WebGraphQlRequest;
5453
import org.springframework.graphql.server.WebGraphQlHandler;
54+
import org.springframework.graphql.server.WebGraphQlRequest;
5555
import org.springframework.graphql.server.WebGraphQlResponse;
5656
import org.springframework.http.HttpHeaders;
5757
import org.springframework.lang.Nullable;
@@ -302,7 +302,8 @@ private static GraphQlSetup initGraphQlSetup(
302302

303303
private WebGraphQlRequest request(String query) {
304304
return new WebGraphQlRequest(
305-
URI.create("/"), new HttpHeaders(), Collections.singletonMap("query", query), "1", Locale.ENGLISH);
305+
URI.create("/"), new HttpHeaders(), null,
306+
Collections.singletonMap("query", query), "1", Locale.ENGLISH);
306307
}
307308

308309

spring-graphql/src/test/java/org/springframework/graphql/data/query/jpa/QueryByExampleDataFetcherJpaTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,15 +25,14 @@
2525
import java.util.stream.Collectors;
2626

2727
import javax.sql.DataSource;
28-
import jakarta.persistence.EntityManagerFactory;
2928

3029
import graphql.schema.DataFetcher;
30+
import jakarta.persistence.EntityManagerFactory;
3131
import org.junit.jupiter.api.Disabled;
3232
import org.junit.jupiter.api.Test;
3333
import reactor.core.publisher.Mono;
3434

3535
import org.springframework.beans.factory.annotation.Autowired;
36-
import org.springframework.beans.factory.annotation.Value;
3736
import org.springframework.context.annotation.Bean;
3837
import org.springframework.context.annotation.Configuration;
3938
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@@ -189,7 +188,8 @@ private static GraphQlSetup initGraphQlSetup(@Nullable QueryByExampleExecutor<?>
189188

190189
private WebGraphQlRequest request(String query) {
191190
return new WebGraphQlRequest(
192-
URI.create("/"), new HttpHeaders(), Collections.singletonMap("query", query), "1", null);
191+
URI.create("/"), new HttpHeaders(), null,
192+
Collections.singletonMap("query", query), "1", null);
193193
}
194194

195195

spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherMongoDbTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,8 +41,8 @@
4141
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
4242
import org.springframework.data.repository.query.QueryByExampleExecutor;
4343
import org.springframework.graphql.BookSource;
44-
import org.springframework.graphql.ResponseHelper;
4544
import org.springframework.graphql.GraphQlSetup;
45+
import org.springframework.graphql.ResponseHelper;
4646
import org.springframework.graphql.data.query.QueryByExampleDataFetcher;
4747
import org.springframework.graphql.execution.RuntimeWiringConfigurer;
4848
import org.springframework.graphql.server.WebGraphQlHandler;
@@ -185,7 +185,8 @@ private static GraphQlSetup initGraphQlSetup(@Nullable QueryByExampleExecutor<?>
185185

186186
private WebGraphQlRequest request(String query) {
187187
return new WebGraphQlRequest(
188-
URI.create("/"), new HttpHeaders(), Collections.singletonMap("query", query), "1", null);
188+
URI.create("/"), new HttpHeaders(), null,
189+
Collections.singletonMap("query", query), "1", null);
189190
}
190191

191192

0 commit comments

Comments
 (0)