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

Commit 7b9bdcf

Browse files
authored
Merge pull request #118 from arnaudmimart/graphiql-endpoints-configuration
Graphiql endpoints configuration
2 parents e7d185d + ccafeb2 commit 7b9bdcf

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ Available Spring Boot configuration parameters (either `application.yml` or `app
106106
```yaml
107107
graphiql:
108108
mapping: /graphiql
109-
endpoint: /graphql
109+
endpoint:
110+
graphql: /graphql
111+
subscriptions: /subscriptions
112+
static:
113+
basePath: /
110114
enabled: true
111115
pageTitle: GraphiQL
112116
cdn:

graphiql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphiql/boot/GraphiQLController.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@
3232
public class GraphiQLController {
3333

3434
private static final String CDNJS_CLOUDFLARE_COM_AJAX_LIBS_GRAPHIQL = "//cdnjs.cloudflare.com/ajax/libs/graphiql/";
35-
@Value("${graphiql.endpoint:/graphql}")
35+
@Value("${graphiql.endpoint.graphql:/graphql}")
3636
private String graphqlEndpoint;
3737

38+
@Value("${graphiql.endpoint.subscriptions:/subscriptions}")
39+
private String subscriptionsEndpoint;
40+
41+
@Value("${graphiql.static.basePath:/}")
42+
private String staticBasePath;
43+
3844
@Value("${graphiql.pageTitle:GraphiQL}")
3945
private String pageTitle;
4046

@@ -86,17 +92,21 @@ private void addIfAbsent(Properties headerProperties, String header) {
8692
public void graphiql(HttpServletRequest request, HttpServletResponse response, @PathVariable Map<String, String> params) throws IOException {
8793
response.setContentType("text/html; charset=UTF-8");
8894

89-
String endpoint = constructGraphQlEndpoint(request, params);
90-
Map<String, String> replacements = getReplacements(endpoint);
95+
Map<String, String> replacements = getReplacements(
96+
constructGraphQlEndpoint(request, params),
97+
request.getContextPath() + subscriptionsEndpoint,
98+
request.getContextPath() + staticBasePath
99+
);
91100

92101
String populatedTemplate = StrSubstitutor.replace(template, replacements);
93-
populatedTemplate = addContextPathIfEnabled(request, populatedTemplate);
94102
response.getOutputStream().write(populatedTemplate.getBytes(Charset.defaultCharset()));
95103
}
96104

97-
private Map<String, String> getReplacements(String endpoint) {
105+
private Map<String, String> getReplacements(String graphqlEndpoint, String subscriptionsEndpoint, String staticBasePath) {
98106
Map<String, String> replacements = new HashMap<>();
99-
replacements.put("graphqlEndpoint", endpoint);
107+
replacements.put("graphqlEndpoint", graphqlEndpoint);
108+
replacements.put("subscriptionsEndpoint", subscriptionsEndpoint);
109+
replacements.put("staticBasePath", staticBasePath);
100110
replacements.put("pageTitle", pageTitle);
101111
replacements.put("graphiqlCssUrl", graphiqlUrl("graphiql.min.css"));
102112
replacements.put("graphiqlJsUrl", graphiqlUrl("graphiql.min.js"));
@@ -109,17 +119,7 @@ private String graphiqlUrl(String filename) {
109119
if (graphiqlCdnEnabled && StringUtils.isNotBlank(graphiqlCdnVersion)) {
110120
return CDNJS_CLOUDFLARE_COM_AJAX_LIBS_GRAPHIQL + graphiqlCdnVersion + "/" + filename;
111121
}
112-
return "/vendor/" + filename;
113-
}
114-
115-
private String addContextPathIfEnabled(HttpServletRequest request, String populatedTemplate) {
116-
if (StringUtils.isNotBlank(request.getContextPath())) {
117-
String vendorPathWithContext = String.format("%s/vendor", request.getContextPath());
118-
populatedTemplate = populatedTemplate
119-
.replaceAll("src=\"/vendor", "src=\"" + vendorPathWithContext)
120-
.replaceAll("href=\"/vendor", "href=\"" + vendorPathWithContext);
121-
}
122-
return populatedTemplate;
122+
return staticBasePath + "vendor/" + filename;
123123
}
124124

125125
private String constructGraphQlEndpoint(HttpServletRequest request, @RequestParam Map<String, String> params) {

graphiql-spring-boot-autoconfigure/src/main/resources/graphiql.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
}
2525
</style>
2626

27-
<script src="/vendor/es6-promise.auto.min.js"></script>
28-
<script src="/vendor/fetch.min.js"></script>
29-
<script src="/vendor/react.min.js"></script>
30-
<script src="/vendor/react-dom.min.js"></script>
27+
<script src="${staticBasePath}vendor/es6-promise.auto.min.js"></script>
28+
<script src="${staticBasePath}vendor/fetch.min.js"></script>
29+
<script src="${staticBasePath}vendor/react.min.js"></script>
30+
<script src="${staticBasePath}vendor/react-dom.min.js"></script>
3131

3232
<link rel="stylesheet" href="${graphiqlCssUrl}"/>
3333
<script src="${graphiqlJsUrl}"></script>
@@ -121,7 +121,7 @@
121121
newUri = "ws:";
122122
}
123123
newUri += "//" + loc.host;
124-
newUri += "/subscriptions";
124+
newUri += "${subscriptionsEndpoint}";
125125

126126
var subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient(newUri, { reconnect: true });
127127
var subscriptionsFetcher = window.GraphiQLSubscriptionsFetcher.graphQLFetcher(subscriptionsClient, graphQLFetcher);

0 commit comments

Comments
 (0)