Skip to content

Commit c592ee0

Browse files
committed
GraphiQlHandler supports path variables
Closes gh-478
1 parent a116505 commit c592ee0

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2022 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.
@@ -86,7 +86,7 @@ private URI getRedirectUrl(ServerRequest request) {
8686
String wsPathQueryParam = applyContextPath(request, this.graphQlWsPath);
8787
builder.queryParam("wsPath", wsPathQueryParam);
8888
}
89-
return builder.build();
89+
return builder.build(request.pathVariables());
9090
}
9191

9292
private String applyContextPath(ServerRequest request, String path) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2022 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.
@@ -85,7 +85,7 @@ private URI getRedirectUrl(ServerRequest request) {
8585
String wsPathQueryParam = applyPathPrefix(request, this.graphQlWsPath);
8686
builder.queryParam("wsPath", wsPathQueryParam);
8787
}
88-
return builder.build();
88+
return builder.build(request.pathVariables());
8989
}
9090

9191
private String applyPathPrefix(ServerRequest request, String path) {

spring-graphql/src/test/java/org/springframework/graphql/server/webflux/GraphiQlHandlerTests.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.charset.StandardCharsets;
2020
import java.util.Collections;
2121
import java.util.List;
22+
import java.util.Map;
2223

2324
import org.junit.jupiter.api.Test;
2425

@@ -31,9 +32,11 @@
3132
import org.springframework.http.codec.HttpMessageWriter;
3233
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
3334
import org.springframework.mock.web.server.MockServerWebExchange;
35+
import org.springframework.web.reactive.function.server.RouterFunctions;
3436
import org.springframework.web.reactive.function.server.ServerRequest;
3537
import org.springframework.web.reactive.function.server.ServerResponse;
3638
import org.springframework.web.reactive.result.view.ViewResolver;
39+
import org.springframework.web.util.UriComponentsBuilder;
3740

3841
import static org.assertj.core.api.Assertions.assertThat;
3942

@@ -45,10 +48,13 @@ class GraphiQlHandlerTests {
4548

4649
private static final List<HttpMessageReader<?>> MESSAGE_READERS = Collections.emptyList();
4750

48-
private final GraphiQlHandler handler = new GraphiQlHandler("/graphql", null,
49-
new ByteArrayResource("GRAPHIQL".getBytes(StandardCharsets.UTF_8)));
51+
private final GraphiQlHandler handler = initHandler("/graphql");
5052

5153

54+
private static GraphiQlHandler initHandler(String path) {
55+
return new GraphiQlHandler(path, null, new ByteArrayResource("GRAPHIQL".getBytes(StandardCharsets.UTF_8)));
56+
}
57+
5258
@Test
5359
void shouldRedirectWithPathQueryParameter() {
5460
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("/graphiql").build();
@@ -73,6 +79,25 @@ void shouldRedirectWithPathAndWsPathQueryParameter() {
7379
assertThat(response.headers().getLocation().toASCIIString()).isEqualTo("/graphiql?path=/graphql&wsPath=/graphql");
7480
}
7581

82+
@Test // gh-478
83+
void shouldRedirectWithPathVariables() {
84+
Map<String, Object> pathVariables = Collections.singletonMap("envId", "123");
85+
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString("/env/{envId}/graphiql");
86+
String path = uriBuilder.build(pathVariables).toString();
87+
88+
MockServerHttpRequest httpRequest = MockServerHttpRequest.get(path).build();
89+
MockServerWebExchange exchange = MockServerWebExchange.from(httpRequest);
90+
exchange.getAttributes().put(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE, pathVariables);
91+
ServerRequest request = ServerRequest.create(exchange, MESSAGE_READERS);
92+
93+
GraphiQlHandler graphiQlHandler = initHandler(uriBuilder.build().toString());
94+
ServerResponse response = graphiQlHandler.handleRequest(request).block();
95+
96+
assertThat(response.statusCode()).isEqualTo(HttpStatus.TEMPORARY_REDIRECT);
97+
assertThat(response.headers().getLocation()).isNotNull();
98+
assertThat(response.headers().getLocation().toASCIIString()).isEqualTo(path + "?path=" + path);
99+
}
100+
76101
@Test
77102
void shouldServeGraphiQlHtmlResource() {
78103
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("/graphiql").queryParam("path", "/graphql").build();

spring-graphql/src/test/java/org/springframework/graphql/server/webmvc/GraphiQlHandlerTests.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.charset.StandardCharsets;
2222
import java.util.Collections;
2323
import java.util.List;
24+
import java.util.Map;
2425

2526
import javax.servlet.ServletException;
2627

@@ -33,8 +34,10 @@
3334
import org.springframework.http.converter.ResourceHttpMessageConverter;
3435
import org.springframework.mock.web.MockHttpServletRequest;
3536
import org.springframework.mock.web.MockHttpServletResponse;
37+
import org.springframework.web.servlet.function.RouterFunctions;
3638
import org.springframework.web.servlet.function.ServerRequest;
3739
import org.springframework.web.servlet.function.ServerResponse;
40+
import org.springframework.web.util.UriComponentsBuilder;
3841

3942
import static org.assertj.core.api.Assertions.assertThat;
4043

@@ -47,8 +50,13 @@ class GraphiQlHandlerTests {
4750

4851
private static final List<HttpMessageConverter<?>> MESSAGE_READERS = Collections.emptyList();
4952

50-
private GraphiQlHandler handler = new GraphiQlHandler("/graphql", null,
51-
new ByteArrayResource("GRAPHIQL".getBytes(StandardCharsets.UTF_8)));
53+
private final GraphiQlHandler handler = initHandler("/graphql");
54+
55+
56+
private static GraphiQlHandler initHandler(String path) {
57+
return new GraphiQlHandler(path, null, new ByteArrayResource("GRAPHIQL".getBytes(StandardCharsets.UTF_8)));
58+
}
59+
5260

5361
@Test
5462
void shouldRedirectWithPathQueryParameter() {
@@ -57,7 +65,8 @@ void shouldRedirectWithPathQueryParameter() {
5765
ServerResponse response = this.handler.handleRequest(request);
5866
assertThat(response.statusCode()).isEqualTo(HttpStatus.TEMPORARY_REDIRECT);
5967
assertThat(response.headers().getLocation()).isNotNull();
60-
assertThat(response.headers().getLocation().toASCIIString()).isEqualTo("http://localhost/graphiql?path=/graphql");
68+
assertThat(response.headers().getLocation().toASCIIString())
69+
.isEqualTo("http://localhost/graphiql?path=/graphql");
6170
}
6271

6372
@Test
@@ -69,7 +78,27 @@ void shouldRedirectWithPathAndWsPathQueryParameter() {
6978
ServerResponse response = wsHandler.handleRequest(request);
7079
assertThat(response.statusCode()).isEqualTo(HttpStatus.TEMPORARY_REDIRECT);
7180
assertThat(response.headers().getLocation()).isNotNull();
72-
assertThat(response.headers().getLocation().toASCIIString()).isEqualTo("http://localhost/graphiql?path=/graphql&wsPath=/graphql");
81+
assertThat(response.headers().getLocation().toASCIIString())
82+
.isEqualTo("http://localhost/graphiql?path=/graphql&wsPath=/graphql");
83+
}
84+
85+
@Test // gh-478
86+
void shouldRedirectWithPathVariables() {
87+
Map<String, Object> pathVariables = Collections.singletonMap("envId", "123");
88+
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString("/env/{envId}/graphiql");
89+
String path = uriBuilder.build(pathVariables).toString();
90+
91+
MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", path);
92+
ServerRequest request = ServerRequest.create(servletRequest, MESSAGE_READERS);
93+
servletRequest.setAttribute(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE, pathVariables);
94+
95+
GraphiQlHandler graphiQlHandler = initHandler(uriBuilder.build().toString());
96+
ServerResponse response = graphiQlHandler.handleRequest(request);
97+
98+
assertThat(response.statusCode()).isEqualTo(HttpStatus.TEMPORARY_REDIRECT);
99+
assertThat(response.headers().getLocation()).isNotNull();
100+
assertThat(response.headers().getLocation().toASCIIString())
101+
.isEqualTo("http://localhost" + path + "?path=" + path);
73102
}
74103

75104
@Test

0 commit comments

Comments
 (0)