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

Commit 281b8cf

Browse files
committed
Load css file for editor theme when provided fix #235
1 parent f38599f commit 281b8cf

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
44
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
55
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
6+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
67
import org.springframework.context.annotation.Bean;
78
import org.springframework.context.annotation.Configuration;
89
import org.springframework.web.servlet.DispatcherServlet;
@@ -13,10 +14,13 @@
1314
@Configuration
1415
@ConditionalOnWebApplication
1516
@ConditionalOnClass(DispatcherServlet.class)
17+
@EnableConfigurationProperties(GraphiQLProperties.class)
1618
public class GraphiQLAutoConfiguration {
19+
1720
@Bean
1821
@ConditionalOnProperty(value = "graphiql.enabled", havingValue = "true", matchIfMissing = true)
1922
GraphiQLController graphiQLController() {
2023
return new GraphiQLController();
2124
}
25+
2226
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public class GraphiQLController {
6363
@Autowired
6464
private Environment environment;
6565

66+
@Autowired
67+
private GraphiQLProperties graphiQLProperties;
68+
6669
private String template;
6770
private String props;
6871
private String headers;
@@ -141,9 +144,22 @@ private Map<String, String> getReplacements(String graphqlEndpoint, String subsc
141144
replacements.put("headers", headers);
142145
replacements.put("subscriptionClientTimeout", String.valueOf(subscriptionsTimeout * 1000));
143146
replacements.put("subscriptionClientReconnect", String.valueOf(subscriptionsReconnect));
147+
replacements.put("editorThemeCss", getEditorThemeCssURL());
144148
return replacements;
145149
}
146150

151+
private String getEditorThemeCssURL() {
152+
String theme = graphiQLProperties.getProps().getVariables().getEditorTheme();
153+
if (theme != null) {
154+
return String.format(
155+
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/%s/theme/%s.min.css",
156+
graphiQLProperties.getCodeMirror().getVersion(),
157+
theme.split("\\s")[0]
158+
);
159+
}
160+
return "";
161+
}
162+
147163
private String getResourceUrl(String staticBasePath, String staticFileName, String cdnUrl) {
148164
if (graphiqlCdnEnabled && StringUtils.isNotBlank(cdnUrl)) {
149165
return cdnUrl;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.oembedler.moon.graphiql.boot;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@Data
7+
@ConfigurationProperties("graphiql")
8+
class GraphiQLProperties {
9+
10+
private CodeMirror codeMirror = new CodeMirror();
11+
private Props props = new Props();
12+
13+
@Data
14+
static class CodeMirror {
15+
private String version = "5.47.0";
16+
}
17+
18+
@Data
19+
static class Props {
20+
21+
private Variables variables = new Variables();
22+
23+
@Data
24+
static class Variables {
25+
private String editorTheme;
26+
}
27+
}
28+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class PropsLoader {
1616

1717
private static final String GRAPHIQL_PROPS_PREFIX = "graphiql.props.";
1818
private static final String GRAPHIQL_PROPS_RESOURCES_PREFIX = GRAPHIQL_PROPS_PREFIX + "resources.";
19-
private static final String GRAPHIQL_PROPS_VALUES_PREFIX = GRAPHIQL_PROPS_PREFIX + "values.";
19+
private static final String GRAPHIQL_PROPS_VALUES_PREFIX = GRAPHIQL_PROPS_PREFIX + "variables.";
2020

2121
private Environment environment;
2222

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@
4141
Loading&hellip;
4242
</div>
4343
<script>
44+
var editorThemeCss = '${editorThemeCss}'
45+
if (editorThemeCss !== '') {
46+
var link = document.createElement( "link" );
47+
link.href = editorThemeCss;
48+
link.type = "text/css";
49+
link.rel = "stylesheet";
50+
link.media = "screen,print";
51+
52+
document.getElementsByTagName( "head" )[0].appendChild( link );
53+
}
54+
4455
// Parse the search string to get url parameters.
4556
var search = window.location.search;
4657
var parameters = {};
@@ -55,8 +66,7 @@
5566
// if variables was provided, try to format it.
5667
if (parameters.variables) {
5768
try {
58-
parameters.variables =
59-
JSON.stringify(JSON.parse(parameters.variables), null, 2);
69+
parameters.variables = JSON.stringify(JSON.parse(parameters.variables), null, 2);
6070
} catch (e) {
6171
// Do nothing, we want to display the invalid JSON as a string, rather
6272
// than present an error.
@@ -84,8 +94,7 @@
8494
var newSearch = '?' + Object.keys(parameters).filter(function (key) {
8595
return Boolean(parameters[key]);
8696
}).map(function (key) {
87-
return encodeURIComponent(key) + '=' +
88-
encodeURIComponent(parameters[key]);
97+
return encodeURIComponent(key) + '=' + encodeURIComponent(parameters[key]);
8998
}).join('&');
9099
history.replaceState(null, null, newSearch);
91100
}
@@ -114,8 +123,7 @@
114123
});
115124
}
116125

117-
var loc = window.location,
118-
newUri;
126+
var loc = window.location, newUri;
119127

120128
if (loc.protocol === "https:") {
121129
newUri = "wss:";
@@ -149,7 +157,7 @@
149157
// Render <GraphiQL /> into the body.
150158
ReactDOM.render(
151159
React.createElement(GraphiQL, props),
152-
document.body
160+
document.getElementById("splash")
153161
);
154162

155163
</script>

0 commit comments

Comments
 (0)