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
This commit is contained in:
Brian Clozel
2021-06-30 21:06:17 +02:00
parent 6e871a790b
commit ef0fe03fce
7 changed files with 56 additions and 3 deletions

View File

@@ -60,7 +60,7 @@ public class GraphQlAutoConfiguration {
@ConditionalOnMissingBean(GraphQlSource.Builder.class)
public static class GraphQlSourceConfiguration {
private static final String SCHEMA_FILES_PATTERN = "*.graphqls";
private static final String[] SCHEMA_FILES_EXTENSIONS = new String[] {"*.graphqls", "*.graphql", "*.gql", "*.gqls"};
@Bean
@ConditionalOnMissingBean
@@ -85,7 +85,9 @@ public class GraphQlAutoConfiguration {
private List<Resource> resolveSchemaResources(ResourcePatternResolver resolver, List<String> schemaLocations) throws IOException {
List<Resource> schemaResources = new ArrayList<>();
for (String location : schemaLocations) {
schemaResources.addAll(Arrays.asList(resolver.getResources(location + SCHEMA_FILES_PATTERN)));
for (String extension : SCHEMA_FILES_EXTENSIONS) {
schemaResources.addAll(Arrays.asList(resolver.getResources(location + extension)));
}
}
return schemaResources;
}

View File

@@ -16,6 +16,7 @@
package org.springframework.graphql.boot;
import graphql.schema.GraphQLSchema;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -58,6 +59,19 @@ class GraphQlAutoConfigurationTests {
});
}
@Test
void shouldScanLocationsForSchemaFiles() {
this.contextRunner.withPropertyValues("spring.graphql.schema.locations:classpath:schema/")
.run((context) -> {
assertThat(context).hasSingleBean(GraphQlSource.class);
GraphQlSource graphQlSource = context.getBean(GraphQlSource.class);
GraphQLSchema schema = graphQlSource.schema();
assertThat(schema.getObjectType("Movie")).isNotNull();
assertThat(schema.getObjectType("Book")).isNotNull();
assertThat(schema.getObjectType("Store")).isNotNull();
});
}
@Configuration(proxyBeanMethods = false)
static class CustomGraphQlBuilderConfiguration {

View File

@@ -0,0 +1,11 @@
type Book {
id: ID
name: String
pageCount: Int
author: Author
}
type Author {
id: ID
name: String
}

View File

@@ -0,0 +1,10 @@
type Movie {
id: ID
title: String
director: Director
}
type Director {
id: ID
name: String
}

View File

@@ -0,0 +1,5 @@
type Query {
bookById(id: ID): Book
movieById(id: ID): Movie
storeById(id: ID): Store
}

View File

@@ -0,0 +1,10 @@
type Store {
id: ID
location: Location
}
type Location {
address: String
city: String
country: String
}

View File

@@ -129,7 +129,8 @@ The Spring GraphQL project offers a few configuration properties to customize yo
----
# web path to the graphql endpoint
spring.graphql.path=/graphql
# locations of the graphql '*.graphqls' schema files
# locations of the graphql schema files
# scanning for files with well-known extensions: '*.graphqls', '*.gqls', '*.graphql', '*.gql'
spring.graphql.schema.locations=classpath:graphql/
# schema printer endpoint configuration
# endpoint path is concatenated with the main path, so "/graphql/schema" by default