Move graphiql index.html to spring-graphql

Closes gh-209
This commit is contained in:
Rossen Stoyanchev
2021-12-02 15:53:36 +00:00
parent 5597f543bd
commit 6b1d5ebf64
5 changed files with 54 additions and 30 deletions

View File

@@ -108,8 +108,7 @@ public class GraphQlWebFluxAutoConfiguration {
handler::handleRequest);
if (properties.getGraphiql().isEnabled()) {
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
GraphiQlHandler graphiQlHandler = new GraphiQlHandler(graphQLPath, resource);
GraphiQlHandler graphiQlHandler = new GraphiQlHandler(graphQLPath);
builder = builder.GET(properties.getGraphiql().getPath(), graphiQlHandler::handleRequest);
}

View File

@@ -117,8 +117,7 @@ public class GraphQlWebMvcAutoConfiguration {
handler::handleRequest);
if (properties.getGraphiql().isEnabled()) {
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
GraphiQlHandler graphiQLHandler = new GraphiQlHandler(graphQLPath, resource);
GraphiQlHandler graphiQLHandler = new GraphiQlHandler(graphQLPath);
builder = builder.GET(properties.getGraphiql().getPath(), graphiQLHandler::handleRequest);
}

View File

@@ -1,96 +0,0 @@
<!--
~ Copyright 2002-2020 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!DOCTYPE html>
<html>
<head>
<style>
body {
height: 100%;
margin: 0;
width: 100%;
overflow: hidden;
}
#graphiql {
height: 100vh;
}
</style>
<!--
This GraphiQL example depends on Promise and fetch, which are available in
modern browsers, but can be "polyfilled" for older browsers.
GraphiQL itself depends on React DOM.
If you do not want to rely on a CDN, you can host these files locally or
include them directly in your favored resource bunder.
-->
<script
crossorigin
src="https://unpkg.com/react@16/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
></script>
<!--
These two files can be found in the npm module, however you may wish to
copy them directly into your environment, or perhaps include them in your
favored resource bundler.
-->
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" />
</head>
<body>
<div id="graphiql">Loading...</div>
<script
src="https://unpkg.com/graphiql/graphiql.min.js"
type="application/javascript"
></script>
<script>
function graphQLFetcher(path) {
return (graphQLParams, options = {}) => fetch(
`${location.protocol}//${location.host}${path}`,
{
method: 'post',
headers: {
...options.headers,
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(graphQLParams),
credentials: 'omit',
},
).then(function (response) {
return response.json().catch(function () {
return response.text();
});
});
}
const params = new URLSearchParams(window.location.search);
const path = params.get("path") || "/graphiql";
const gqlFetcher = graphQLFetcher(path);
ReactDOM.render(
React.createElement(GraphiQL, {
fetcher: gqlFetcher,
defaultVariableEditorOpen: true,
headerEditorEnabled: true
}),
document.getElementById('graphiql'),
);
</script>
</body>
</html>