スタンドアロンセットアップ

アプリケーションが Spring Boot を使用していない場合は、関連する Spring for GraphQL コンポーネントを設定する必要があります。アプリケーションがすでに Spring MVC コントローラー用に構成されていると仮定すると、最小限の設定ではいくつかの Bean が必要になります。

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.graphql.ExecutionGraphQlService;
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
import org.springframework.graphql.execution.ConnectionTypeDefinitionConfigurer;
import org.springframework.graphql.execution.DefaultBatchLoaderRegistry;
import org.springframework.graphql.execution.DefaultExecutionGraphQlService;
import org.springframework.graphql.execution.GraphQlSource;
import org.springframework.graphql.server.WebGraphQlHandler;
import org.springframework.graphql.server.webmvc.GraphQlHttpHandler;
import org.springframework.graphql.server.webmvc.GraphQlRequestPredicates;
import org.springframework.graphql.server.webmvc.GraphiQlHandler;
import org.springframework.web.servlet.function.RequestPredicate;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;

@Configuration(proxyBeanMethods = false)
public class GraphQlConfiguration {

	@Bean (1)
	public AnnotatedControllerConfigurer controllerConfigurer() {
		return new AnnotatedControllerConfigurer();
	}

	@Bean (2)
	public ExecutionGraphQlService executionGraphQlService(AnnotatedControllerConfigurer controllerConfigurer) {
		GraphQlSource graphQlSource = GraphQlSource.schemaResourceBuilder() (3)
				.schemaResources(new ClassPathResource("graphql/schema.graphqls"))
				.configureTypeDefinitions(new ConnectionTypeDefinitionConfigurer())
				.configureRuntimeWiring(controllerConfigurer)
				.exceptionResolvers(List.of(controllerConfigurer.getExceptionResolver()))
				.build();
		DefaultBatchLoaderRegistry batchLoaderRegistry = new DefaultBatchLoaderRegistry();
		DefaultExecutionGraphQlService service = new DefaultExecutionGraphQlService(graphQlSource);
		service.addDataLoaderRegistrar(batchLoaderRegistry);
		return service;
	}


	@Bean (4)
	public RouterFunction<ServerResponse> graphQlRouterFunction(ExecutionGraphQlService graphQlService) {
		WebGraphQlHandler webGraphQlHandler = WebGraphQlHandler.builder(graphQlService).build();
		GraphQlHttpHandler graphQlHttpHandler = new GraphQlHttpHandler(webGraphQlHandler);
		RequestPredicate graphQlPredicate = GraphQlRequestPredicates.graphQlHttp("/graphql");
		GraphiQlHandler graphiQlHandler = new GraphiQlHandler("/graphql", "");
		return RouterFunctions.route() (5)
				.route(graphQlPredicate, graphQlHttpHandler::handleRequest)
				.GET("/graphiql", graphiQlHandler::handleRequest)
				.build();
	}
}
1AnnotatedControllerConfigurer Bean は、GraphQL @Controller ハンドラーを検出するロールを担います。
2ExecutionGraphQlService は、トランスポートに依存しない方法で GraphQL リクエストを処理します。
3GraphQlSource ビルダーは主要な構成ポイントです。その他のオプションについては、その API を調べましょう。
4RouterFunction は、GraphQL ルートを関数エンドポイントとして公開します。
5 その後、さまざまなルートでさまざまなトランスポート (WebSocket、SSE、HTTP) を公開できます。

Spring for GraphQL は、Spring プロジェクトとのその他の多くのオプションと統合を提供します。詳細については、Spring Boot 自動構成を参照してください。