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

Commit c4faf03

Browse files
authored
Merge pull request #777 from graphql-java-kickstart/feature/726-fix-graphiql-config
Feature/726 fix graphiql config
2 parents f3464ec + a5988b1 commit c4faf03

File tree

10 files changed

+100
-178
lines changed

10 files changed

+100
-178
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,30 @@ Available Spring Boot configuration parameters (either `application.yml`
193193
or `application.properties`):
194194

195195
```yaml
196-
graphiql:
197-
mapping: /graphiql
198-
endpoint:
199-
graphql: /graphql
200-
subscriptions: /subscriptions
201-
subscriptions:
202-
timeout: 30
203-
reconnect: false
204-
basePath: /
205-
enabled: true
206-
pageTitle: GraphiQL
207-
cdn:
208-
enabled: false
209-
version: latest
210-
props:
211-
resources:
212-
query: query.graphql
213-
defaultQuery: defaultQuery.graphql
214-
variables: variables.graphql
215-
variables:
216-
editorTheme: "solarized light"
217-
headers:
218-
Authorization: "Bearer <your-token>"
196+
graphql:
197+
graphiql:
198+
mapping: /graphiql
199+
endpoint:
200+
graphql: /graphql
201+
subscriptions: /subscriptions
202+
subscriptions:
203+
timeout: 30
204+
reconnect: false
205+
basePath: /
206+
enabled: true
207+
pageTitle: GraphiQL
208+
cdn:
209+
enabled: false
210+
version: latest
211+
props:
212+
resources:
213+
query: query.graphql
214+
defaultQuery: defaultQuery.graphql
215+
variables: variables.json
216+
variables:
217+
editorTheme: "solarized light"
218+
headers:
219+
Authorization: "Bearer <your-token>"
219220
```
220221

221222
By default GraphiQL is served from within the package. This can be configured to be served from CDN

graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/editor/PropertyGroupReader.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/editor/PropsLoader.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLAutoConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public class GraphiQLAutoConfiguration {
2121

2222
@Bean(name = "graphiQLController")
2323
@ConditionalOnWebApplication(type = SERVLET)
24-
ServletGraphiQLController servletGraphiQLController() {
25-
return new ServletGraphiQLController();
24+
ServletGraphiQLController servletGraphiQLController(GraphiQLProperties properties) {
25+
return new ServletGraphiQLController(properties);
2626
}
2727

2828
@Bean(name = "graphiQLController")
2929
@ConditionalOnMissingBean(ServletGraphiQLController.class)
3030
@ConditionalOnWebApplication(type = REACTIVE)
31-
ReactiveGraphiQLController reactiveGraphiQLController() {
32-
return new ReactiveGraphiQLController();
31+
ReactiveGraphiQLController reactiveGraphiQLController(GraphiQLProperties properties) {
32+
return new ReactiveGraphiQLController(properties);
3333
}
3434
}

graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLController.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package graphql.kickstart.autoconfigure.editor.graphiql;
22

3+
import static java.util.Objects.nonNull;
4+
35
import com.fasterxml.jackson.core.JsonProcessingException;
46
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import graphql.kickstart.autoconfigure.editor.PropertyGroupReader;
6-
import graphql.kickstart.autoconfigure.editor.PropsLoader;
7+
import graphql.kickstart.autoconfigure.editor.graphiql.GraphiQLProperties.Props.GraphiQLVariables;
8+
import graphql.kickstart.autoconfigure.editor.graphiql.GraphiQLProperties.Resources;
79
import java.io.IOException;
810
import java.io.InputStream;
911
import java.nio.charset.Charset;
12+
import java.nio.charset.StandardCharsets;
13+
import java.nio.file.Files;
14+
import java.util.Collections;
1015
import java.util.HashMap;
1116
import java.util.Map;
12-
import java.util.Properties;
17+
import java.util.Optional;
18+
import lombok.RequiredArgsConstructor;
1319
import lombok.extern.slf4j.Slf4j;
1420
import org.apache.commons.lang3.StringUtils;
1521
import org.apache.commons.text.StringSubstitutor;
16-
import org.springframework.beans.factory.annotation.Autowired;
17-
import org.springframework.core.env.Environment;
1822
import org.springframework.core.io.ClassPathResource;
1923
import org.springframework.security.web.csrf.CsrfToken;
2024
import org.springframework.util.StreamUtils;
@@ -23,25 +27,22 @@
2327

2428
/** @author Andrew Potter */
2529
@Slf4j
30+
@RequiredArgsConstructor
2631
public abstract class GraphiQLController {
2732

2833
private static final String CDNJS_CLOUDFLARE_COM_AJAX_LIBS = "//cdnjs.cloudflare.com/ajax/libs/";
2934
private static final String CDN_JSDELIVR_NET_NPM = "//cdn.jsdelivr.net/npm/";
3035
private static final String GRAPHIQL = "graphiql";
3136
private static final String FAVICON_GRAPHQL_ORG = "//graphql.org/img/favicon.png";
3237

33-
@Autowired private Environment environment;
34-
35-
@Autowired private GraphiQLProperties graphiQLProperties;
38+
private final GraphiQLProperties graphiQLProperties;
3639

3740
private String template;
3841
private String props;
39-
private Properties headerProperties;
4042

4143
public void onceConstructed() throws IOException {
4244
loadTemplate();
4345
loadProps();
44-
loadHeaders();
4546
}
4647

4748
private void loadTemplate() throws IOException {
@@ -52,35 +53,45 @@ private void loadTemplate() throws IOException {
5253
}
5354

5455
private void loadProps() throws IOException {
55-
props =
56-
new PropsLoader(environment, "graphiql.props.resources.", "graphiql.props.variables.")
57-
.load();
58-
}
59-
60-
private void loadHeaders() {
61-
PropertyGroupReader propertyReader = new PropertyGroupReader(environment, "graphiql.headers.");
62-
headerProperties = propertyReader.load();
56+
Resources resources = graphiQLProperties.getProps().getResources();
57+
GraphiQLVariables combinedVariables = graphiQLProperties.getProps().getVariables();
58+
if (nonNull(resources.getVariables())) {
59+
combinedVariables = combinedVariables.withVariables(getContent(resources.getVariables()));
60+
}
61+
if (nonNull(resources.getDefaultQuery())) {
62+
combinedVariables
63+
= combinedVariables.withDefaultQuery(getContent(resources.getDefaultQuery()));
64+
}
65+
if (nonNull(resources.getQuery())) {
66+
combinedVariables = combinedVariables.withQuery(getContent(resources.getQuery()));
67+
}
68+
this.props = new ObjectMapper().writeValueAsString(combinedVariables);
6369
}
6470

6571
public byte[] graphiql(
6672
String contextPath, @PathVariable Map<String, String> params, Object csrf) {
73+
Map<String, String> finalHeaders = Optional.ofNullable(graphiQLProperties.getHeaders())
74+
.orElseGet(Collections::emptyMap);
6775
if (csrf != null) {
6876
CsrfToken csrfToken = (CsrfToken) csrf;
69-
headerProperties.setProperty(csrfToken.getHeaderName(), csrfToken.getToken());
77+
finalHeaders = new HashMap<>(finalHeaders);
78+
finalHeaders.put(csrfToken.getHeaderName(), csrfToken.getToken());
7079
}
7180

7281
Map<String, String> replacements =
7382
getReplacements(
7483
constructGraphQlEndpoint(contextPath, params),
7584
contextPath + graphiQLProperties.getEndpoint().getSubscriptions(),
76-
contextPath + graphiQLProperties.getBasePath());
85+
contextPath + graphiQLProperties.getBasePath(),
86+
finalHeaders);
7787

7888
String populatedTemplate = StringSubstitutor.replace(template, replacements);
7989
return populatedTemplate.getBytes(Charset.defaultCharset());
8090
}
8191

8292
private Map<String, String> getReplacements(
83-
String graphqlEndpoint, String subscriptionsEndpoint, String staticBasePath) {
93+
String graphqlEndpoint, String subscriptionsEndpoint, String staticBasePath,
94+
Map<String, String> headers) {
8495
Map<String, String> replacements = new HashMap<>();
8596
replacements.put("graphqlEndpoint", graphqlEndpoint);
8697
replacements.put("subscriptionsEndpoint", subscriptionsEndpoint);
@@ -137,7 +148,7 @@ private Map<String, String> getReplacements(
137148
joinJsDelivrPath("graphiql-subscriptions-fetcher", "0.0.2", "browser/client.js")));
138149
replacements.put("props", props);
139150
try {
140-
replacements.put("headers", new ObjectMapper().writeValueAsString(headerProperties));
151+
replacements.put("headers", new ObjectMapper().writeValueAsString(headers));
141152
} catch (JsonProcessingException e) {
142153
log.error("Cannot serialize headers", e);
143154
}
@@ -191,4 +202,8 @@ private String constructGraphQlEndpoint(
191202
}
192203
return endpoint;
193204
}
205+
206+
private String getContent(final ClassPathResource resource) throws IOException {
207+
return new String(Files.readAllBytes(resource.getFile().toPath()), StandardCharsets.UTF_8);
208+
}
194209
}

0 commit comments

Comments
 (0)