From c6d75f57d7b66ab5a86fff01f45ab111ea0093d1 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 5 Jan 2021 11:20:05 +0100 Subject: [PATCH] Load GraphQL schema as a Resource --- .../graphql/GraphQLAutoConfiguration.java | 42 ++++++++++--------- .../MissingGraphQLSchemaException.java | 20 +++++---- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/spring-graphql-web/src/main/java/org/springframework/boot/graphql/GraphQLAutoConfiguration.java b/spring-graphql-web/src/main/java/org/springframework/boot/graphql/GraphQLAutoConfiguration.java index 6408ea51..a6977ba7 100644 --- a/spring-graphql-web/src/main/java/org/springframework/boot/graphql/GraphQLAutoConfiguration.java +++ b/spring-graphql-web/src/main/java/org/springframework/boot/graphql/GraphQLAutoConfiguration.java @@ -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 instrumentationsProvider) { - try { - File schemaFile = ResourceUtils.getFile(properties.getSchemaLocation()); - GraphQLSchema schema = buildSchema(schemaFile, runtimeWiring); - GraphQL.Builder builder = GraphQL.newGraphQL(schema); - List 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 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); + } } } diff --git a/spring-graphql-web/src/main/java/org/springframework/boot/graphql/MissingGraphQLSchemaException.java b/spring-graphql-web/src/main/java/org/springframework/boot/graphql/MissingGraphQLSchemaException.java index 2943ee3f..b44862ec 100644 --- a/spring-graphql-web/src/main/java/org/springframework/boot/graphql/MissingGraphQLSchemaException.java +++ b/spring-graphql-web/src/main/java/org/springframework/boot/graphql/MissingGraphQLSchemaException.java @@ -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; } }