From 9180a7e4dc2bd7d6d6fd09d10fb3585093bfa5ae Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 26 Mar 2025 16:01:22 +0100 Subject: [PATCH] Allow creation of custom GraphQlSource instance Closes gh-1086 --- .../AbstractGraphQlSourceBuilder.java | 14 +++++++-- .../graphql/execution/GraphQlSource.java | 29 ++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java index 8c418900..09b6af76 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -32,6 +32,7 @@ import graphql.schema.SchemaTransformer; import graphql.schema.SchemaTraverser; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; /** @@ -58,6 +59,8 @@ public abstract class AbstractGraphQlSourceBuilder graphQlConfigurer; + private GraphQlSource.Factory graphQlSourceFactory = FixedGraphQlSource::new; + @Override public B exceptionResolvers(List resolvers) { @@ -96,6 +99,13 @@ public abstract class AbstractGraphQlSourceBuilder T self() { return (T) this; @@ -118,7 +128,7 @@ public abstract class AbstractGraphQlSourceBuilder configurer); + /** + * Configure a factory to use to create the {@link GraphQlSource} instance + * to return from the {@link #build()} method. + *

By default, the instance is a simple container of {@link GraphQL} and + * {@link GraphQLSchema}. Applications can use this to create a different + * implementation that applies additional per-request logic. + * @param factory the factory to use + * @return the current builder + * @since 1.4.0 + */ + B graphQlSourceFactory(Factory factory); + /** * Build the {@link GraphQlSource} instance. */ @@ -240,4 +252,19 @@ public interface GraphQlSource { } + + /** + * Strategy to create the {@link GraphQlSource} instance in {@link Builder#build()}. + */ + interface Factory { + + /** + * Create a {@link GraphQlSource} with the given inputs. + * @param graphQl the {@code GraphQLJava} initialized by the builder + * @param schema the schema initialized by the builder + * @return the created instance + */ + GraphQlSource create(GraphQL graphQl, GraphQLSchema schema); + } + }