Skip to content

Polymorphic queries not working with ktor version 3.x.x #2074

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

Open
techpert opened this issue Feb 12, 2025 · 0 comments
Open

Polymorphic queries not working with ktor version 3.x.x #2074

techpert opened this issue Feb 12, 2025 · 0 comments
Labels
type: bug Something isn't working

Comments

@techpert
Copy link

techpert commented Feb 12, 2025

Library Version
8.3.0

Describe the bug
I was trying to follow the example given in your documentation here https://opensource.expediagroup.com/graphql-kotlin/docs/schema-generator/writing-schemas/interfaces/
If i try to use an interface it gives me an exception com.expediagroup.graphql.generator.exceptions.ConflictingTypesException: Conflicting class names in schema generation

To Reproduce

  1. Create a simple KTOR application with Ktor version 3.0.3
  2. my libs.versions.toml file is as follows :

[versions]
kotlin-version = "2.1.0"
ktor-version = "3.0.3"
logback-version = "1.4.14"
graphql-version= "8.3.0"

[libraries]
ktor-server-core = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor-version" }
ktor-server-host-common = { module = "io.ktor:ktor-server-host-common-jvm", version.ref = "ktor-version" }
ktor-server-status-pages = { module = "io.ktor:ktor-server-status-pages-jvm", version.ref = "ktor-version" }
ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negotiation-jvm", version.ref = "ktor-version" }
ktor-serialization-jackson = { module = "io.ktor:ktor-serialization-jackson-jvm", version.ref = "ktor-version" }
ktor-server-netty = { module = "io.ktor:ktor-server-netty-jvm", version.ref = "ktor-version" }
logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback-version" }
ktor-server-config-yaml = { module = "io.ktor:ktor-server-config-yaml-jvm", version.ref = "ktor-version" }
ktor-server-test-host = { module = "io.ktor:ktor-server-test-host-jvm", version.ref = "ktor-version" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin-version" }
graphql-kotlin-ktor={module="com.expediagroup:graphql-kotlin-ktor-server", version.ref = "graphql-version"}
ktor-server-websockets = { module = "io.ktor:ktor-server-websockets-jvm", version.ref = "ktor-version" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin-version" }
ktor = { id = "io.ktor.plugin", version.ref = "ktor-version" }
graphql= {id="com.expediagroup.graphql", version.ref = "graphql-version"}

My build.gradle is as follows :

plugins {
    alias(libs.plugins.kotlin.jvm)
    alias(libs.plugins.ktor)
    alias(libs.plugins.graphql)
}

group = "com.example"
version = "0.0.1"

application {
    mainClass.set("io.ktor.server.netty.EngineMain")

    val isDevelopment: Boolean = project.ext.has("development")
    applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(libs.ktor.server.core)
    implementation(libs.ktor.server.host.common)
    implementation(libs.ktor.server.status.pages)
    implementation(libs.ktor.server.content.negotiation)
    implementation(libs.ktor.serialization.jackson)
    implementation(libs.ktor.server.websockets)
    implementation(libs.ktor.server.netty)
    implementation(libs.logback.classic)
    implementation(libs.ktor.server.config.yaml)
    implementation(libs.graphql.kotlin.ktor)
    testImplementation(libs.ktor.server.test.host)
    testImplementation(libs.kotlin.test.junit)
}

My application.kt is as follows :

package com.example


import com.expediagroup.graphql.server.ktor.GraphQL
import com.expediagroup.graphql.server.ktor.defaultGraphQLStatusPages
import com.expediagroup.graphql.server.ktor.graphQLPostRoute
import com.expediagroup.graphql.server.ktor.graphiQLRoute
import com.expediagroup.graphql.server.operations.Query
import io.ktor.server.application.*
import io.ktor.server.plugins.statuspages.StatusPages
import io.ktor.server.routing.routing

fun main(args: Array<String>) {
    io.ktor.server.netty.EngineMain.main(args)
}
enum class AnimalTypes {
    CAT,
    DOG
}

interface Animal {
    val type: AnimalTypes
    fun sound(): String
}

class Cat : Animal {
    override val type = AnimalTypes.CAT

    override fun sound() = "meow"

    fun ignoreEveryone(): String = "ignore everyone"
}

class Dog : Animal {
    override val type = AnimalTypes.DOG

    override fun sound() = "bark"

    fun barkAtEveryone(): String = "bark at everyone"
}

class HelloWorldQuery : Query {
    fun getAnimalByType(type: AnimalTypes): Animal = when (type) {
        AnimalTypes.CAT -> Cat()
        AnimalTypes.DOG -> Dog()
    }
}

fun Application.module() {
    install(GraphQL) {
        schema {
            packages = listOf("com.example")
            queries = listOf(
                HelloWorldQuery()
            )
        }
    }
    routing{
        graphQLPostRoute()
        graphiQLRoute()
    }


    install(StatusPages) {
        defaultGraphQLStatusPages()
    }
}


Expected behavior
the query should work fine as it works with ktor 2.x.x and library version 8.2.1

Actual Behaviour
I get an exception on the console

Exception in thread "main" com.expediagroup.graphql.generator.exceptions.ConflictingTypesException: Conflicting class names in schema generation [class com.example.graphql.model.Animal, class com.example.graphql.model.Animal]
	at com.expediagroup.graphql.generator.internal.state.TypesCache.get$graphql_kotlin_schema_generator(TypesCache.kt:60)


Image

The same code works fine with ktor version 2.x.x and library version 8.2.1

Attached is minimum reproducible code as a project

ktor-sample859.zip

@techpert techpert added the type: bug Something isn't working label Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Development

No branches or pull requests

1 participant