Skip to content

Commit 7fc4df7

Browse files
committed
Add request attributes to WebGraphQlRequest
Closes gh-633
1 parent 66b3c82 commit 7fc4df7

14 files changed

+52
-25
lines changed

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
import java.net.URI;
21-
import java.util.Map;
21+
import java.util.Collections;
2222

2323
import reactor.core.publisher.Mono;
2424

@@ -77,10 +77,12 @@ public CodecConfigurer getCodecConfigurer() {
7777

7878
@Override
7979
protected Mono<ExecutionGraphQlResponse> executeInternal(ExecutionGraphQlRequest executionRequest) {
80-
String id = idGenerator.generateId().toString();
81-
Map<String, Object> body = executionRequest.toMap();
82-
WebGraphQlRequest webRequest = new WebGraphQlRequest(this.url, this.headers, null, body, id, null);
83-
return this.graphQlHandler.handleRequest(webRequest).cast(ExecutionGraphQlResponse.class);
80+
81+
WebGraphQlRequest request = new WebGraphQlRequest(
82+
this.url, this.headers, null, Collections.emptyMap(), executionRequest.toMap(),
83+
idGenerator.generateId().toString(), null);
84+
85+
return this.graphQlHandler.handleRequest(request).cast(ExecutionGraphQlResponse.class);
8486
}
8587

8688
}

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

+25-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.graphql.server;
1818

1919
import java.net.URI;
20+
import java.util.Collections;
2021
import java.util.Locale;
2122
import java.util.Map;
2223

@@ -56,29 +57,32 @@ public class WebGraphQlRequest extends DefaultExecutionGraphQlRequest implements
5657

5758
private final MultiValueMap<String, HttpCookie> cookies;
5859

60+
private final Map<String, Object> attributes;
61+
5962

6063
/**
6164
* Create an instance.
6265
* @deprecated as of 1.1.3 in favor of the constructor with cookies
6366
*/
6467
@Deprecated
6568
public WebGraphQlRequest(URI uri, HttpHeaders headers, Map<String, Object> body, String id, @Nullable Locale locale) {
66-
this(uri, headers, null, body, id, locale);
69+
this(uri, headers, null, Collections.emptyMap(), body, id, locale);
6770
}
6871

6972
/**
7073
* Create an instance.
7174
* @param uri the URL for the HTTP request or WebSocket handshake
7275
* @param headers the HTTP request headers
73-
* @param cookies the request cookies
76+
* @param cookies the HTTP request cookies
77+
* @param attributes request attributes
7478
* @param body the deserialized content of the GraphQL request
7579
* @param id an identifier for the GraphQL request
7680
* @param locale the locale from the HTTP request, if any
7781
* @since 1.1.3
7882
*/
7983
public WebGraphQlRequest(
8084
URI uri, HttpHeaders headers, @Nullable MultiValueMap<String, HttpCookie> cookies,
81-
Map<String, Object> body, String id, @Nullable Locale locale) {
85+
Map<String, Object> attributes, Map<String, Object> body, String id, @Nullable Locale locale) {
8286

8387
super(getKey("query", body), getKey("operationName", body), getKey("variables", body),
8488
getKey("extensions", body), id, locale);
@@ -88,7 +92,8 @@ public WebGraphQlRequest(
8892

8993
this.uri = UriComponentsBuilder.fromUri(uri).build(true);
9094
this.headers = headers;
91-
this.cookies = (cookies != null ? cookies : EMPTY_COOKIES);
95+
this.cookies = (cookies != null ? CollectionUtils.unmodifiableMultiValueMap(cookies) : EMPTY_COOKIES);
96+
this.attributes = Collections.unmodifiableMap(attributes);
9297
}
9398

9499
@SuppressWarnings("unchecked")
@@ -114,4 +119,20 @@ public HttpHeaders getHeaders() {
114119
return this.headers;
115120
}
116121

122+
/**
123+
* Return the cookies of the request of WebSocket handshake.
124+
* @since 1.1.3
125+
*/
126+
public MultiValueMap<String, HttpCookie> getCookies() {
127+
return this.cookies;
128+
}
129+
130+
/**
131+
* Return the request or WebSocket session attributes.
132+
* @since 1.1.3
133+
*/
134+
public Map<String, Object> getAttributes() {
135+
return this.attributes;
136+
}
137+
117138
}

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
import java.net.URI;
21+
import java.util.Collections;
2122
import java.util.Locale;
2223
import java.util.Map;
2324

@@ -49,14 +50,15 @@ public WebSocketGraphQlRequest(
4950
URI uri, HttpHeaders headers, Map<String, Object> body, String id, @Nullable Locale locale,
5051
WebSocketSessionInfo sessionInfo) {
5152

52-
this(uri, headers, null, body, id, locale, sessionInfo);
53+
this(uri, headers, null, Collections.emptyMap(), body, id, locale, sessionInfo);
5354
}
5455

