Skip to content

Instantly share code, notes, and snippets.

@zleonov
Last active October 11, 2022 03:25
Show Gist options
  • Save zleonov/32f45aefdce49d4666877f4d276d5c2a to your computer and use it in GitHub Desktop.
Save zleonov/32f45aefdce49d4666877f4d276d5c2a to your computer and use it in GitHub Desktop.
This gist demonstrates an bug in graphql-java-tools where checked-exception messages don't propogate
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.kickstart.tools.GraphQLQueryResolver;
import graphql.kickstart.tools.SchemaParser;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.TypeDefinitionRegistry;
public class CheckedErrorTest {
private static final String SCHEMA = "type Query { hello: String! }";
public static void main(String[] args) {
executeWithGraphQLJava(); // prints: Exception while fetching data (/hello) : *** MESSAGE ***
executeWithGraphQLJavaTools(); // prints: Exception while fetching data (/hello) : null
}
public static void executeWithGraphQLJava() {
final TypeDefinitionRegistry typedef = new graphql.schema.idl.SchemaParser().parse(SCHEMA);
final RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring().type("Query", builder -> builder.dataFetcher("hello", new ErrorFetcher())).build();
final GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(typedef, wiring);
final GraphQL graphql = GraphQL.newGraphQL(schema).build();
final ExecutionResult result = graphql.execute("{ hello }");
System.out.println(result.getErrors().iterator().next().getMessage());
}
public static void executeWithGraphQLJavaTools() {
final GraphQLSchema schema = SchemaParser.newParser().schemaString(SCHEMA).resolvers(new ErrorResolver()).build().makeExecutableSchema();
final GraphQL graphql = GraphQL.newGraphQL(schema).build();
final ExecutionResult result = graphql.execute("{ hello }");
System.out.println(result.getErrors().iterator().next().getMessage());
}
public static class ErrorResolver implements GraphQLQueryResolver {
public String hello() throws Throwable {
throw new Exception("*** MESSAGE ***"); }
}
private static class ErrorFetcher implements DataFetcher<String> {
@Override
public String get(final DataFetchingEnvironment env) throws Exception {
throw new Exception("*** MESSAGE ***");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment