Skip to content
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

feat: Use official graphql java 22 version #1980

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -9,6 +9,7 @@ dependencies {
api(libs.graphql.java) {
exclude(group = "com.graphql-java", module = "java-dataloader")
}
testImplementation(projects.graphqlKotlinServer)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to bring this dependency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's to have access to the dataFetchingEnvironmentExtensions in the tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is somewhat problematic as you end up with cyclic dependency -> i.e. graphql-kotlin-server depends on graphql-kotlin-dataloader-instrumentation as API dependency (link) and we depend here on the server as a test dependency.

Guessing Gradle is smart enough to handle the API vs test dependency.... but I'd avoid it and either copy over the extension locally to one of the tests or simply do what extension does....

testImplementation(libs.reactor.core)
testImplementation(libs.reactor.extensions)
}
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 @@ -23,6 +23,9 @@ import com.expediagroup.graphql.dataloader.instrumentation.fixture.domain.Missio
import com.expediagroup.graphql.dataloader.instrumentation.fixture.domain.Planet
import com.expediagroup.graphql.dataloader.instrumentation.fixture.extensions.toListOfNullables
import com.expediagroup.graphql.dataloader.instrumentation.fixture.repository.AstronautRepository
import com.expediagroup.graphql.server.exception.MissingDataLoaderException
import com.expediagroup.graphql.server.extensions.getValueFromDataLoader
import com.expediagroup.graphql.server.extensions.getValuesFromDataLoader
import graphql.GraphQLContext
import graphql.schema.DataFetchingEnvironment
import org.dataloader.DataLoader
Expand Down Expand Up @@ -57,9 +60,7 @@ class AstronautService {
request: AstronautServiceRequest,
environment: DataFetchingEnvironment
): CompletableFuture<Astronaut> =
environment
.getDataLoader<AstronautServiceRequest, Astronaut>("AstronautDataLoader")
.load(request)
environment.getValueFromDataLoader("AstronautDataLoader", request)

fun createAstronaut(
request: CreateAstronautServiceRequest
Expand All @@ -71,9 +72,7 @@ class AstronautService {
environment: DataFetchingEnvironment
): CompletableFuture<List<Astronaut?>> = when {
requests.isNotEmpty() -> {
environment
.getDataLoader<AstronautServiceRequest, Astronaut>("AstronautDataLoader")
.loadMany(requests)
environment.getValuesFromDataLoader("AstronautDataLoader", requests)
}
else -> {
AstronautRepository
Expand All @@ -89,7 +88,9 @@ class AstronautService {
environment: DataFetchingEnvironment
): CompletableFuture<List<Planet>> {
val missionsByAstronautDataLoader = environment.getDataLoader<MissionServiceRequest, List<Mission>>("MissionsByAstronautDataLoader")
?: throw MissingDataLoaderException("MissionsByAstronautDataLoader")
val planetsByMissionDataLoader = environment.getDataLoader<PlanetServiceRequest, List<Planet>>("PlanetsByMissionDataLoader")
?: throw MissingDataLoaderException("PlanetsByMissionDataLoader")
return missionsByAstronautDataLoader
.load(MissionServiceRequest(0, astronautId = request.id))
.thenCompose { missions ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import com.expediagroup.graphql.dataloader.KotlinDataLoader
import com.expediagroup.graphql.dataloader.instrumentation.fixture.domain.Mission
import com.expediagroup.graphql.dataloader.instrumentation.fixture.extensions.toListOfNullables
import com.expediagroup.graphql.dataloader.instrumentation.fixture.repository.MissionRepository
import com.expediagroup.graphql.server.extensions.getValueFromDataLoader
import com.expediagroup.graphql.server.extensions.getValuesFromDataLoader
import graphql.GraphQLContext
import graphql.schema.DataFetchingEnvironment
import org.dataloader.DataLoader
Expand Down Expand Up @@ -64,18 +66,14 @@ class MissionService {
request: MissionServiceRequest,
environment: DataFetchingEnvironment
): CompletableFuture<Mission> =
environment
.getDataLoader<MissionServiceRequest, Mission>("MissionDataLoader")
.load(request)
environment.getValueFromDataLoader("MissionDataLoader", request)

fun getMissions(
requests: List<MissionServiceRequest>,
environment: DataFetchingEnvironment
): CompletableFuture<List<Mission?>> = when {
requests.isNotEmpty() -> {
environment
.getDataLoader<MissionServiceRequest, Mission>("MissionDataLoader")
.loadMany(requests)
environment.getValuesFromDataLoader("MissionDataLoader", requests)
}
else -> {
MissionRepository
Expand All @@ -90,7 +88,5 @@ class MissionService {
request: MissionServiceRequest,
environment: DataFetchingEnvironment
): CompletableFuture<List<Mission>> =
environment
.getDataLoader<MissionServiceRequest, List<Mission>>("MissionsByAstronautDataLoader")
.load(request)
environment.getValueFromDataLoader("MissionsByAstronautDataLoader", request)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.expediagroup.graphql.dataloader.instrumentation.fixture.datafetcher
import com.expediagroup.graphql.dataloader.KotlinDataLoader
import com.expediagroup.graphql.dataloader.instrumentation.fixture.domain.Planet
import com.expediagroup.graphql.dataloader.instrumentation.fixture.repository.PlanetRepository
import com.expediagroup.graphql.server.extensions.getValueFromDataLoader
import graphql.GraphQLContext
import graphql.schema.DataFetchingEnvironment
import org.dataloader.DataLoader
Expand Down Expand Up @@ -48,7 +49,5 @@ class PlanetService {
request: PlanetServiceRequest,
environment: DataFetchingEnvironment
): CompletableFuture<List<Planet>> =
environment
.getDataLoader<PlanetServiceRequest, List<Planet>>("PlanetsByMissionDataLoader")
.load(request)
environment.getValueFromDataLoader("PlanetsByMissionDataLoader", request)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.expediagroup.graphql.dataloader.KotlinDataLoader
import com.expediagroup.graphql.dataloader.instrumentation.fixture.domain.Product
import com.expediagroup.graphql.dataloader.instrumentation.fixture.extensions.toListOfNullables
import com.expediagroup.graphql.dataloader.instrumentation.fixture.repository.ProductRepository
import com.expediagroup.graphql.server.extensions.getValueFromDataLoader
import graphql.GraphQLContext
import graphql.schema.DataFetchingEnvironment
import org.dataloader.DataLoader
Expand Down Expand Up @@ -51,7 +52,5 @@ class ProductService {
request: ProductServiceRequest,
environment: DataFetchingEnvironment
): CompletableFuture<Product> =
environment
.getDataLoader<ProductServiceRequest, Product>("ProductDataLoader")
.load(request)
environment.getValueFromDataLoader("ProductDataLoader", request)
}
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? = ""
)
Loading
Loading