Skip to content

Commit f69aa39

Browse files
committed
Only scan for *.graphqls and *.gqls schema files
Prior to this commit, the auto-configuration was scanning for `"*.graphql,*.gql,*.graphqls,*.gqls"` files in the configured locations. This was not flexible enough, as developers could not customize the considered file extensions when scanning for files. This commit provides a new `spring.graphql.schema.file-extensions` configuration property that enables such customizations. Because `"*.graphql,*.gql"` extensions seem to be tied to queries (and not schemas), this commit also changes the default value to only consider the `"*.graphqls,*.gqls"` file extensions by default. Closes gh-134 Closes gh-135
1 parent 9480953 commit f69aa39

File tree

6 files changed

+40
-22
lines changed

6 files changed

+40
-22
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,15 @@ public class GraphQlAutoConfiguration {
5656

5757
private static final Log logger = LogFactory.getLog(GraphQlAutoConfiguration.class);
5858

59-
private static final String[] SCHEMA_FILES_EXTENSIONS = new String[] {"*.graphqls", "*.graphql", "*.gql", "*.gqls"};
60-
6159
@Bean
6260
public GraphQlSource graphQlSource(ResourcePatternResolver resourcePatternResolver, GraphQlProperties properties,
6361
ObjectProvider<DataFetcherExceptionResolver> exceptionResolversProvider,
6462
ObjectProvider<Instrumentation> instrumentationsProvider,
6563
ObjectProvider<GraphQlSourceBuilderCustomizer> sourceCustomizers,
6664
ObjectProvider<RuntimeWiringConfigurer> wiringConfigurers) throws IOException {
6765

68-
List<Resource> schemaResources = resolveSchemaResources(resourcePatternResolver, properties.getSchema().getLocations());
66+
List<Resource> schemaResources = resolveSchemaResources(resourcePatternResolver, properties.getSchema().getLocations(),
67+
properties.getSchema().getFileExtensions());
6968
GraphQlSource.Builder builder = GraphQlSource.builder()
7069
.schemaResources(schemaResources.toArray(new Resource[0]))
7170
.exceptionResolvers(exceptionResolversProvider.orderedStream().collect(Collectors.toList()))
@@ -80,15 +79,16 @@ public GraphQlSource graphQlSource(ResourcePatternResolver resourcePatternResolv
8079
}
8180
}
8281

83-
private List<Resource> resolveSchemaResources(ResourcePatternResolver resolver, List<String> schemaLocations) throws IOException {
82+
private List<Resource> resolveSchemaResources(ResourcePatternResolver resolver, String[] schemaLocations, String[] fileExtensions) {
8483
List<Resource> schemaResources = new ArrayList<>();
8584
for (String location : schemaLocations) {
86-
for (String extension : SCHEMA_FILES_EXTENSIONS) {
85+
for (String extension : fileExtensions) {
86+
String resourcePattern = location + "*" + extension;
8787
try {
88-
schemaResources.addAll(Arrays.asList(resolver.getResources(location + extension)));
88+
schemaResources.addAll(Arrays.asList(resolver.getResources(resourcePattern)));
8989
}
9090
catch (IOException ex) {
91-
logger.debug("Could not resolve schema location: '" + location + extension + "'", ex);
91+
logger.debug("Could not resolve schema location: '" + resourcePattern + "'", ex);
9292
}
9393
}
9494
}

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

+22-11
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
package org.springframework.graphql.boot;
1818

1919
import java.time.Duration;
20-
import java.util.ArrayList;
21-
import java.util.Collections;
22-
import java.util.List;
23-
import java.util.stream.Collectors;
20+
import java.util.Arrays;
2421

2522
import org.springframework.boot.context.properties.ConfigurationProperties;
2623

@@ -67,24 +64,38 @@ public Websocket getWebsocket() {
6764
public static class Schema {
6865

6966
/**
70-
* Locations of GraphQL '*.graphqls' schema files.
67+
* Locations of GraphQL schema files.
7168
*/
72-
private List<String> locations = new ArrayList<>(Collections.singletonList("classpath:graphql/"));
69+
private String[] locations = new String[] { "classpath:graphql/"};
70+
71+
/**
72+
* File extensions for GraphQL schema files.
73+
*/
74+
private String[] fileExtensions = new String[] { ".graphqls", ".gqls"};
75+
7376

7477
private final Printer printer = new Printer();
7578

76-
public List<String> getLocations() {
79+
public String[] getLocations() {
7780
return this.locations;
7881
}
7982

80-
public void setLocations(List<String> locations) {
83+
public void setLocations(String[] locations) {
8184
this.locations = appendSlashIfNecessary(locations);
8285
}
8386

84-
private List<String> appendSlashIfNecessary(List<String> locations) {
85-
return locations.stream()
87+
public String[] getFileExtensions() {
88+
return this.fileExtensions;
89+
}
90+
91+
public void setFileExtensions(String[] fileExtensions) {
92+
this.fileExtensions = fileExtensions;
93+
}
94+
95+
private String[] appendSlashIfNecessary(String[] locations) {
96+
return Arrays.stream(locations)
8697
.map(location -> location.endsWith("/") ? location : location + "/")
87-
.collect(Collectors.toList());
98+
.toArray(String[]::new);
8899
}
89100

90101
public Printer getPrinter() {

graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/InvalidSchemaLocationsException.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public class InvalidSchemaLocationsException extends NestedRuntimeException {
3535

3636
private final List<SchemaLocation> schemaLocations;
3737

38-
public InvalidSchemaLocationsException(List<String> locations, ResourcePatternResolver resolver) {
38+
public InvalidSchemaLocationsException(String[] locations, ResourcePatternResolver resolver) {
3939
this(locations, resolver, null);
4040
}
4141

42-
public InvalidSchemaLocationsException(List<String> locations, ResourcePatternResolver resolver, Throwable cause) {
42+
public InvalidSchemaLocationsException(String[] locations, ResourcePatternResolver resolver, Throwable cause) {
4343
super("No schema file could be found in the provided locations.", cause);
4444
List<SchemaLocation> providedLocations = new ArrayList<>();
4545
for (String location : locations) {

graphql-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"name": "spring.graphql.schema.locations",
55
"defaultValue": "classpath:graphql/"
66
},
7+
{
8+
"name": "spring.graphql.schema.file-extensions",
9+
"defaultValue": ".graphqls,.gqls"
10+
},
711
{
812
"name": "management.metrics.graphql.autotime.enabled",
913
"description": "Whether to automatically time graphql requests.",

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ void shouldUseProgrammaticallyDefinedBuilder() {
6262

6363
@Test
6464
void shouldScanLocationsForSchemaFiles() {
65-
this.contextRunner.withPropertyValues("spring.graphql.schema.locations:classpath:schema/")
65+
this.contextRunner.withPropertyValues("spring.graphql.schema.locations:classpath:schema/",
66+
"spring.graphql.schema.file-extensions:.gql,.gqls,.graphql,.graphqls")
6667
.run((context) -> {
6768
assertThat(context).hasSingleBean(GraphQlSource.class);
6869
GraphQlSource graphQlSource = context.getBean(GraphQlSource.class);
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
management.endpoints.web.exposure.include=health,metrics,info
22

3-
spring.graphql.schema.printer.enabled=true
3+
spring.graphql.schema.printer.enabled=true
4+
5+
spring.graph

0 commit comments

Comments
 (0)