https://kotlinlang.org logo
Title
d

Dariusz Kuc

04/24/2020, 3:48 PM
hello! 👋 I’m hitting a pretty weird error from
HttpClient
and I am unsure whats causing it, given some result object when I run the following
lateinit var result: GraphQLResult<ExampleQuery.ExampleQueryResult>
runBlocking {
    val graphQLRequest = mapOf(
            "query" to EXAMPLE_QUERY,
            "operationName" to "ExampleQuery",
            "variables" to variables
    )
    result = <http://ktorClient.post|ktorClient.post>("<http://localhost:8080/graphql>") {
        accept(ContentType.Application.Json)
        contentType(ContentType.Application.Json)
        body = graphQLRequest
    }
    // works fine
    println("ktor: ${result.data}")

    val myClient = MyClient(ktorClient, "<http://localhost:8080/graphql>")
    result = myClient.executeOperation(EXAMPLE_QUERY, "ExampleQuery", variables)
    // blows up saying cannot cast linked hash map to my result object
    println("myClient: ${result.data}")
}

class MyClient(val client: HttpClient, val url: String) {
    suspend fun <T> executeOperation(query: String, operationName: String? = null, variables: Any? = null): GraphQLResult<T> {
        val graphQLRequest = mapOf(
                "query" to query,
                "operationName" to operationName,
                "variables" to variables
        )

        return <http://client.post|client.post>(url) {
            accept(ContentType.Application.Json)
            contentType(ContentType.Application.Json)
            body = graphQLRequest
        }
    }
}
any ideas why it would behave differently?
looks like changing
MyClient
to drop the generics and use
suspend fun executeOperation(query: String, operationName: String? = null, variables: Any? = null): GraphQLResult<ExampleQuery.ExampleQueryResult>
works
any idea how I could use generics there?
d

Dominaezzz

04/24/2020, 3:59 PM
I thought it might be that. You can use reified generics.
d

Dariusz Kuc

04/24/2020, 4:05 PM
yeah the problem is that when it gets compiled the generic type is just an object 😞