Skip to content

Commit 6957557

Browse files
committed
Configure additional GraphQL schema files
Prior to this commit, the GraphQL auto-configuration would provide configuration properties for specifying the locations and file extensions of schema files to be scanned during startup. This commit adds a new "spring.graphql.schema.additional-files" that applications can use to point at particular files, like "classpath:mylocation/schema.graphqls". These files will be parsed and considered for the schema in addition to the scanned ones. Closes gh-42792
1 parent af49970 commit 6957557

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-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.
@@ -96,11 +96,15 @@ public GraphQlSource graphQlSource(ResourcePatternResolver resourcePatternResolv
9696
ObjectProvider<SubscriptionExceptionResolver> subscriptionExceptionResolvers,
9797
ObjectProvider<Instrumentation> instrumentations, ObjectProvider<RuntimeWiringConfigurer> wiringConfigurers,
9898
ObjectProvider<GraphQlSourceBuilderCustomizer> sourceCustomizers) {
99+
99100
String[] schemaLocations = properties.getSchema().getLocations();
100-
Resource[] schemaResources = resolveSchemaResources(resourcePatternResolver, schemaLocations,
101-
properties.getSchema().getFileExtensions());
101+
List<Resource> schemaResources = new ArrayList<>();
102+
schemaResources.addAll(resolveSchemaResources(resourcePatternResolver, schemaLocations,
103+
properties.getSchema().getFileExtensions()));
104+
schemaResources.addAll(Arrays.asList(properties.getSchema().getAdditionalFiles()));
105+
102106
GraphQlSource.SchemaResourceBuilder builder = GraphQlSource.schemaResourceBuilder()
103-
.schemaResources(schemaResources)
107+
.schemaResources(schemaResources.toArray(new Resource[0]))
104108
.exceptionResolvers(exceptionResolvers.orderedStream().toList())
105109
.subscriptionExceptionResolvers(subscriptionExceptionResolvers.orderedStream().toList())
106110
.instrumentation(instrumentations.orderedStream().toList());
@@ -116,15 +120,15 @@ public GraphQlSource graphQlSource(ResourcePatternResolver resourcePatternResolv
116120
return builder.build();
117121
}
118122

119-
private Resource[] resolveSchemaResources(ResourcePatternResolver resolver, String[] locations,
123+
private List<Resource> resolveSchemaResources(ResourcePatternResolver resolver, String[] locations,
120124
String[] extensions) {
121125
List<Resource> resources = new ArrayList<>();
122126
for (String location : locations) {
123127
for (String extension : extensions) {
124128
resources.addAll(resolveSchemaResources(resolver, location + "*" + extension));
125129
}
126130
}
127-
return resources.toArray(new Resource[0]);
131+
return resources;
128132
}
129133

130134
private List<Resource> resolveSchemaResources(ResourcePatternResolver resolver, String pattern) {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-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.
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121

2222
import org.springframework.boot.context.properties.ConfigurationProperties;
23+
import org.springframework.core.io.Resource;
2324

2425
/**
2526
* {@link ConfigurationProperties Properties} for Spring GraphQL.
@@ -85,6 +86,11 @@ public static class Schema {
8586
*/
8687
private String[] fileExtensions = new String[] { ".graphqls", ".gqls" };
8788

89+
/**
90+
* Locations of additional, individual schema files to parse.
91+
*/
92+
private Resource[] additionalFiles = new Resource[0];
93+
8894
private final Inspection inspection = new Inspection();
8995

9096
private final Introspection introspection = new Introspection();
@@ -107,6 +113,14 @@ public void setFileExtensions(String[] fileExtensions) {
107113
this.fileExtensions = fileExtensions;
108114
}
109115

116+
public Resource[] getAdditionalFiles() {
117+
return this.additionalFiles;
118+
}
119+
120+
public void setAdditionalFiles(Resource[] additionalFiles) {
121+
this.additionalFiles = additionalFiles;
122+
}
123+
110124
private String[] appendSlashIfNecessary(String[] locations) {
111125
return Arrays.stream(locations)
112126
.map((location) -> location.endsWith("/") ? location : location + "/")

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-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.
@@ -110,6 +110,19 @@ void shouldScanLocationsWithCustomExtension() {
110110
});
111111
}
112112

113+
@Test
114+
void shouldConfigureAdditionalSchemaFiles() {
115+
this.contextRunner
116+
.withPropertyValues("spring.graphql.schema.additional-files=classpath:graphql/types/person.custom")
117+
.run((context) -> {
118+
assertThat(context).hasSingleBean(GraphQlSource.class);
119+
GraphQlSource graphQlSource = context.getBean(GraphQlSource.class);
120+
GraphQLSchema schema = graphQlSource.schema();
121+
assertThat(schema.getObjectType("Book")).isNotNull();
122+
assertThat(schema.getObjectType("Person")).isNotNull();
123+
});
124+
}
125+
113126
@Test
114127
void shouldBackOffWithCustomGraphQlSource() {
115128
this.contextRunner.withUserConfiguration(CustomGraphQlSourceConfiguration.class).run((context) -> {

0 commit comments

Comments
 (0)