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

Commit 30b258a

Browse files
authored
Merge pull request #484 from graphql-java-kickstart/combine-headers
Add support for two graphiql headers configurations
2 parents 4319fcf + 6ff2907 commit 30b258a

File tree

10 files changed

+196
-93
lines changed

10 files changed

+196
-93
lines changed

.github/workflows/ci.yml

+29
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,32 @@ jobs:
6767
BINTRAY_USER: ${{ secrets.BINTRAY_USER }}
6868
BINTRAY_PASS: ${{ secrets.BINTRAY_PASSWORD }}
6969
run: ./gradlew artifactoryPublish -Dsnapshot=true -Dbuild.number=${{ env.GITHUB_RUN_NUMBER }}
70+
sonar:
71+
name: Sonar analysis
72+
needs: validation
73+
runs-on: ubuntu-latest
74+
steps:
75+
- uses: actions/checkout@v2
76+
with:
77+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
78+
- name: Set up JDK 11
79+
uses: actions/setup-java@v1
80+
with:
81+
java-version: 11
82+
- name: Cache SonarCloud packages
83+
uses: actions/cache@v1
84+
with:
85+
path: ~/.sonar/cache
86+
key: ${{ runner.os }}-sonar
87+
restore-keys: ${{ runner.os }}-sonar
88+
- name: Cache Gradle packages
89+
uses: actions/cache@v1
90+
with:
91+
path: ~/.gradle/caches
92+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
93+
restore-keys: ${{ runner.os }}-gradle
94+
- name: Build and analyze
95+
env:
96+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
97+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
98+
run: ./gradlew build jacocoTestReport sonarqube --info

.github/workflows/pull-request.yml

+37
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
branches-ignore:
55
- master
66
pull_request:
7+
types: [opened, synchronize, reopened]
78

89
jobs:
910
validation:
@@ -49,3 +50,39 @@ jobs:
4950
if: matrix.os == 'windows-latest'
5051
shell: cmd
5152
run: gradlew --info check
53+
build:
54+
name: Sonar analysis
55+
needs: validation
56+
runs-on: ubuntu-latest
57+
env:
58+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
59+
steps:
60+
- uses: actions/checkout@v2
61+
if: env.SONAR_TOKEN != null
62+
with:
63+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
64+
- name: Set up JDK 11
65+
if: env.SONAR_TOKEN != null
66+
uses: actions/setup-java@v1
67+
with:
68+
java-version: 11
69+
- name: Cache SonarCloud packages
70+
if: env.SONAR_TOKEN != null
71+
uses: actions/cache@v1
72+
with:
73+
path: ~/.sonar/cache
74+
key: ${{ runner.os }}-sonar
75+
restore-keys: ${{ runner.os }}-sonar
76+
- name: Cache Gradle packages
77+
if: env.SONAR_TOKEN != null
78+
uses: actions/cache@v1
79+
with:
80+
path: ~/.gradle/caches
81+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
82+
restore-keys: ${{ runner.os }}-gradle
83+
- name: Build and analyze
84+
if: env.SONAR_TOKEN != null
85+
env:
86+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
87+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
88+
run: ./gradlew build jacocoTestReport sonarqube --info

build.gradle

+20
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,22 @@ plugins {
2525
id 'net.researchgate.release' version "$LIB_RELEASE_PLUGIN_VER"
2626
id "org.springframework.boot" version "$LIB_SPRING_BOOT_VER" apply false
2727
id "com.jfrog.artifactory" version "4.15.1" apply false
28+
id "org.sonarqube" version "3.0"
29+
id "jacoco"
30+
}
31+
32+
sonarqube {
33+
properties {
34+
property "sonar.projectKey", "graphql-java-kickstart_graphql-spring-boot"
35+
property "sonar.organization", "graphql-java-kickstart"
36+
property "sonar.host.url", "https://sonarcloud.io"
37+
}
2838
}
2939

3040
subprojects {
3141
apply plugin: 'idea'
42+
apply plugin: 'jacoco'
43+
apply plugin: 'org.sonarqube'
3244
apply plugin: 'java'
3345
apply plugin: 'java-library'
3446
apply plugin: 'maven-publish'
@@ -76,6 +88,14 @@ subprojects {
7688
}
7789
}
7890

