Skip to content

Commit

Permalink
update graphql-kotlin example to fed 2.3 schema (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariuszkuc authored Feb 16, 2023
1 parent da09ba3 commit 549517d
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 32 deletions.
2 changes: 1 addition & 1 deletion implementations/graphql-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repositories {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("com.expediagroup:graphql-kotlin-spring-server:7.0.0-alpha.0")
implementation("com.expediagroup:graphql-kotlin-spring-server:6.3.6")
}

tasks.withType<KotlinCompile> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.demo

import com.expediagroup.graphql.generator.annotations.GraphQLDirective
import com.expediagroup.graphql.generator.federation.directives.ComposeDirective
import com.expediagroup.graphql.generator.federation.directives.LinkDirective
import com.expediagroup.graphql.server.Schema
import graphql.introspection.Introspection
import org.springframework.stereotype.Component

@LinkDirective(url = "https://myspecs.dev/myCustomDirective/v1.0", import = ["@custom"])
@ComposeDirective("@custom")
@Component
class CustomSchema : Schema

@GraphQLDirective(name = "custom", locations = [Introspection.DirectiveLocation.OBJECT])
annotation class CustomDirective
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.expediagroup.graphql.generator.annotations.GraphQLName
import com.expediagroup.graphql.generator.federation.directives.FieldSet
import com.expediagroup.graphql.generator.federation.directives.KeyDirective
import com.expediagroup.graphql.generator.federation.execution.FederatedTypeResolver
import com.expediagroup.graphql.generator.federation.execution.FederatedTypeSuspendResolver
import graphql.schema.DataFetchingEnvironment
import org.springframework.stereotype.Component

Expand Down Expand Up @@ -46,11 +45,13 @@ data class DeprecatedProduct(
}

@Component
class DeprecatedProductResolver : FederatedTypeSuspendResolver<DeprecatedProduct> {
class DeprecatedProductResolver : FederatedTypeResolver<DeprecatedProduct> {
override val typeName: String = "DeprecatedProduct"

override suspend fun resolve(
environment: DataFetchingEnvironment,
representation: Map<String, Any>
): DeprecatedProduct? = DeprecatedProduct.byReference(representation)
representations: List<Map<String, Any>>
): List<DeprecatedProduct?> {
return representations.map { DeprecatedProduct.byReference(it) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.demo.model

import com.expediagroup.graphql.generator.federation.directives.ExtendsDirective
import com.expediagroup.graphql.generator.federation.directives.ExternalDirective
import com.expediagroup.graphql.generator.federation.directives.FieldSet
import com.expediagroup.graphql.generator.federation.directives.InterfaceObjectDirective
import com.expediagroup.graphql.generator.federation.directives.KeyDirective
import com.expediagroup.graphql.generator.federation.execution.FederatedTypeResolver
import com.expediagroup.graphql.generator.scalars.ID
import graphql.schema.DataFetchingEnvironment
import org.springframework.stereotype.Component

/*
NOTE: @extends and @external are only required in graphql-kotlin v6, this was fixed in v7
type Inventory @extends @interfaceObject @key(fields: "id") {
id: ID! @external
deprecatedProducts: [DeprecatedProduct!]!
}
*/
@ExtendsDirective
@KeyDirective(fields = FieldSet("id"))
@InterfaceObjectDirective
data class Inventory(@ExternalDirective val id: ID) {
fun deprecatedProducts(): List<DeprecatedProduct> = listOf(DEPRECATED_PRODUCT)
}

@Component
class InventoryResolver : FederatedTypeResolver<Inventory> {
override val typeName: String = "Inventory"

override suspend fun resolve(
environment: DataFetchingEnvironment,
representations: List<Map<String, Any>>
): List<Inventory?> {
return representations.map {
if (it["id"] == "apollo-oss") {
Inventory(ID("apollo-oss"))
} else {
null
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.example.demo.model

import com.example.demo.CustomDirective
import com.expediagroup.graphql.generator.annotations.GraphQLName
import com.expediagroup.graphql.generator.federation.directives.FieldSet
import com.expediagroup.graphql.generator.federation.directives.KeyDirective
import com.expediagroup.graphql.generator.federation.directives.ProvidesDirective
import com.expediagroup.graphql.generator.federation.directives.TagDirective
import com.expediagroup.graphql.generator.federation.execution.FederatedTypeResolver
import com.expediagroup.graphql.generator.federation.execution.FederatedTypeSuspendResolver
import com.expediagroup.graphql.generator.scalars.ID
import graphql.schema.DataFetchingEnvironment
import org.springframework.stereotype.Component
Expand Down Expand Up @@ -47,6 +47,7 @@ type Product
research: [ProductResearch!]!
}
*/
@CustomDirective
@KeyDirective(fields = FieldSet("id"))
@KeyDirective(fields = FieldSet("sku package"))
@KeyDirective(fields = FieldSet("sku variation { id }"))
Expand Down Expand Up @@ -91,11 +92,13 @@ data class Product(
}

@Component
class ProductsResolver : FederatedTypeSuspendResolver<Product> {
class ProductsResolver : FederatedTypeResolver<Product> {
override val typeName: String = "Product"

override suspend fun resolve(
environment: DataFetchingEnvironment,
representation: Map<String, Any>
): Product? = Product.byReference(representation)
representations: List<Map<String, Any>>
): List<Product?> {
return representations.map { Product.byReference(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.example.demo.model
import com.expediagroup.graphql.generator.federation.directives.FieldSet
import com.expediagroup.graphql.generator.federation.directives.KeyDirective
import com.expediagroup.graphql.generator.federation.execution.FederatedTypeResolver
import com.expediagroup.graphql.generator.federation.execution.FederatedTypeSuspendResolver
import com.expediagroup.graphql.generator.scalars.ID
import graphql.schema.DataFetchingEnvironment
import org.springframework.stereotype.Component
Expand All @@ -25,19 +24,21 @@ data class ProductResearch(
)

@Component
class ProductResearchResolver : FederatedTypeSuspendResolver<ProductResearch> {
class ProductResearchResolver : FederatedTypeResolver<ProductResearch> {
override val typeName: String = "ProductResearch"

override suspend fun resolve(
environment: DataFetchingEnvironment,
representation: Map<String, Any>
): ProductResearch? {
val study = representation["study"]
val caseNumber = if (study is Map<*, *>) {
study["caseNumber"].toString()
} else {
null
representations: List<Map<String, Any>>
): List<ProductResearch?> {
return representations.map {
val study = it["study"]
val caseNumber = if (study is Map<*, *>) {
study["caseNumber"].toString()
} else {
null
}
RESEARCH_LIST.find { research -> research.study.caseNumber.value == caseNumber }
}
return RESEARCH_LIST.find { research -> research.study.caseNumber.value == caseNumber }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.example.demo.model

import com.expediagroup.graphql.generator.federation.directives.*
import com.expediagroup.graphql.generator.federation.execution.FederatedTypeResolver
import com.expediagroup.graphql.generator.federation.execution.FederatedTypeSuspendResolver
import com.expediagroup.graphql.generator.scalars.ID
import graphql.schema.DataFetchingEnvironment
import org.springframework.stereotype.Component
Expand Down Expand Up @@ -40,21 +39,23 @@ data class User(
}

@Component
class UserResolver : FederatedTypeSuspendResolver<User> {
class UserResolver : FederatedTypeResolver<User> {
override val typeName: String = "User"

override suspend fun resolve(
environment: DataFetchingEnvironment,
representation: Map<String, Any>
): User? {
val email = representation["email"]?.toString() ?: throw RuntimeException("invalid entity reference")
val user = User(email = ID(email), name = "Jane Smith", totalProductsCreated = 1337)
representation["totalProductsCreated"]?.toString()?.toIntOrNull()?.let { totalProductsCreated ->
user.totalProductsCreated = totalProductsCreated
representations: List<Map<String, Any>>
): List<User?> {
return representations.map {
val email = it["email"]?.toString() ?: throw RuntimeException("invalid entity reference")
val user = User(email = ID(email), name = "Jane Smith", totalProductsCreated = 1337)
it["totalProductsCreated"]?.toString()?.toIntOrNull()?.let { totalProductsCreated ->
user.totalProductsCreated = totalProductsCreated
}
it["yearsOfEmployment"]?.toString()?.toIntOrNull()?.let { yearsOfEmployment ->
user.yearsOfEmployment = yearsOfEmployment
}
user
}
representation["yearsOfEmployment"]?.toString()?.toIntOrNull()?.let { yearsOfEmployment ->
user.yearsOfEmployment = yearsOfEmployment
}
return user
}
}

0 comments on commit 549517d

Please sign in to comment.