From 5433e42ce6459f538c64ecbd83b9be2be6be307e Mon Sep 17 00:00:00 2001 From: dugenkui03 Date: Thu, 24 Feb 2022 01:58:45 +0800 Subject: [PATCH 1/2] print the schemaResource description info when schemaResource not exist --- .../graphql/execution/DefaultGraphQlSourceBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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..5aaaa1a51 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 @@ -184,7 +184,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); From 555289ddfe20479656ef6cc5dc7cc6775bc1f01d Mon Sep 17 00:00:00 2001 From: dugenkui03 Date: Thu, 24 Feb 2022 03:06:53 +0800 Subject: [PATCH 2/2] provide details when schema resource is invalid --- .../DefaultGraphQlSourceBuilder.java | 4 ++ .../InvalidSchemaResourceException.java | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 spring-graphql/src/main/java/org/springframework/graphql/execution/InvalidSchemaResourceException.java 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 5aaaa1a51..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; @@ -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 + + '}'; + } + +}