aPureBase / KGraphQL

Pure Kotlin GraphQL implementation
https://kgraphql.io
MIT License
307 stars 59 forks source link

NullPointerException in FunctionWrapper #123

Open pyricau opened 3 years ago

pyricau commented 3 years ago

I'm following the provided example.

I created the following sample code:

import com.apurebase.kgraphql.KGraphQL

data class Article(val id: Int, val text: String)

fun main() {
  val schema = KGraphQL.schema {
    query("article") {
      resolver { id: Int?, text: String ->
        Article(id ?: -1, text)
      }
    }
    type<Article> {
      property<String>("fullText") {
        resolver { article: Article ->
          "${article.id}: ${article.text}"
        }
      }
    }
  }

  schema.executeBlocking("""
        {
            article(id: 5, text: "Hello World") {
                id
                fullText
            }
        }
    """.trimIndent()).let(::println)
}

When I run this, I get:

Exception in thread "main" java.lang.NullPointerException
    at com.apurebase.kgraphql.schema.model.FunctionWrapper$ArityTwo$kFunction$2.invoke(FunctionWrapper.kt:224)
    at com.apurebase.kgraphql.schema.model.FunctionWrapper$ArityTwo$kFunction$2.invoke(FunctionWrapper.kt:210)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at com.apurebase.kgraphql.schema.model.FunctionWrapper$ArityTwo.getKFunction(FunctionWrapper.kt)
    at com.apurebase.kgraphql.schema.model.FunctionWrapper$DefaultImpls.getObjectTypeName(FunctionWrapper.kt:481)
    at com.apurebase.kgraphql.schema.model.FunctionWrapper$DefaultImpls.hasReturnType(FunctionWrapper.kt:479)
    at com.apurebase.kgraphql.schema.model.FunctionWrapper$Base.hasReturnType(FunctionWrapper.kt:84)
    at com.apurebase.kgraphql.schema.dsl.operations.AbstractOperationDSL.resolver(AbstractOperationDSL.kt:22)
    at com.apurebase.kgraphql.schema.dsl.operations.AbstractOperationDSL.resolver(AbstractOperationDSL.kt:36)
    at shark.KgraphKt$main$schema$1$1.invoke(kgraph.kt:10)
    at shark.KgraphKt$main$schema$1$1.invoke(kgraph.kt)
    at com.apurebase.kgraphql.schema.dsl.SchemaBuilder.query(SchemaBuilder.kt:46)
    at shark.KgraphKt$main$schema$1.invoke(kgraph.kt:9)
    at shark.KgraphKt$main$schema$1.invoke(kgraph.kt)
    at com.apurebase.kgraphql.KGraphQL$Companion.schema(KGraphQL.kt:11)
    at shark.KgraphKt.main(kgraph.kt:8)
    at shark.KgraphKt.main(kgraph.kt)

My build file:

import org.jetbrains.compose.compose
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.4.20"
    id("org.jetbrains.compose") version "0.2.0-build132"
}

group = "me.py"
version = "1.0"

repositories {
    jcenter()
    mavenCentral()
    maven { url = uri("https://maven.pkg.jetbrains.space/public/p/compose/dev") }
}

dependencies {
    testImplementation(kotlin("test-junit5"))
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")
    implementation(compose.desktop.currentOs)
    implementation("com.squareup.leakcanary:shark:2.5")
    implementation("com.squareup.okio:okio:2.9.0")
    implementation("com.apurebase:kgraphql:0.16.0")
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "11"
}

compose.desktop {
    application {
        mainClass = "MainKt"
        nativeDistributions {
            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            packageName = "SharkApp"
        }
    }
}

Note: the sample code has execute which is a suspend function and generates a compiler error: Suspend function 'execute' should be called only from a coroutine or another suspend function so I replaced it with executeBlocking.

jeggy commented 3 years ago

Thanks for your report. I've tested this and it has something to do with the environment setup. Cause when not using the compose library setup, it works fine. I will investigate further and see if I can find out what exactly is the issue here.

jeggy commented 3 years ago

This is happens because of using Kotlin Compose. I have reported this issue here: https://github.com/JetBrains/compose-jb/issues/250

jeggy commented 3 years ago

This seems like a compiler issue:

I will keep this open as we should try to find a fix that will work even with this compiler issue existing.

pyricau commented 3 years ago

From the compose-jb report:

Maybe you could work it around by putting reflection code into separate module not using Compose compiler plugin.

That's actually somewhat reasonable advice. Creating a separate non compose module for KGraphQL should work.