ExpediaGroup / graphql-kotlin

Libraries for running GraphQL in Kotlin
https://opensource.expediagroup.com/graphql-kotlin/
Apache License 2.0
1.74k stars 345 forks source link

Unable to retrieve federated types with apollo federation v1 #1933

Closed Jasper-ui closed 7 months ago

Jasper-ui commented 7 months ago

Hi guys, I am unable to retrieve federated types with apollo federation v1. I am gettign this error with the below classes

end_time {
  seconds: 1709241684
  nanos: 935868841
}
start_time {
  seconds: 1709241684
  nanos: 902215362
}
duration_ns: 33656653
root {
  child {
    response_name: "_entities"
    type: "[_Entity]!"
    start_time: 19058924
    end_time: 32150362
    error {
      message: "Unable to resolve federated type, representation={__typename=Property, primaryKey=8db0b295-e4c5-48c8-8dc6-84d79853f7f0}"
      location {
        line: 4294967295
        column: 4294967295
      }
    }
    parent_type: "Query"
  }
}

Resolver

class PropertyResolver(
    private val control: IApartmentGraphqlControl
) : FederatedTypeSuspendResolver<PropertyResolver.Property> {
    override val typeName = "Property"
    override suspend fun resolve(
        environment: DataFetchingEnvironment,
        representation: Map<String, Any>
    ): Property? =
        representation["primaryKey"]?.toString()?.let { id -> Property(id, control) }

    @KeyDirective(fields = FieldSet("primaryKey"))
    @ExtendsDirective
    data class Property(
        @ExternalDirective val primaryKey: String,
        private val control: IApartmentGraphqlControl
    ) {
        fun apartments(): List<GraphqlApartmentOutput> = control.findAllApartmentsForProperty(UUID.fromString(primaryKey))

    }

}
graphql:
  schema:
    registry:
      enabled: ${GRAPHQL_SCHEMA_REGISTRY_ENABLED}
      url: ${GRAPHQL_SCHEMA_REGISTRY_URL}
      token: ${GRAPHQL_SCHEMA_REGISTRY_API_TOKEN}
      servlet: "/graphql"
      service:
        port: ${server.port}
        serverName: localhost
        name: "{microservice.name}"
        version: "1.0.0"
  federation:
    optInV2: false
    enabled: true
    tracing:
      enabled: true
      debug: true
  maxPageSize: 32
  packages:
    - "com.example.apartment"
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "3.1.4"
    id("io.spring.dependency-management") version "1.1.3"
    id("com.expediagroup.graphql") version "7.0.2"
    kotlin("jvm") version "1.8.22"
    kotlin("plugin.spring") version "1.8.22"
}

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

java {
    sourceCompatibility = JavaVersion.VERSION_19
}

repositories {
    mavenCentral()
    maven("https://jitpack.io")
    maven {
        url = uri("https://plugins.gradle.org/m2/")
    }
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("software.amazon.awssdk:dynamodb:2.20.153")
    implementation("software.amazon.awssdk:auth:2.20.153")
    implementation ("com.github.oharaandrew314:dynamodb-kotlin-module:0.3.0")
    implementation ("io.github.oshai:kotlin-logging-jvm:5.1.0")
    implementation("com.expediagroup:graphql-kotlin-schema-generator:7.0.2")
    implementation("com.expediagroup:graphql-kotlin-federation:7.0.2")
    implementation("com.expediagroup:graphql-kotlin-federated-hooks-provider:7.0.2")
    implementation("com.expediagroup:graphql-kotlin-spring-server:7.0.2")
    implementation("javax.validation:validation-api:2.0.0.Final")
    implementation("org.json:json:20230618")
    graphqlSDL("com.expediagroup", "graphql-kotlin-federated-hooks-provider")
    testImplementation("com.graphql-java-kickstart:graphql-spring-boot-starter-test:15.0.0")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation ("io.kotest:kotest-runner-junit5:5.7.2")
    testImplementation ("io.kotest:kotest-assertions-core:5.7.2")
    testImplementation("org.http4k:http4k-connect-amazon-dynamodb-fake:5.2.0.0")
    testImplementation ("org.junit.jupiter:junit-jupiter-api:5.8.1")
    testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.8.1")
}

tasks.named("build") {
    dependsOn("bootJar")
    dependsOn("graphqlGenerateSDL")
}

apply(plugin = "com.expediagroup.graphql")

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs += "-Xjsr305=strict"
        jvmTarget = "19"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

property class in property MS

@GraphQLName("Property")
@KeyDirective(fields = FieldSet("primaryKey"))
data class GraphqlPropertyOutput(
        val primaryKey: String,
        val name: String,
        val landlordName: String,
        val createdAt: String,
        val updatedAt: String
)
Jasper-ui commented 7 months ago

Forgot to add @Component to the resolver :facepalm: