Skip to content
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

Resolvers instantiated as anonymous inner classes cannot be resolved by Kotlin #714

Open
zleonov opened this issue Oct 11, 2022 · 0 comments
Labels

Comments

@zleonov
Copy link

zleonov commented Oct 11, 2022

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

@zleonov zleonov added the bug label Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant