Skip to content

Commit

Permalink
feat: Use official graphql java 22 version (#1980)
Browse files Browse the repository at this point in the history
### 📝 Description

Follow up to #1967 to
use an official graphql java release.
  • Loading branch information
eocantu authored Jun 20, 2024
1 parent 60879c5 commit 793c5ed
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.expediagroup.graphql.examples.server.ktor.schema
import com.expediagroup.graphql.examples.server.ktor.schema.dataloaders.BookDataLoader
import com.expediagroup.graphql.examples.server.ktor.schema.models.Book
import com.expediagroup.graphql.generator.annotations.GraphQLDescription
import com.expediagroup.graphql.server.extensions.getValuesFromDataLoader
import com.expediagroup.graphql.server.operations.Query
import graphql.schema.DataFetchingEnvironment
import java.util.concurrent.CompletableFuture
Expand All @@ -30,8 +31,7 @@ class BookQueryService : Query {
@GraphQLDescription("Return list of books based on BookSearchParameter options")
@Suppress("unused")
fun searchBooks(params: BookSearchParameters, dfe: DataFetchingEnvironment): CompletableFuture<List<Book>> =
dfe.getDataLoader<Int, Book>(BookDataLoader.dataLoaderName)
.loadMany(params.ids)
dfe.getValuesFromDataLoader(BookDataLoader.dataLoaderName, params.ids)
}

data class BookSearchParameters(val ids: List<Int>)
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package com.expediagroup.graphql.examples.server.ktor.schema

import com.expediagroup.graphql.examples.server.ktor.schema.dataloaders.CourseDataLoader
import com.expediagroup.graphql.examples.server.ktor.schema.models.Course
import com.expediagroup.graphql.server.extensions.getValuesFromDataLoader
import com.expediagroup.graphql.server.operations.Query
import graphql.schema.DataFetchingEnvironment
import java.util.concurrent.CompletableFuture

class CourseQueryService : Query {
fun searchCourses(params: CourseSearchParameters, dfe: DataFetchingEnvironment): CompletableFuture<List<Course>> =
dfe.getDataLoader<Int, Course>(CourseDataLoader.dataLoaderName)
.loadMany(params.ids)
dfe.getValuesFromDataLoader(CourseDataLoader.dataLoaderName, params.ids)
}

data class CourseSearchParameters(val ids: List<Int>)
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package com.expediagroup.graphql.examples.server.ktor.schema

import com.expediagroup.graphql.examples.server.ktor.schema.dataloaders.UniversityDataLoader
import com.expediagroup.graphql.examples.server.ktor.schema.models.University
import com.expediagroup.graphql.server.extensions.getValuesFromDataLoader
import com.expediagroup.graphql.server.operations.Query
import graphql.schema.DataFetchingEnvironment
import java.util.concurrent.CompletableFuture

class UniversityQueryService : Query {
fun searchUniversities(params: UniversitySearchParameters, dfe: DataFetchingEnvironment): CompletableFuture<List<University>> =
dfe.getDataLoader<Int, University>(UniversityDataLoader.dataLoaderName)
.loadMany(params.ids)
dfe.getValuesFromDataLoader(UniversityDataLoader.dataLoaderName, params.ids)
}

data class UniversitySearchParameters(val ids: List<Int>)
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DataLoaderQuery : Query {
class CompanyDataFetcher : DataFetcher<CompletableFuture<Company>> {

override fun get(environment: DataFetchingEnvironment): CompletableFuture<Company> {
val companyId = environment.getSource<Employee>().companyId
val companyId = environment.getSource<Employee>()?.companyId ?: throw IllegalArgumentException("companyId is null")
return environment.getValueFromDataLoader(CompanyDataLoader.name, companyId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ object ProductGraphQL {
private val runtimeWiring = RuntimeWiring.newRuntimeWiring().apply {
type(
TypeRuntimeWiring.newTypeWiring("Query")
.dataFetcher("product") { products[it.getArgument<String>("id").toInt()] }
.dataFetcher("product") {
it.getArgument<String>("id")?.let { id -> products[id.toInt()] }
}
)
}.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,16 @@ object AstronautGraphQL {

private val astronautService = AstronautService()
private val astronautDataFetcher = DataFetcher { environment ->
val astronautId = environment.getArgument<String>("id")?.toInt() ?: throw IllegalArgumentException("Astronaut ID is null")
astronautService.getAstronaut(
AstronautServiceRequest(
environment.getArgument<String>("id").toInt()
),
AstronautServiceRequest(astronautId),
environment
)
}
private val createAstronautDataFetcher = DataFetcher { environment ->
val astronautName = environment.getArgument<String>("name") ?: throw IllegalArgumentException("Astronaut name is null")
astronautService.createAstronaut(
CreateAstronautServiceRequest(
environment.getArgument("name")
)
CreateAstronautServiceRequest(astronautName)
)
}
private val astronautsDataFetcher = DataFetcher { environment ->
Expand All @@ -118,10 +116,9 @@ object AstronautGraphQL {

private val missionService = MissionService()
private val missionDataFetcher = DataFetcher { environment ->
val missionId = environment.getArgument<String>("id")?.toInt() ?: throw IllegalArgumentException("Mission ID is null")
missionService.getMission(
MissionServiceRequest(
environment.getArgument<String>("id").toInt()
),
MissionServiceRequest(missionId),
environment
)
}
Expand All @@ -134,26 +131,26 @@ object AstronautGraphQL {
)
}
private val missionsByAstronautDataFetcher = DataFetcher { environment ->
val astronaut = environment.getSource<Astronaut>()
val astronautId = environment.getSource<Astronaut>()?.id ?: throw IllegalArgumentException("Astronaut ID is null")
missionService
.getMissionsByAstronaut(
MissionServiceRequest(0, astronaut.id),
MissionServiceRequest(0, astronautId),
environment
)
}

private val planetService = PlanetService()
private val planetsByMissionDataFetcher = DataFetcher { environment ->
val mission = environment.getSource<Mission>()
val missionId = environment.getSource<Mission>()?.id ?: throw IllegalArgumentException("Mission ID is null")
planetService.getPlanets(
PlanetServiceRequest(0, mission.id),
PlanetServiceRequest(0, missionId),
environment
)
}
private val planetsByAstronautDataFetcher = DataFetcher { environment ->
val astronaut = environment.getSource<Astronaut>()
val astronautId = environment.getSource<Astronaut>()?.id ?: throw IllegalArgumentException("Astronaut ID is null")
astronautService.getPlanets(
AstronautServiceRequest(astronaut.id),
AstronautServiceRequest(astronautId),
environment
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object ProductGraphQL {
private val productService = ProductService()

private val productDataFetcher = DataFetcher<CompletableFuture<Product>> { environment ->
val productId = environment.getArgument<String>("id").toInt()
val productId = environment.getArgument<String>("id")?.toInt() ?: throw IllegalArgumentException("Product ID is null")
val selectionFields = environment.selectionSet.immediateFields.map(SelectedField::getName)
productService.getProduct(
ProductServiceRequest(productId, selectionFields),
Expand All @@ -73,7 +73,7 @@ object ProductGraphQL {
}

private val productSummaryDataFetcher = DataFetcher<CompletableFuture<ProductSummary>> { environment ->
val productId = environment.getArgument<String>("productId").toInt()
val productId = environment.getArgument<String>("productId")?.toInt() ?: throw IllegalArgumentException("Product ID is null")
val selectionFields = listOf("summary")
productService.getProduct(
ProductServiceRequest(productId, selectionFields),
Expand All @@ -82,7 +82,7 @@ object ProductGraphQL {
}

private val productDetailsDataFetcher = DataFetcher<CompletableFuture<ProductDetails>> { environment ->
val productId = environment.getArgument<String>("productId").toInt()
val productId = environment.getArgument<String>("productId")?.toInt() ?: throw IllegalArgumentException("Product ID is null")
val selectionFields = listOf("details")
productService.getProduct(
ProductServiceRequest(productId, selectionFields),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AstronautService {
): CompletableFuture<Astronaut> =
environment
.getDataLoader<AstronautServiceRequest, Astronaut>("AstronautDataLoader")
.load(request)
?.load(request) ?: throw IllegalArgumentException("No data loader called AstronautDataLoader was found")

fun createAstronaut(
request: CreateAstronautServiceRequest
Expand All @@ -73,7 +73,7 @@ class AstronautService {
requests.isNotEmpty() -> {
environment
.getDataLoader<AstronautServiceRequest, Astronaut>("AstronautDataLoader")
.loadMany(requests)
?.loadMany(requests) ?: throw IllegalArgumentException("No data loader called AstronautDataLoader was found")
}
else -> {
AstronautRepository
Expand All @@ -89,7 +89,9 @@ class AstronautService {
environment: DataFetchingEnvironment
): CompletableFuture<List<Planet>> {
val missionsByAstronautDataLoader = environment.getDataLoader<MissionServiceRequest, List<Mission>>("MissionsByAstronautDataLoader")
?: throw IllegalArgumentException("No data loader called MissionsByAstronautDataLoader was found")
val planetsByMissionDataLoader = environment.getDataLoader<PlanetServiceRequest, List<Planet>>("PlanetsByMissionDataLoader")
?: throw IllegalArgumentException("No data loader called PlanetsByMissionDataLoader was found")
return missionsByAstronautDataLoader
.load(MissionServiceRequest(0, astronautId = request.id))
.thenCompose { missions ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class MissionService {
): CompletableFuture<Mission> =
environment
.getDataLoader<MissionServiceRequest, Mission>("MissionDataLoader")
.load(request)
?.load(request) ?: throw IllegalArgumentException("No data loader called MissionDataLoader was found")

fun getMissions(
requests: List<MissionServiceRequest>,
Expand All @@ -75,7 +75,7 @@ class MissionService {
requests.isNotEmpty() -> {
environment
.getDataLoader<MissionServiceRequest, Mission>("MissionDataLoader")
.loadMany(requests)
?.loadMany(requests) ?: throw IllegalArgumentException("No data loader called MissionDataLoader was found")
}
else -> {
MissionRepository
Expand All @@ -92,5 +92,5 @@ class MissionService {
): CompletableFuture<List<Mission>> =
environment
.getDataLoader<MissionServiceRequest, List<Mission>>("MissionsByAstronautDataLoader")
.load(request)
?.load(request) ?: throw IllegalArgumentException("No data loader called MissionsByAstronautDataLoader was found")
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ class PlanetService {
): CompletableFuture<List<Planet>> =
environment
.getDataLoader<PlanetServiceRequest, List<Planet>>("PlanetsByMissionDataLoader")
.load(request)
?.load(request) ?: throw IllegalStateException("No data loader called PlanetsByMissionDataLoader was found")
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ class ProductService {
): CompletableFuture<Product> =
environment
.getDataLoader<ProductServiceRequest, Product>("ProductDataLoader")
.load(request)
?.load(request) ?: throw IllegalStateException("No data loader called ProductDataLoader was found")
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ open class EntitiesDataFetcher(
* @return list of resolved nullable entities
*/
override fun get(env: DataFetchingEnvironment): CompletableFuture<DataFetcherResult<List<Any?>>> {
val representations: List<Map<String, Any>> = env.getArgument(REPRESENTATIONS)
val representations: List<Map<String, Any>> = env.getArgumentOrDefault(REPRESENTATIONS, listOf(emptyMap()))

val representationsWithoutResolver = mutableListOf<IndexedValue<Map<String, Any>>>()
val entitiesWithPromiseResolver = mutableListOf<ResolvableEntity<FederatedTypePromiseResolver<*>>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class EntitiesDataFetcherTest {
mapOf<String, Any>("__typename" to "User", "userId" to 123, "name" to "testName")
)
val env = mockk<DataFetchingEnvironment> {
every { getArgument<Any>(any()) } returns representations
every { getArgumentOrDefault<Any>(any(), any()) } returns representations
every { graphQlContext } returns GraphQLContext.newContext().build()
}

Expand All @@ -57,7 +57,7 @@ class EntitiesDataFetcherTest {
mapOf<String, Any>("__typename" to "Author", "authorId" to 1)
)
val env = mockk<DataFetchingEnvironment> {
every { getArgument<Any>(any()) } returns representations
every { getArgumentOrDefault<Any>(any(), any()) } returns representations
}
val result = resolver.get(env).get()
verifyData(result.data, Author(1, "Author 1"))
Expand All @@ -72,7 +72,7 @@ class EntitiesDataFetcherTest {
mapOf<String, Any>("__typename" to "User", "userId" to 123, "name" to "testName")
)
val env = mockk<DataFetchingEnvironment> {
every { getArgument<Any>(any()) } returns representations
every { getArgumentOrDefault<Any>(any(), any()) } returns representations
}

val result = resolver.get(env).get()
Expand All @@ -86,7 +86,7 @@ class EntitiesDataFetcherTest {
val mockUserResolver = mockk<FederatedTypeSuspendResolver<*>> { every { typeName } returns "User" }
val resolver = EntitiesDataFetcher(mockBookResolver, mockUserResolver)
val env = mockk<DataFetchingEnvironment> {
every { getArgument<Any>(any()) } returns listOf(emptyMap<String, Any>())
every { getArgumentOrDefault<Any>(any(), any()) } returns listOf(emptyMap<String, Any>())
every { graphQlContext } returns GraphQLContext.newContext().build()
}

Expand All @@ -100,7 +100,7 @@ class EntitiesDataFetcherTest {
val resolver = EntitiesDataFetcher(emptyList())
val representations = listOf(mapOf<String, Any>("__typename" to "User", "userId" to 123, "name" to "testName"))
val env = mockk<DataFetchingEnvironment> {
every { getArgument<Any>(any()) } returns representations
every { getArgumentOrDefault<Any>(any(), any()) } returns representations
every { graphQlContext } returns GraphQLContext.newContext().build()
}

Expand All @@ -118,7 +118,7 @@ class EntitiesDataFetcherTest {
val resolver = EntitiesDataFetcher(listOf(mockUserResolver))
val representations = listOf(mapOf<String, Any>("__typename" to "User", "userId" to 123, "name" to "testName"))
val env: DataFetchingEnvironment = mockk {
every { getArgument<Any>(any()) } returns representations
every { getArgumentOrDefault<Any>(any(), any()) } returns representations
every { graphQlContext } returns GraphQLContext.newContext().build()
}

Expand All @@ -136,7 +136,7 @@ class EntitiesDataFetcherTest {
}
val representations = listOf(user1.toRepresentation(), book.toRepresentation(), user2.toRepresentation())
val env = mockk<DataFetchingEnvironment> {
every { getArgument<Any>(any()) } returns representations
every { getArgumentOrDefault<Any>(any(), any()) } returns representations
every { graphQlContext } returns GraphQLContext.newContext().build()
}

Expand All @@ -163,7 +163,7 @@ class EntitiesDataFetcherTest {
}
val representations = listOf(user.toRepresentation(), book.toRepresentation())
val env = mockk<DataFetchingEnvironment> {
every { getArgument<Any>(any()) } returns representations
every { getArgumentOrDefault<Any>(any(), any()) } returns representations
every { graphQlContext } returns GraphQLContext.newContext().build()
}

Expand Down Expand Up @@ -195,7 +195,7 @@ class EntitiesDataFetcherTest {
)

val env = mockk<DataFetchingEnvironment> {
every { getArgument<Any>(any()) } returns representations
every { getArgumentOrDefault<Any>(any(), any()) } returns representations
every { graphQlContext } returns GraphQLContext.newContext().build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class QueryWithDeprecatedFields {
data class ClassWithDeprecatedField(
val something: String,
@Deprecated("this field is deprecated")
val deprecatedField: String,
val deprecatedField: String?,
@GraphQLDeprecated("this field is also deprecated")
val graphqlDeprecatedField: String = ""
val graphqlDeprecatedField: String? = ""
)
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ android-plugin = "8.1.1"
classgraph = "4.8.170"
dataloader = "3.3.0"
federation = "4.4.1"
graphql-java = "0.0.0-2024-04-23-expedia-branch-1"
graphql-java = "22.1"
graalvm = "0.10.1"
jackson = "2.15.4"
# kotlin version has to match the compile-testing compiler version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.expediagroup.graalvm.schema

import com.expediagroup.graalvm.schema.dataloader.EXAMPLE_LOADER
import com.expediagroup.graphql.generator.scalars.ID
import com.expediagroup.graphql.server.extensions.getValueFromDataLoader
import com.expediagroup.graphql.server.operations.Query
import graphql.schema.DataFetchingEnvironment
import kotlinx.coroutines.coroutineScope
Expand All @@ -33,7 +34,7 @@ class AsyncQuery : Query {

fun future(): CompletableFuture<Int> = CompletableFuture.completedFuture(random.nextInt())

fun dataLoader(env: DataFetchingEnvironment, id: ID): CompletableFuture<String> = env.getDataLoader<ID, String>(EXAMPLE_LOADER).load(id)
fun dataLoader(env: DataFetchingEnvironment, id: ID): CompletableFuture<String> = env.getValueFromDataLoader(EXAMPLE_LOADER, id)

suspend fun coroutine(): Int = coroutineScope {
delay(10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.expediagroup.graphql.generator.execution.FlowSubscriptionExecutionStr
import com.expediagroup.graphql.generator.extensions.toGraphQLContext
import com.expediagroup.graphql.generator.hooks.FlowSubscriptionSchemaGeneratorHooks
import com.expediagroup.graphql.generator.toSchema
import com.expediagroup.graphql.server.extensions.getValueFromDataLoader
import com.expediagroup.graphql.server.types.GraphQLBatchRequest
import com.expediagroup.graphql.server.types.GraphQLBatchResponse
import com.expediagroup.graphql.server.types.GraphQLRequest
Expand Down Expand Up @@ -389,7 +390,7 @@ class GraphQLRequestHandlerTest {
id: Int,
dataFetchingEnvironment: DataFetchingEnvironment
): CompletableFuture<User> =
dataFetchingEnvironment.getDataLoader<Int, User>("UserDataLoader").load(id)
dataFetchingEnvironment.getValueFromDataLoader("UserDataLoader", id)

fun hello(name: String): String = "Hello $name!"

Expand Down

0 comments on commit 793c5ed

Please sign in to comment.