graphql-java-kickstart/graphql-java-tools

Resolvers instantiated as anonymous inner classes cannot be resolved by Kotlin

zleonov opened this issue · 0 comments

Description

Java allows classes to be defined in their own separate file (the norm), or nested classes (both inner and static), or instantiated as anonymous inner classes.

When Resolvers are instantiated as anonymous inner classes, and assigned to a field, Kotlin throws an exception when the program is run.

I have not found a similar issue in this repo.

Expected behavior

There should be no difference whether or not Resolvers are defined in their own class files, as nested classes, or instantiated as anonymous inner classes.

Actual behavior

When Resolvers are defined as anonymous inner classes Kotlin fails with the following error:

Exception in thread "main" kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Unresolved class: class example.graphql.HelloWorld$1
	at kotlin.reflect.jvm.internal.KClassImpl.reportUnresolvedClass(KClassImpl.kt:328)
	at kotlin.reflect.jvm.internal.KClassImpl.access$reportUnresolvedClass(KClassImpl.kt:44)
	...
	...

Steps to reproduce the bug

This code reproduces the bug:

import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.kickstart.tools.GraphQLQueryResolver;
import graphql.kickstart.tools.SchemaParser;
import graphql.schema.GraphQLSchema;

public class GraphQLToolsHelloWorld {

    private static GraphQLQueryResolver queryResolver = new GraphQLQueryResolver() {
        public String hello() {
            return "World!";
        }
    };

    // This will work
    // private static GraphQLQueryResolver queryResolver = new QueryResolver();

    public static void main(String[] args) {
        final GraphQLSchema schema = SchemaParser.newParser().file("schema.graphqls").resolvers(queryResolver).build().makeExecutableSchema();
        final GraphQL graphql = GraphQL.newGraphQL(schema).build();
        final ExecutionInput input = ExecutionInput.newExecutionInput().query("{ hello }").build();
        final ExecutionResult result = graphql.execute(input);

        System.out.println(result);
    }
}

This may be an issue native to Kotlin. JetBrains has an ongoing thread about a similar issue: https://youtrack.jetbrains.com/issue/KT-41373/KotlinReflectionInternalError-Unresolved-class-when-inspecting-anonymous-Java-class