Move graphiql index.html to spring-graphql
Closes gh-209
This commit is contained in:
@@ -20,45 +20,58 @@ import java.net.URI;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
/**
|
||||
* Spring WebFlux functional handler that renders a GraphiQl UI page.
|
||||
* Spring WebFlux handler to serve a GraphiQl UI page.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class GraphiQlHandler {
|
||||
|
||||
private final String graphQlPath;
|
||||
|
||||
private final Resource graphiQlResource;
|
||||
private final Resource htmlResource;
|
||||
|
||||
|
||||
/**
|
||||
* Create an instance.
|
||||
* Constructor that serves the default {@code graphiql/index.html} included
|
||||
* in the {@code spring-graphql} module.
|
||||
* @param graphQlPath the path to the GraphQL endpoint
|
||||
* @param graphiQlResource the GraphiQL page
|
||||
*/
|
||||
public GraphiQlHandler(String graphQlPath, Resource graphiQlResource) {
|
||||
public GraphiQlHandler(String graphQlPath) {
|
||||
this(graphQlPath, new ClassPathResource("graphiql/index.html"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with the HTML page to serve.
|
||||
* @param graphQlPath the path to the GraphQL endpoint
|
||||
* @param htmlResource the GraphiQL page to serve
|
||||
*/
|
||||
public GraphiQlHandler(String graphQlPath, Resource htmlResource) {
|
||||
this.graphQlPath = graphQlPath;
|
||||
this.graphiQlResource = graphiQlResource;
|
||||
this.htmlResource = htmlResource;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle the request, serving the GraphiQL page as HTML or adding a "path"
|
||||
* param and redirecting back to the same URL if needed.
|
||||
* Render the GraphiQL page as "text/html", or if the "path" query parameter
|
||||
* is missing, add it and redirect back to the same URL.
|
||||
*/
|
||||
public Mono<ServerResponse> handleRequest(ServerRequest request) {
|
||||
if (!request.queryParam("path").isPresent()) {
|
||||
URI url = request.uriBuilder().queryParam("path", this.graphQlPath).build();
|
||||
return ServerResponse.temporaryRedirect(url).build();
|
||||
}
|
||||
return ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(this.graphiQlResource);
|
||||
return (request.queryParam("path").isPresent() ?
|
||||
ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(this.htmlResource) :
|
||||
ServerResponse.temporaryRedirect(getRedirectUrl(request)).build());
|
||||
}
|
||||
|
||||
private URI getRedirectUrl(ServerRequest request) {
|
||||
return request.uriBuilder().queryParam("path", this.graphQlPath).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,45 +18,58 @@ package org.springframework.graphql.web.webmvc;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.servlet.function.ServerRequest;
|
||||
import org.springframework.web.servlet.function.ServerResponse;
|
||||
|
||||
/**
|
||||
* Spring MVC functional handler that renders a GraphiQl UI page.
|
||||
* Spring MVC handler to serve a GraphiQl UI page.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class GraphiQlHandler {
|
||||
|
||||
private final String graphQlPath;
|
||||
|
||||
private final Resource graphiQlResource;
|
||||
private final Resource htmlResource;
|
||||
|
||||
|
||||
/**
|
||||
* Create an instance.
|
||||
* Constructor that serves the default {@code graphiql/index.html} included
|
||||
* in the {@code spring-graphql} module.
|
||||
* @param graphQlPath the path to the GraphQL endpoint
|
||||
* @param graphiQlResource the GraphiQL page
|
||||
*/
|
||||
public GraphiQlHandler(String graphQlPath, Resource graphiQlResource) {
|
||||
public GraphiQlHandler(String graphQlPath) {
|
||||
this(graphQlPath, new ClassPathResource("graphiql/index.html"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with the HTML page to serve.
|
||||
* @param graphQlPath the path to the GraphQL endpoint
|
||||
* @param htmlResource the GraphiQL page to serve
|
||||
*/
|
||||
public GraphiQlHandler(String graphQlPath, Resource htmlResource) {
|
||||
this.graphQlPath = graphQlPath;
|
||||
this.graphiQlResource = graphiQlResource;
|
||||
this.htmlResource = htmlResource;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle the request, serving the GraphiQL page as HTML or adding a "path"
|
||||
* param and redirecting back to the same URL if needed.
|
||||
* Render the GraphiQL page as "text/html", or if the "path" query parameter
|
||||
* is missing, add it and redirect back to the same URL.
|
||||
*/
|
||||
public ServerResponse handleRequest(ServerRequest request) {
|
||||
if (!request.param("path").isPresent()) {
|
||||
URI url = request.uriBuilder().queryParam("path", this.graphQlPath).build();
|
||||
return ServerResponse.temporaryRedirect(url).build();
|
||||
}
|
||||
return ServerResponse.ok().contentType(MediaType.TEXT_HTML).body(this.graphiQlResource);
|
||||
return (request.param("path").isPresent() ?
|
||||
ServerResponse.ok().contentType(MediaType.TEXT_HTML).body(this.htmlResource) :
|
||||
ServerResponse.temporaryRedirect(getRedirectUrl(request)).build());
|
||||
}
|
||||
|
||||
private URI getRedirectUrl(ServerRequest request) {
|
||||
return request.uriBuilder().queryParam("path", this.graphQlPath).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
96
spring-graphql/src/main/resources/graphiql/index.html
Normal file
96
spring-graphql/src/main/resources/graphiql/index.html
Normal file
@@ -0,0 +1,96 @@
|
||||
<!--
|
||||
~ 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>
|
||||
Reference in New Issue
Block a user