Skip to content

Commit 8748198

Browse files
fix: unit tests dataFetchingEnvironment DataLoader extension functions (#1653)
1 parent 4f7d236 commit 8748198

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

servers/graphql-kotlin-server/src/main/kotlin/com/expediagroup/graphql/server/extensions/dataFetchingEnvironmentExtensions.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Expedia, Inc
2+
* Copyright 2023 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,17 +32,16 @@ import java.util.concurrent.CompletableFuture
3232
fun <K, V> DataFetchingEnvironment.getValueFromDataLoader(dataLoaderName: String, key: K): CompletableFuture<V> =
3333
getDataLoader<K, V>(dataLoaderName)?.load(
3434
key,
35-
this.getContext() ?: this.graphQlContext
35+
this.getContext<Any>() ?: this.graphQlContext
3636
) ?: throw MissingDataLoaderException(dataLoaderName)
3737

3838
/**
3939
* Helper method to get values from a registered DataLoader.
4040
*/
41-
fun <K, V> DataFetchingEnvironment.getValuesFromDataLoader(dataLoaderName: String, keys: List<K>): CompletableFuture<List<V>> =
42-
getDataLoader<K, V>(dataLoaderName)?.loadMany(
43-
keys,
44-
listOf(this.getContext() ?: this.graphQlContext)
45-
) ?: throw MissingDataLoaderException(dataLoaderName)
41+
fun <K, V> DataFetchingEnvironment.getValuesFromDataLoader(dataLoaderName: String, keys: List<K>): CompletableFuture<List<V>> = getDataLoader<K, V>(dataLoaderName)?.loadMany(
42+
keys,
43+
listOf(this.getContext<Any>() ?: this.graphQlContext)
44+
) ?: throw MissingDataLoaderException(dataLoaderName)
4645

4746
/**
4847
* Returns a value from the graphQLContext by KClass key

servers/graphql-kotlin-server/src/test/kotlin/com/expediagroup/graphql/server/extensions/DataFetchingEnvironmentExtensionsKtTest.kt

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Expedia, Inc
2+
* Copyright 2023 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package com.expediagroup.graphql.server.extensions
1818

1919
import com.expediagroup.graphql.server.exception.MissingDataLoaderException
20+
import graphql.GraphQLContext
2021
import graphql.schema.DataFetchingEnvironment
2122
import io.mockk.every
2223
import io.mockk.mockk
@@ -29,7 +30,7 @@ class DataFetchingEnvironmentExtensionsKtTest {
2930

3031
@Test
3132
fun `getting a dataloader passes when a matching name is found`() {
32-
val dataFetchingEnvironment: DataFetchingEnvironment = mockk {
33+
val dataFetchingEnvironment = mockk<DataFetchingEnvironment> {
3334
every { getContext<Any>() } returns mockk()
3435
every { getDataLoader<String, String>("foo") } returns mockk {
3536
every { load("bar", any()) } returns CompletableFuture.completedFuture("123")
@@ -41,9 +42,44 @@ class DataFetchingEnvironmentExtensionsKtTest {
4142
assertEquals("123", result.get())
4243
}
4344

45+
@Test
46+
fun `getting a dataloader with custom context passes when a matching name is found`() {
47+
val customContext = mockk<CustomContext>()
48+
val dataFetchingEnvironment = mockk<DataFetchingEnvironment> {
49+
every { getContext<CustomContext>() } returns customContext
50+
every { getDataLoader<String, String>("foo") } returns mockk {
51+
every { load("bar", ofType<CustomContext>()) } returns CompletableFuture.completedFuture("123")
52+
}
53+
}
54+
55+
val result: CompletableFuture<String> = dataFetchingEnvironment.getValueFromDataLoader("foo", "bar")
56+
57+
assertEquals("123", result.get())
58+
}
59+
60+
@Test
61+
fun `getting a dataloader with custom graphql context passes when a matching name is found`() {
62+
val customContext = mockk<CustomContext>()
63+
val dataFetchingEnvironment = mockk<DataFetchingEnvironment> {
64+
every { getContext<CustomContext>() } returns null
65+
every { graphQlContext } returns GraphQLContext.of(
66+
mapOf(
67+
CustomContext::class to customContext
68+
)
69+
)
70+
every { getDataLoader<String, String>("foo") } returns mockk {
71+
every { load("bar", ofType<GraphQLContext>()) } returns CompletableFuture.completedFuture("123")
72+
}
73+
}
74+
75+
val result: CompletableFuture<String> = dataFetchingEnvironment.getValueFromDataLoader("foo", "bar")
76+
77+
assertEquals("123", result.get())
78+
}
79+
4480
@Test
4581
fun `getting values from a dataloader based on a list of keys`() {
46-
val dataFetchingEnvironment: DataFetchingEnvironment = mockk {
82+
val dataFetchingEnvironment = mockk<DataFetchingEnvironment> {
4783
every { getContext<Any>() } returns mockk()
4884
every { getDataLoader<String, String>("foo") } returns mockk {
4985
every { loadMany(listOf("bar"), any()) } returns CompletableFuture.completedFuture(listOf("123"))
@@ -58,13 +94,15 @@ class DataFetchingEnvironmentExtensionsKtTest {
5894

5995
@Test
6096
fun `getting a dataloader throws exception when name not found`() {
61-
val dataFetchingEnvironment: DataFetchingEnvironment = mockk {
97+
val dataFetchingEnvironment = mockk<DataFetchingEnvironment> {
6298
every { getContext<Any>() } returns mockk()
6399
every { getDataLoader<String, String>("foo") } returns null
64100
}
65101

66-
assertFailsWith(MissingDataLoaderException::class) {
102+
assertFailsWith<MissingDataLoaderException> {
67103
dataFetchingEnvironment.getValueFromDataLoader<String, String>("foo", "bar")
68104
}
69105
}
106+
107+
data class CustomContext(val foo: String)
70108
}

0 commit comments

Comments
 (0)