Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit ee8c56f

Browse files
authored
Merge pull request #412 from 0ffer/voyager-url-templates
Add graphQL endpoint template resolve as in graphiql controller.
2 parents 6596787 + 94e63af commit ee8c56f

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

voyager-spring-boot-autoconfigure/src/main/java/graphql/kickstart/voyager/boot/ReactiveVoyagerController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import org.springframework.http.ResponseEntity;
66
import org.springframework.stereotype.Controller;
77
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
89

910
import java.io.IOException;
11+
import java.util.Map;
1012

1113
/**
1214
* @author Max David Günther
@@ -17,9 +19,9 @@ public class ReactiveVoyagerController {
1719
private VoyagerIndexHtmlTemplate indexTemplate;
1820

1921
@GetMapping(path = "${voyager.mapping:/voyager}")
20-
public ResponseEntity<String> voyager() throws IOException {
22+
public ResponseEntity<String> voyager(@PathVariable Map<String, String> params) throws IOException {
2123
// no context path in spring-webflux
22-
String indexHtmlContent = indexTemplate.fillIndexTemplate("");
24+
String indexHtmlContent = indexTemplate.fillIndexTemplate("", params);
2325
return ResponseEntity.ok()
2426
.contentType(MediaType.valueOf("text/html; charset=UTF-8"))
2527
.body(indexHtmlContent);

voyager-spring-boot-autoconfigure/src/main/java/graphql/kickstart/voyager/boot/VoyagerController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import org.springframework.http.MediaType;
55
import org.springframework.http.ResponseEntity;
66
import org.springframework.stereotype.Controller;
7+
import org.springframework.web.bind.annotation.PathVariable;
78
import org.springframework.web.bind.annotation.RequestMapping;
89

910
import javax.servlet.http.HttpServletRequest;
1011
import java.io.IOException;
12+
import java.util.Map;
1113

1214
/**
1315
* @author Max David Günther
@@ -18,9 +20,10 @@ public class VoyagerController {
1820
private VoyagerIndexHtmlTemplate indexTemplate;
1921

2022
@RequestMapping(value = "${voyager.mapping:/voyager}")
21-
public ResponseEntity<String> voyager(HttpServletRequest request) throws IOException {
23+
public ResponseEntity<String> voyager(HttpServletRequest request,
24+
@PathVariable Map<String, String> params) throws IOException {
2225
String contextPath = request.getContextPath();
23-
String indexHtmlContent = indexTemplate.fillIndexTemplate(contextPath);
26+
String indexHtmlContent = indexTemplate.fillIndexTemplate(contextPath, params);
2427
return ResponseEntity.ok()
2528
.contentType(MediaType.valueOf("text/html; charset=UTF-8"))
2629
.body(indexHtmlContent);

voyager-spring-boot-autoconfigure/src/main/java/graphql/kickstart/voyager/boot/VoyagerIndexHtmlTemplate.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package graphql.kickstart.voyager.boot;
22

33
import org.apache.commons.lang3.StringUtils;
4-
import org.apache.commons.lang3.text.StrSubstitutor;
4+
import org.apache.commons.text.StringSubstitutor;
55
import org.springframework.beans.factory.annotation.Value;
66
import org.springframework.core.io.ClassPathResource;
77
import org.springframework.util.StreamUtils;
8+
import org.springframework.web.bind.annotation.RequestParam;
89

910
import java.io.IOException;
1011
import java.nio.charset.Charset;
@@ -36,10 +37,10 @@ public class VoyagerIndexHtmlTemplate {
3637
@Value("${voyager.cdn.version:1.0.0-rc.26}")
3738
private String voyagerCdnVersion;
3839

39-
public String fillIndexTemplate(String contextPath) throws IOException {
40+
public String fillIndexTemplate(String contextPath, Map<String, String> params) throws IOException {
4041
String template = StreamUtils.copyToString(new ClassPathResource("voyager.html").getInputStream(), Charset.defaultCharset());
4142
Map<String, String> replacements = new HashMap<>();
42-
replacements.put("graphqlEndpoint", contextPath + graphqlEndpoint);
43+
replacements.put("graphqlEndpoint", constructGraphQlEndpoint(contextPath, params));
4344
replacements.put("pageTitle", pageTitle);
4445
replacements.put("pageFavicon", getResourceUrl(staticBasePath, "favicon.ico", FAVICON_APIS_GURU));
4546
replacements.put("es6PromiseJsUrl", getResourceUrl(staticBasePath, "es6-promise.auto.min.js",
@@ -58,7 +59,18 @@ public String fillIndexTemplate(String contextPath) throws IOException {
5859
joinJsDelivrPath(VOYAGER, voyagerCdnVersion, "dist/voyager.worker.min.js")));
5960
replacements.put("contextPath", contextPath);
6061

61-
return StrSubstitutor.replace(template, replacements);
62+
return StringSubstitutor.replace(template, replacements);
63+
}
64+
65+
private String constructGraphQlEndpoint(String contextPath, @RequestParam Map<String, String> params) {
66+
String endpoint = graphqlEndpoint;
67+
for (Map.Entry<String, String> param : params.entrySet()) {
68+
endpoint = endpoint.replaceAll("\\{" + param.getKey() + "}", param.getValue());
69+
}
70+
if (StringUtils.isNotBlank(contextPath) && !endpoint.startsWith(contextPath)) {
71+
return contextPath + endpoint;
72+
}
73+
return endpoint;
6274
}
6375

6476
private String getResourceUrl(String staticBasePath, String staticFileName, String cdnUrl) {

0 commit comments

Comments
 (0)