Load GraphQL schema as a Resource

This commit is contained in:
Brian Clozel
2021-01-05 11:20:05 +01:00
parent 63065d4e8e
commit c6d75f57d7
2 changed files with 35 additions and 27 deletions

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.boot.graphql;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
@@ -35,7 +34,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
@Configuration
@ConditionalOnClass(GraphQL.class)
@@ -57,26 +57,30 @@ public class GraphQLAutoConfiguration {
@Bean
public GraphQL.Builder graphQLBuilder(GraphQLProperties properties, RuntimeWiring runtimeWiring,
ResourceLoader resourceLoader,
ObjectProvider<Instrumentation> instrumentationsProvider) {
try {
File schemaFile = ResourceUtils.getFile(properties.getSchemaLocation());
GraphQLSchema schema = buildSchema(schemaFile, runtimeWiring);
GraphQL.Builder builder = GraphQL.newGraphQL(schema);
List<Instrumentation> instrumentations = instrumentationsProvider.orderedStream().collect(Collectors.toList());
if (!instrumentations.isEmpty()) {
builder = builder.instrumentation(new ChainedInstrumentation(instrumentations));
}
return builder;
}
catch (FileNotFoundException ex) {
throw new MissingGraphQLSchemaException(properties.getSchemaLocation());
Resource schemaResource = resourceLoader.getResource(properties.getSchemaLocation());
GraphQLSchema schema = buildSchema(schemaResource, runtimeWiring);
GraphQL.Builder builder = GraphQL.newGraphQL(schema);
List<Instrumentation> instrumentations = instrumentationsProvider.orderedStream().collect(Collectors.toList());
if (!instrumentations.isEmpty()) {
builder = builder.instrumentation(new ChainedInstrumentation(instrumentations));
}
return builder;
}
private GraphQLSchema buildSchema(File schemaFile, RuntimeWiring runtimeWiring) {
TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(schemaFile);
SchemaGenerator schemaGenerator = new SchemaGenerator();
return schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
private GraphQLSchema buildSchema(Resource schemaResource, RuntimeWiring runtimeWiring) {
if (!schemaResource.exists()) {
throw new MissingGraphQLSchemaException(schemaResource);
}
try {
TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(schemaResource.getInputStream());
SchemaGenerator schemaGenerator = new SchemaGenerator();
return schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
}
catch (IOException exc) {
throw new MissingGraphQLSchemaException(exc, schemaResource);
}
}
}

View File

@@ -16,22 +16,26 @@
package org.springframework.boot.graphql;
import org.springframework.util.StringUtils;
import org.springframework.core.io.Resource;
/**
* Exception thrown when no GraphQL schema is available.
*/
public class MissingGraphQLSchemaException extends RuntimeException {
private final String path;
private final Resource schemaResource;
MissingGraphQLSchemaException(String path) {
super(StringUtils.hasText(path) ? "Path to GraphQL schema not configured" : "Cannot find schema file at: "
+ path + " (please add a schema file or check your GraphQL configuration)");
this.path = path;
public MissingGraphQLSchemaException(Throwable cause, Resource schemaResource) {
super(cause);
this.schemaResource = schemaResource;
}
public String getPath() {
return this.path;
MissingGraphQLSchemaException(Resource schemaResource) {
super("Schema resource " + schemaResource.toString() + " does not exist");
this.schemaResource = schemaResource;
}
public Resource getSchemaResource() {
return this.schemaResource;
}
}