Skip to content

Commit c1ed773

Browse files
committed
Document minimum standalone setup for GraphQL support
Most Spring for GraphQL applications use Spring Boot as a way to auto-configure the required infrastructure for running GraphQL applications. This commit documents a minimal setup for Spring applications not relying on Spring Boot. This assumes an existing infrastructure for a Spring MVC application. Closes gh-606
1 parent 441b18d commit c1ed773

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

spring-graphql-docs/modules/ROOT/nav.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
* xref:graphiql.adoc[]
1313
* xref:testing.adoc[]
1414
* xref:boot-starter.adoc[]
15+
* xref:standalone-setup.adoc[]
1516
* xref:samples.adoc[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[[standalone-setup]]
2+
= Standalone Setup
3+
4+
If your application is not using Spring Boot, you are responsible for setting up the relevant Spring for GraphQL components.
5+
Assuming that your application is already configured for Spring MVC controllers, the minimum setup will require several beans.
6+
7+
include-code::GraphQlConfiguration[]
8+
<1> The `AnnotatedControllerConfigurer` bean is responsible for detecting GraphQL `@Controller` handlers.
9+
<2> The `ExecutionGraphQlService` processes GraphQL requests in a transport-agnostic fashion.
10+
<3> The `GraphQlSource` builder is the main configuration point. Explore its API for more options.
11+
<4> The `RouterFunction` exposes the GraphQL routes as {spring-framework-ref-docs}/web/webmvc-functional.html[functional endpoints].
12+
<5> You can then expose various transports (WebSocket, SSE, HTTP) over different routes.
13+
14+
Spring for GraphQL offers many other options and integrations with Spring projects.
15+
For more on this, you can xref:boot-starter.adoc[explore the Spring Boot auto-configurations].
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2020-2024 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.standalonesetup;
18+
19+
import java.util.List;
20+
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.core.io.ClassPathResource;
24+
import org.springframework.graphql.ExecutionGraphQlService;
25+
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
26+
import org.springframework.graphql.execution.ConnectionTypeDefinitionConfigurer;
27+
import org.springframework.graphql.execution.DefaultBatchLoaderRegistry;
28+
import org.springframework.graphql.execution.DefaultExecutionGraphQlService;
29+
import org.springframework.graphql.execution.GraphQlSource;
30+
import org.springframework.graphql.server.WebGraphQlHandler;
31+
import org.springframework.graphql.server.webmvc.GraphQlHttpHandler;
32+
import org.springframework.graphql.server.webmvc.GraphQlRequestPredicates;
33+
import org.springframework.graphql.server.webmvc.GraphiQlHandler;
34+
import org.springframework.web.servlet.function.RequestPredicate;
35+
import org.springframework.web.servlet.function.RouterFunction;
36+
import org.springframework.web.servlet.function.RouterFunctions;
37+
import org.springframework.web.servlet.function.ServerResponse;
38+
39+
@Configuration(proxyBeanMethods = false)
40+
public class GraphQlConfiguration {
41+
42+
@Bean // <1>
43+
public AnnotatedControllerConfigurer controllerConfigurer() {
44+
return new AnnotatedControllerConfigurer();
45+
}
46+
47+
@Bean // <2>
48+
public ExecutionGraphQlService executionGraphQlService(AnnotatedControllerConfigurer controllerConfigurer) {
49+
GraphQlSource graphQlSource = GraphQlSource.schemaResourceBuilder() // <3>
50+
.schemaResources(new ClassPathResource("graphql/schema.graphqls"))
51+
.configureTypeDefinitions(new ConnectionTypeDefinitionConfigurer())
52+
.configureRuntimeWiring(controllerConfigurer)
53+
.exceptionResolvers(List.of(controllerConfigurer.getExceptionResolver()))
54+
.build();
55+
DefaultBatchLoaderRegistry batchLoaderRegistry = new DefaultBatchLoaderRegistry();
56+
DefaultExecutionGraphQlService service = new DefaultExecutionGraphQlService(graphQlSource);
57+
service.addDataLoaderRegistrar(batchLoaderRegistry);
58+
return service;
59+
}
60+
61+
62+
@Bean // <4>
63+
public RouterFunction<ServerResponse> graphQlRouterFunction(ExecutionGraphQlService graphQlService) {
64+
WebGraphQlHandler webGraphQlHandler = WebGraphQlHandler.builder(graphQlService).build();
65+
GraphQlHttpHandler graphQlHttpHandler = new GraphQlHttpHandler(webGraphQlHandler);
66+
RequestPredicate graphQlPredicate = GraphQlRequestPredicates.graphQlHttp("/graphql");
67+
GraphiQlHandler graphiQlHandler = new GraphiQlHandler("/graphql", "");
68+
return RouterFunctions.route() // <5>
69+
.route(graphQlPredicate, graphQlHttpHandler::handleRequest)
70+
.GET("/graphiql", graphiQlHandler::handleRequest)
71+
.build();
72+
}
73+
}

0 commit comments

Comments
 (0)