91+
jacocoTestReport {
92+
reports {
93+
xml.enabled = true
94+
html.enabled = false
95+
csv.enabled = false
96+
}
97+
}
98+
7999
idea {
80100
module {
81101
downloadJavadoc = true

example/src/main/resources/application.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ altair:
2525
graphiql:
2626
enabled: true
2727
cdn:
28-
enabled: true
28+
enabled: false
2929
version: 0.17.5
30+
headers:
31+
Test: TestHeader
32+
props:
33+
variables:
34+
headerEditorEnabled: true
35+
headers: '{ "Authorization": "SomeValue" }'
3036
voyager:
3137
enabled: true
3238
cdn:

gradle.properties

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ TARGET_COMPATIBILITY = 1.8
4040
### Dependencies
4141

4242
LIB_GRAPHQL_JAVA_VER = 15.0
43-
LIB_SPRING_BOOT_VER = 2.3.4.RELEASE
44-
LIB_GRAPHQL_SERVLET_VER = 10.0.0
45-
LIB_GRAPHQL_JAVA_TOOLS_VER = 6.2.0
43+
LIB_SPRING_BOOT_VER = 2.3.6.RELEASE
44+
LIB_GRAPHQL_SERVLET_VER = 10.1.0-SNAPSHOT
45+
LIB_GRAPHQL_JAVA_TOOLS_VER = 6.3.0
4646
LIB_GRAPHQL_ANNOTATIONS_VER = 8.2
4747
LIB_REFLECTIONS_VER = 0.9.11
4848
LIB_APACHE_COMMONS_TEXT=1.8

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

+18-23
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.nio.charset.Charset;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
import java.util.Properties;
511
import lombok.extern.slf4j.Slf4j;
612
import org.apache.commons.lang3.StringUtils;
7-
import org.apache.commons.lang3.text.StrSubstitutor;
13+
import org.apache.commons.text.StringSubstitutor;
814
import org.springframework.beans.factory.annotation.Autowired;
915
import org.springframework.core.env.Environment;
1016
import org.springframework.core.io.ClassPathResource;
11-
import org.springframework.http.MediaType;
1217
import org.springframework.security.web.csrf.CsrfToken;
1318
import org.springframework.util.StreamUtils;
1419
import org.springframework.web.bind.annotation.PathVariable;
1520
import org.springframework.web.bind.annotation.RequestParam;
1621

17-
import java.io.IOException;
18-
import java.io.InputStream;
19-
import java.nio.charset.Charset;
20-
import java.util.HashMap;
21-
import java.util.Map;
22-
import java.util.Properties;
23-
2422
/**
2523
* @author Andrew Potter
2624
*/
@@ -61,17 +59,10 @@ private void loadProps() throws IOException {
6159
private void loadHeaders() {
6260
PropertyGroupReader propertyReader = new PropertyGroupReader(environment, "graphiql.headers.");
6361
headerProperties = propertyReader.load();
64-
addIfAbsent(headerProperties, "Accept");
65-
addIfAbsent(headerProperties, "Content-Type");
6662
}
6763

68-
private void addIfAbsent(Properties headerProperties, String header) {
69-
if (!headerProperties.containsKey(header)) {
70-
headerProperties.setProperty(header, MediaType.APPLICATION_JSON_VALUE);
71-
}
72-
}
73-
74-
public byte[] graphiql(String contextPath, @PathVariable Map<String, String> params, Object csrf) {
64+
public byte[] graphiql(String contextPath, @PathVariable Map<String, String> params,
65+
Object csrf) {
7566
if (csrf != null) {
7667
CsrfToken csrfToken = (CsrfToken) csrf;
7768
headerProperties.setProperty(csrfToken.getHeaderName(), csrfToken.getToken());
@@ -83,7 +74,7 @@ public byte[] graphiql(String contextPath, @PathVariable Map<String, String> par
8374
contextPath + graphiQLProperties.getSTATIC().getBasePath()
8475
);
8576

86-
String populatedTemplate = StrSubstitutor.replace(template, replacements);
77+
String populatedTemplate = StringSubstitutor.replace(template, replacements);
8778
return populatedTemplate.getBytes(Charset.defaultCharset());
8879
}
8980

@@ -97,7 +88,8 @@ private Map<String, String> getReplacements(
9788
replacements.put("subscriptionsEndpoint", subscriptionsEndpoint);
9889
replacements.put("staticBasePath", staticBasePath);
9990
replacements.put("pageTitle", graphiQLProperties.getPageTitle());
100-
replacements.put("pageFavicon", getResourceUrl(staticBasePath, "favicon.ico", FAVICON_GRAPHQL_ORG));
91+
replacements
92+
.put("pageFavicon", getResourceUrl(staticBasePath, "favicon.ico", FAVICON_GRAPHQL_ORG));
10193
replacements.put("es6PromiseJsUrl", getResourceUrl(staticBasePath, "es6-promise.auto.min.js",
10294
joinCdnjsPath("es6-promise", "4.1.1", "es6-promise.auto.min.js")));
10395
replacements.put("fetchJsUrl", getResourceUrl(staticBasePath, "fetch.min.js",
@@ -123,9 +115,11 @@ private Map<String, String> getReplacements(
123115
log.error("Cannot serialize headers", e);
124116
}
125117
replacements
126-
.put("subscriptionClientTimeout", String.valueOf(graphiQLProperties.getSubscriptions().getTimeout() * 1000));
118+
.put("subscriptionClientTimeout",
119+
String.valueOf(graphiQLProperties.getSubscriptions().getTimeout() * 1000));
127120
replacements
128-
.put("subscriptionClientReconnect", String.valueOf(graphiQLProperties.getSubscriptions().isReconnect()));
121+
.put("subscriptionClientReconnect",
122+
String.valueOf(graphiQLProperties.getSubscriptions().isReconnect()));
129123
replacements.put("editorThemeCss", getEditorThemeCssURL());
130124
return replacements;
131125
}
@@ -161,7 +155,8 @@ private String joinJsDelivrPath(String library, String cdnVersion, String cdnFil
161155
return CDN_JSDELIVR_NET_NPM + library + "@" + cdnVersion + "/" + cdnFileName;
162156
}
163157

164-
private String constructGraphQlEndpoint(String contextPath, @RequestParam Map<String, String> params) {
158+
private String constructGraphQlEndpoint(String contextPath,
159+
@RequestParam Map<String, String> params) {
165160
String endpoint = graphiQLProperties.getEndpoint().getGraphql();
166161
for (Map.Entry<String, String> param : params.entrySet()) {
167162
endpoint = endpoint.replaceAll("\\{" + param.getKey() + "}", param.getValue());

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,22 @@
101101

102102
var headers = ${headers}
103103

104+
function addRequiredHeadersIfAbsent() {
105+
if (!headers['Accept']) {
106+
headers['Accept'] = 'application/json'
107+
}
108+
if (!headers['Content-Type']) {
109+
headers['Content-Type'] = 'application/json'
110+
}
111+
}
112+
104113
function onEditHeaders(newHeaders) {
105114
try {
106115
headers = JSON.parse(newHeaders)
107116
} catch(e) {
108117
headers = {}
109118
}
119+
addRequiredHeadersIfAbsent()
110120
}
111121

112122
// Defines a GraphQL fetcher using the fetch API. You're not required to
@@ -162,8 +172,13 @@
162172
props.onEditVariables = onEditVariables
163173
props.onEditOperationName = onEditOperationName
164174
props.onEditHeaders = onEditHeaders
175+
props.headers = props.headers || '{}'
176+
if (headers) {
177+
var newHeaders = Object.assign({}, JSON.parse(props.headers), headers)
178+
props.headers = JSON.stringify(newHeaders, undefined, 2)
179+
}
180+
onEditHeaders(props.headers)
165181

166-
console.debug(props)
167182
// Render <GraphiQL /> into the body.
168183
ReactDOM.render(
169184
React.createElement(GraphiQL, props),

graphql-kickstart-spring-support/src/main/java/graphql/kickstart/spring/AbstractGraphQLController.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ public Object graphqlPOST(
7575
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, "Could not process GraphQL request");
7676
}
7777

78-
@GetMapping(value = "${graphql.url:graphql}",
79-
produces = MediaType.APPLICATION_JSON_VALUE)
78+
@GetMapping(value = "${graphql.url:graphql}", produces = MediaType.APPLICATION_JSON_VALUE)
8079
public Object graphqlGET(
8180
@Nullable @RequestParam("query") String query,
8281
@Nullable @RequestParam(value = "operationName", required = false) String operationName,

graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/spring/web/boot/GraphQLWebAutoConfiguration.java

-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ public GraphQLConfiguration graphQLServletConfiguration(GraphQLInvocationInputFa
281281
.with(queryInvoker)
282282
.with(graphQLObjectMapper)
283283
.with(listeners)
284-
.with(graphQLServletProperties.isAsyncModeEnabled())
285284
.with(graphQLServletProperties.getSubscriptionTimeout())
286285
.with(batchInputPreProcessor)
287286
.with(graphQLServletProperties.getContextSetting())

0 commit comments

Comments
 (0)