|
| 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