Skip to content

Commit 9180a7e

Browse files
committed
Allow creation of custom GraphQlSource instance
Closes gh-1086
1 parent 4ce9ca7 commit 9180a7e

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

Diff for: spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import graphql.schema.SchemaTraverser;
3333

3434
import org.springframework.lang.Nullable;
35+
import org.springframework.util.Assert;
3536

3637

3738
/**
@@ -58,6 +59,8 @@ public abstract class AbstractGraphQlSourceBuilder<B extends GraphQlSource.Build
5859
@Nullable
5960
private Consumer<GraphQL.Builder> graphQlConfigurer;
6061

62+
private GraphQlSource.Factory graphQlSourceFactory = FixedGraphQlSource::new;
63+
6164

6265
@Override
6366
public B exceptionResolvers(List<DataFetcherExceptionResolver> resolvers) {
@@ -96,6 +99,13 @@ public B configureGraphQl(Consumer<GraphQL.Builder> configurer) {
9699
return self();
97100
}
98101

102+
@Override
103+
public B graphQlSourceFactory(GraphQlSource.Factory factory) {
104+
Assert.notNull(factory, "GraphQlSource.Factory is required");
105+
this.graphQlSourceFactory = factory;
106+
return self();
107+
}
108+
99109
@SuppressWarnings("unchecked")
100110
private <T extends B> T self() {
101111
return (T) this;
@@ -118,7 +128,7 @@ public GraphQlSource build() {
118128

119129
applyGraphQlConfigurers(builder);
120130

121-
return new FixedGraphQlSource(builder.build(), schema);
131+
return this.graphQlSourceFactory.create(builder.build(), schema);
122132
}
123133

124134
/**

Diff for: spring-graphql/src/main/java/org/springframework/graphql/execution/GraphQlSource.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -149,6 +149,18 @@ interface Builder<B extends Builder<B>> {
149149
*/
150150
B configureGraphQl(Consumer<GraphQL.Builder> configurer);
151151

152+
/**
153+
* Configure a factory to use to create the {@link GraphQlSource} instance
154+
* to return from the {@link #build()} method.
155+
* <p>By default, the instance is a simple container of {@link GraphQL} and
156+
* {@link GraphQLSchema}. Applications can use this to create a different
157+
* implementation that applies additional per-request logic.
158+
* @param factory the factory to use
159+
* @return the current builder
160+
* @since 1.4.0
161+
*/
162+
B graphQlSourceFactory(Factory factory);
163+
152164
/**
153165
* Build the {@link GraphQlSource} instance.
154166
*/
@@ -240,4 +252,19 @@ SchemaResourceBuilder schemaFactory(
240252

241253
}
242254

255+
256+
/**
257+
* Strategy to create the {@link GraphQlSource} instance in {@link Builder#build()}.
258+
*/
259+
interface Factory {
260+
261+
/**
262+
* Create a {@link GraphQlSource} with the given inputs.
263+
* @param graphQl the {@code GraphQLJava} initialized by the builder
264+
* @param schema the schema initialized by the builder
265+
* @return the created instance
266+
*/
267+
GraphQlSource create(GraphQL graphQl, GraphQLSchema schema);
268+
}
269+
243270
}

0 commit comments

Comments
 (0)