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

Commit

Permalink
Added ability to server GraphiQL from CDN.
Browse files Browse the repository at this point in the history
  • Loading branch information
donbeave committed Jun 3, 2018
1 parent a78f74e commit ea7af58
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Sun Jun 03 22:34:30 HKT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -27,8 +28,11 @@ public class GraphiQLController {
@Value("${graphiql.pageTitle:GraphiQL}")
private String pageTitle;

@Value("${graphiql.version:0.11.11}")
private String graphiqlVersion;
@Value("${graphiql.cdn.enabled:false}")
private Boolean graphiqlCdnEnabled;

@Value("${graphiql.cdn.version:0.11.11}")
private String graphiqlCdnVersion;

@RequestMapping(value = "${graphiql.mapping:/graphiql}")
public void graphiql(HttpServletResponse response, @PathVariable Map<String, String> params) throws IOException {
Expand All @@ -37,11 +41,20 @@ public void graphiql(HttpServletResponse response, @PathVariable Map<String, Str
String template = StreamUtils.copyToString(new ClassPathResource("graphiql.html").getInputStream(), Charset.defaultCharset());
Map<String, String> replacements = new HashMap<>();

String graphiqlCssUrl = "./vendor/graphiql.min.css";
String graphiqlJsUrl = "./vendor/graphiql.min.js";

if (graphiqlCdnEnabled && !StringUtils.isEmpty(graphiqlCdnVersion)) {
graphiqlCssUrl = "//cdnjs.cloudflare.com/ajax/libs/graphiql/" + graphiqlCdnVersion + "/graphiql.min.css";
graphiqlJsUrl = "//cdnjs.cloudflare.com/ajax/libs/graphiql/" + graphiqlCdnVersion + "/graphiql.min.js";
}

String endpoint = constructGraphQlEndpoint(params);

replacements.put("graphqlEndpoint", endpoint);
replacements.put("pageTitle", pageTitle);
replacements.put("graphiqlVersion", graphiqlVersion);
replacements.put("graphiqlCssUrl", graphiqlCssUrl);
replacements.put("graphiqlJsUrl", graphiqlJsUrl);

response.getOutputStream().write(StrSubstitutor.replace(template, replacements).getBytes(Charset.defaultCharset()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
"type": "java.lang.String"
},
{
"name": "graphiql.version",
"name": "graphiql.cdn.enabled",
"defaultValue": false,
"type": "java.lang.Boolean"
},
{
"name": "graphiql.cdn.version",
"defaultValue": "0.11.11",
"type": "java.lang.String"
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
<script src="./vendor/react.min.js"></script>
<script src="./vendor/react-dom.min.js"></script>

<link rel="stylesheet" href="./vendor/graphiql.min.css"/>
<script src="./vendor/graphiql.min.js"></script>
<link rel="stylesheet" href="${graphiqlCssUrl}"/>
<script src="${graphiqlJsUrl}"></script>
</head>
<body>
<div id="splash">
Expand Down

0 comments on commit ea7af58

Please sign in to comment.