5556
/**
5657
* Create an instance.
5758
* @param uri the URL for the HTTP request or WebSocket handshake
5859
* @param headers the HTTP request headers
59-
* @param cookies the request cookies
60+
* @param cookies the HTTP request cookies
61+
* @param attributes session attributes
6062
* @param body the deserialized content of the GraphQL request
6163
* @param id the id from the GraphQL over WebSocket {@code "subscribe"} message
6264
* @param locale the locale from the HTTP request, if any
@@ -65,9 +67,10 @@ public WebSocketGraphQlRequest(
6567
*/
6668
public WebSocketGraphQlRequest(
6769
URI uri, HttpHeaders headers, @Nullable MultiValueMap<String, HttpCookie> cookies,
68-
Map<String, Object> body, String id, @Nullable Locale locale, WebSocketSessionInfo sessionInfo) {
70+
Map<String, Object> attributes, Map<String, Object> body, String id, @Nullable Locale locale,
71+
WebSocketSessionInfo sessionInfo) {
6972

70-
super(uri, headers, cookies, body, id, locale);
73+
super(uri, headers, cookies, attributes, body, id, locale);
7174
Assert.notNull(sessionInfo, "WebSocketSessionInfo is required");
7275
this.sessionInfo = sessionInfo;
7376
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public Mono<ServerResponse> handleRequest(ServerRequest serverRequest) {
7575
.flatMap(body -> {
7676
WebGraphQlRequest graphQlRequest = new WebGraphQlRequest(
7777
serverRequest.uri(), serverRequest.headers().asHttpHeaders(),
78-
serverRequest.cookies(), body,
78+
serverRequest.cookies(), serverRequest.attributes(), body,
7979
serverRequest.exchange().getRequest().getId(),
8080
serverRequest.exchange().getLocaleContext().getLocale());
8181
if (logger.isDebugEnabled()) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public Mono<Void> handle(WebSocketSession session) {
149149
}
150150
WebSocketGraphQlRequest request = new WebSocketGraphQlRequest(
151151
handshakeInfo.getUri(), handshakeInfo.getHeaders(), handshakeInfo.getCookies(),
152-
payload, id, null, sessionInfo);
152+
handshakeInfo.getAttributes(), payload, id, null, sessionInfo);
153153
if (logger.isDebugEnabled()) {
154154
logger.debug("Executing: " + request);
155155
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public ServerResponse handleRequest(ServerRequest serverRequest) throws ServletE
9090

9191
WebGraphQlRequest graphQlRequest = new WebGraphQlRequest(
9292
serverRequest.uri(), serverRequest.headers().asHttpHeaders(), initCookies(serverRequest),
93-
readBody(serverRequest), this.idGenerator.generateId().toString(),
93+
serverRequest.attributes(), readBody(serverRequest), this.idGenerator.generateId().toString(),
9494
LocaleContextHolder.getLocale());
9595

9696
if (logger.isDebugEnabled()) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ private void handleInternal(WebSocketSession session, TextMessage webSocketMessa
203203
URI uri = session.getUri();
204204
Assert.notNull(uri, "Expected handshake url");
205205
HttpHeaders headers = session.getHandshakeHeaders();
206-
WebSocketGraphQlRequest request =
207-
new WebSocketGraphQlRequest(uri, headers, null, payload, id, null, state.getSessionInfo());
206+
WebSocketGraphQlRequest request = new WebSocketGraphQlRequest(
207+
uri, headers, null, session.getAttributes(), payload, id, null, state.getSessionInfo());
208208
if (logger.isDebugEnabled()) {
209209
logger.debug("Executing: " + request);
210210
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ private static GraphQlSetup initGraphQlSetup(
302302

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private static GraphQlSetup initGraphQlSetup(@Nullable QueryByExampleExecutor<?>
188188

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private static GraphQlSetup initGraphQlSetup(@Nullable QueryByExampleExecutor<?>
185185

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

Diff for: spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherReactiveMongoDbTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private static GraphQlSetup initGraphQlSetup(@Nullable ReactiveQueryByExampleExe
157157

158158
private WebGraphQlRequest request(String query) {
159159
return new WebGraphQlRequest(
160-
URI.create("/"), new HttpHeaders(), null,
160+
URI.create("/"), new HttpHeaders(), null, Collections.emptyMap(),
161161
Collections.singletonMap("query", query), "1", null);
162162
}
163163

Diff for: spring-graphql/src/test/java/org/springframework/graphql/observation/PropagationWebGraphQlInterceptorTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
import java.net.URI;
21+
import java.util.Collections;
2122
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
@@ -83,7 +84,7 @@ WebGraphQlRequest createRequest(Map<String, String> headers) {
8384
HttpHeaders httpHeaders = new HttpHeaders();
8485
headers.forEach(httpHeaders::set);
8586
return new WebGraphQlRequest(
86-
URI.create("https://example.org/graphql"), httpHeaders, null,
87+
URI.create("https://example.org/graphql"), httpHeaders, null, Collections.emptyMap(),
8788
Map.of("query", "{ notUsed }"), "1", null);
8889
}
8990

Diff for: spring-graphql/src/test/java/org/springframework/graphql/server/WebGraphQlHandlerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
public class WebGraphQlHandlerTests {
4343

4444
private static final WebGraphQlRequest webInput = new WebGraphQlRequest(
45-
URI.create("https://abc.org"), new HttpHeaders(), null,
45+
URI.create("https://abc.org"), new HttpHeaders(), null, Collections.emptyMap(),
4646
Collections.singletonMap("query", "{ greeting }"), "1", null);
4747

4848

Diff for: spring-graphql/src/test/java/org/springframework/graphql/server/WebGraphQlInterceptorTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
public class WebGraphQlInterceptorTests {
4141

4242
private static final WebGraphQlRequest webRequest = new WebGraphQlRequest(
43-
URI.create("http://abc.org"), new HttpHeaders(), null,
43+
URI.create("http://abc.org"), new HttpHeaders(), null, Collections.emptyMap(),
4444
Collections.singletonMap("query", "{ notUsed }"), "1", null);
4545

4646
@Test

0 commit comments

Comments
 (0)