Skip to content

Commit 054aaed

Browse files
committed
Document GraphiQL integration
Closes gh-403
1 parent 97990c2 commit 054aaed

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[[graphiql]]
2+
= GraphiQL
3+
4+
https://github.com/graphql/graphiql/tree/main/packages/graphiql#readme[GraphiQL] is a graphical interactive in-browser GraphQL IDE.
5+
It is very popular amongst developers as it makes it easy to explore and interactively develop GraphQL APIs.
6+
During development, a stock GraphiQL integration is often enough to help developers work on an API.
7+
In production, applications can require a custom GraphiQL build, that ships with a company logo or specific authentication support.
8+
9+
Spring for GraphQL ships with https://github.com/spring-projects/spring-graphql/blob/main/spring-graphql/src/main/resources/graphiql/index.html[a stock GraphiQL `index.html` page] that uses static resources hosted on the unpkg.com CDN.
10+
Spring Boot applications can easily {spring-boot-ref-docs}/web.html#web.graphql.graphiql[enable this page with a configuration property].
11+
12+
Your application may need a custom GraphiQL build if it requires a setup that doesn't rely on a CDN, or if you wish to customize the user interface.
13+
This can be done in two steps:
14+
15+
1. Configure and compile a GraphiQL build
16+
2. Expose the built GraphiQL instance through the Spring web infrastructure
17+
18+
[[graphiql.custombuild]]
19+
== Creating a custom GraphiQL build
20+
21+
This part is generally outside of the scope of this documentation, as there are several options for custom builds.
22+
You will find more information in the https://github.com/graphql/graphiql/tree/main/packages/graphiql#readme[official GraphiQL documentation].
23+
You can choose to copy the build result directly in your application resources.
24+
Alternatively, you can integrate the JavaScript build in your project as a separate module by leveraging Node.js https://github.com/node-gradle/gradle-node-plugin[Gradle] or https://github.com/eirslett/frontend-maven-plugin[Maven] build plugins.
25+
26+
27+
[[graphiql.configuration]]
28+
== Exposing a GraphiQL instance
29+
30+
Once a GraphiQL build is available on the classpath, you can expose it as an endpoint with the {spring-framework-ref-docs}/web/webmvc-functional.html#webmvc-fn-router-functions[functional web frameworks].
31+
32+
include::code:GraphiQlConfiguration[]
33+
<1> Load the GraphiQL page from the classpath (here, we are using the version shipped with Spring for GraphQL)
34+
<2> Configure a web handler for processing HTTP requests; you can implement a custom `HandlerFunction` depending on your use case
35+
<3> Finally, map the handler to a specific HTTP endpoint
36+
<4> Expose this new route through a `RouterFunction` bean
37+
38+
You might also need to configure your application to {spring-boot-ref-docs}/web.html#web.servlet.spring-mvc.static-content[serve the relevant static resources].

spring-graphql-docs/src/docs/asciidoc/index.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ include::includes/client.adoc[leveloffset=+1]
7171

7272

7373

74+
include::includes/graphiql.adoc[leveloffset=+1]
75+
76+
77+
7478
include::includes/testing.adoc[leveloffset=+1]
7579

7680

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2020-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.docs.graphiql.configuration;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.core.annotation.Order;
22+
import org.springframework.core.io.ClassPathResource;
23+
import org.springframework.graphql.server.webmvc.GraphiQlHandler;
24+
import org.springframework.web.servlet.function.RouterFunction;
25+
import org.springframework.web.servlet.function.RouterFunctions;
26+
import org.springframework.web.servlet.function.ServerResponse;
27+
28+
@Configuration
29+
public class GraphiQlConfiguration {
30+
31+
@Bean
32+
@Order(0)
33+
public RouterFunction<ServerResponse> graphiQlRouterFunction() {
34+
RouterFunctions.Builder builder = RouterFunctions.route();
35+
ClassPathResource graphiQlPage = new ClassPathResource("graphiql/index.html"); // <1>
36+
GraphiQlHandler graphiQLHandler = new GraphiQlHandler("/graphql", "", graphiQlPage); // <2>
37+
builder = builder.GET("/graphiql", graphiQLHandler::handleRequest); // <3>
38+
return builder.build(); // <4>
39+
}
40+
}

0 commit comments

Comments
 (0)