Skip to content

Provide details when schema resource is invalid #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason to create InvalidSchemaResourceException is that I think the 'wrong schema config' not match the semantic of IllegalArgumentException.

}
}

private GraphQLSchema applyTypeVisitors(GraphQLSchema schema) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Contributor Author

@dugenkui03 dugenkui03 Feb 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SchemaProblem will be changed from internal to publicApi, we are free to use it, details in issues 2734;


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 +
'}';
}

}