Skip to content

Commit ef0fe03

Browse files
committed
Scan schema locations for well-known file extensions
This commit improves gh-56 to scan schema locations for more well-known file extensions, as previously only `*.graphqls` was supported. We're now scanning for `*.graphqls`, `*.gqls`, `*.graphql`, `*.gql`. Closes gh-56
1 parent 6e871a7 commit ef0fe03

File tree

7 files changed

+56
-3
lines changed

7 files changed

+56
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public GraphQlSource graphQlSource(GraphQlSource.Builder builder) {
6060
@ConditionalOnMissingBean(GraphQlSource.Builder.class)
6161
public static class GraphQlSourceConfiguration {
6262

63-
private static final String SCHEMA_FILES_PATTERN = "*.graphqls";
63+
private static final String[] SCHEMA_FILES_EXTENSIONS = new String[] {"*.graphqls", "*.graphql", "*.gql", "*.gqls"};
6464

6565
@Bean
6666
@ConditionalOnMissingBean
@@ -85,7 +85,9 @@ public GraphQlSource.Builder graphQlSourceBuilder(ResourcePatternResolver resour
8585
private List<Resource> resolveSchemaResources(ResourcePatternResolver resolver, List<String> schemaLocations) throws IOException {
8686
List<Resource> schemaResources = new ArrayList<>();
8787
for (String location : schemaLocations) {
88-
schemaResources.addAll(Arrays.asList(resolver.getResources(location + SCHEMA_FILES_PATTERN)));
88+
for (String extension : SCHEMA_FILES_EXTENSIONS) {
89+
schemaResources.addAll(Arrays.asList(resolver.getResources(location + extension)));
90+
}
8991
}
9092
return schemaResources;
9193
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.graphql.boot;
1818

19+
import graphql.schema.GraphQLSchema;
1920
import org.junit.jupiter.api.Test;
2021

2122
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -58,6 +59,19 @@ void shouldUseProgrammaticallyDefinedBuilder() {
5859
});
5960
}
6061

62+
@Test
63+
void shouldScanLocationsForSchemaFiles() {
64+
this.contextRunner.withPropertyValues("spring.graphql.schema.locations:classpath:schema/")
65+
.run((context) -> {
66+
assertThat(context).hasSingleBean(GraphQlSource.class);
67+
GraphQlSource graphQlSource = context.getBean(GraphQlSource.class);
68+
GraphQLSchema schema = graphQlSource.schema();
69+
assertThat(schema.getObjectType("Movie")).isNotNull();
70+
assertThat(schema.getObjectType("Book")).isNotNull();
71+
assertThat(schema.getObjectType("Store")).isNotNull();
72+
});
73+
}
74+
6175
@Configuration(proxyBeanMethods = false)
6276
static class CustomGraphQlBuilderConfiguration {
6377

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type Book {
2+
id: ID
3+
name: String
4+
pageCount: Int
5+
author: Author
6+
}
7+
8+
type Author {
9+
id: ID
10+
name: String
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type Movie {
2+
id: ID
3+
title: String
4+
director: Director
5+
}
6+
7+
type Director {
8+
id: ID
9+
name: String
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type Query {
2+
bookById(id: ID): Book
3+
movieById(id: ID): Movie
4+
storeById(id: ID): Store
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type Store {
2+
id: ID
3+
location: Location
4+
}
5+
6+
type Location {
7+
address: String
8+
city: String
9+
country: String
10+
}

spring-graphql-docs/src/docs/asciidoc/index.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ The Spring GraphQL project offers a few configuration properties to customize yo
129129
----
130130
# web path to the graphql endpoint
131131
spring.graphql.path=/graphql
132-
# locations of the graphql '*.graphqls' schema files
132+
# locations of the graphql schema files
133+
# scanning for files with well-known extensions: '*.graphqls', '*.gqls', '*.graphql', '*.gql'
133134
spring.graphql.schema.locations=classpath:graphql/
134135
# schema printer endpoint configuration
135136
# endpoint path is concatenated with the main path, so "/graphql/schema" by default

0 commit comments

Comments
 (0)