diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultGraphQlSourceBuilder.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultGraphQlSourceBuilder.java index b553eb3cc..8eeeec0de 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultGraphQlSourceBuilder.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultGraphQlSourceBuilder.java @@ -46,6 +46,7 @@ import graphql.schema.idl.TypeDefinitionRegistry; import graphql.schema.idl.WiringFactory; +import graphql.schema.idl.errors.SchemaProblem; import org.springframework.core.io.Resource; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -184,7 +185,7 @@ private void registerDefaultTypeResolver(TypeDefinitionRegistry registry, Runtim private TypeDefinitionRegistry parseSchemaResource(Resource schemaResource) { Assert.notNull(schemaResource, "'schemaResource' not provided"); - Assert.isTrue(schemaResource.exists(), "'schemaResource' does not exist"); + Assert.isTrue(schemaResource.exists(), "'schemaResource' must exist: " + schemaResource); try { try (InputStream inputStream = schemaResource.getInputStream()) { return new SchemaParser().parse(inputStream); @@ -193,6 +194,9 @@ private TypeDefinitionRegistry parseSchemaResource(Resource schemaResource) { catch (IOException ex) { throw new IllegalArgumentException("Failed to load schema resource: " + schemaResource); } + catch (SchemaProblem ex) { + throw new InvalidSchemaResourceException(schemaResource.getDescription(), ex); + } } private GraphQLSchema applyTypeVisitors(GraphQLSchema schema) { diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/InvalidSchemaResourceException.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/InvalidSchemaResourceException.java new file mode 100644 index 000000000..ff8068cdd --- /dev/null +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/InvalidSchemaResourceException.java @@ -0,0 +1,44 @@ +package org.springframework.graphql.execution; + +import graphql.GraphQLException; +import graphql.schema.idl.errors.SchemaProblem; + + +/** + * Indicates that the Resource in {@link GraphQlSource.Builder} is invalid. + * + * @author GenKui Du + * @since 1.0.0 + */ +public class InvalidSchemaResourceException extends GraphQLException { + + private final String resourceDescription; + + private final SchemaProblem schemaProblem; + + public InvalidSchemaResourceException(String resourceDescription, SchemaProblem schemaProblem) { + super( + String.format("invalid schema, resource = %s, errors = %s", resourceDescription, schemaProblem.getErrors()), + schemaProblem + ); + this.resourceDescription = resourceDescription; + this.schemaProblem = schemaProblem; + } + + public String getResourceDescription() { + return resourceDescription; + } + + public SchemaProblem getSchemaProblem() { + return schemaProblem; + } + + @Override + public String toString() { + return "InvalidSchemaResourceException{" + + "resourceDescription='" + resourceDescription + '\'' + + ", schemaProblem=" + schemaProblem + + '}'; + } + +}