Skip to content

Commit a3629cb

Browse files
committed
1 parent f919ced commit a3629cb

File tree

7 files changed

+136
-51
lines changed

7 files changed

+136
-51
lines changed

kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/execution/ParallelRequestExecutor.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class ParallelRequestExecutor(val schema: DefaultSchema) : RequestExecutor {
204204
when (child) {
205205
is Execution.Fragment -> objectNode.setAll<JsonNode>(handleFragment(ctx, value, child))
206206
else -> {
207-
val (key, jsonNode) = handleProperty(ctx, value, child, type)
207+
val (key, jsonNode) = handleProperty(ctx, value, child, type) ?: continue
208208
objectNode.merge(key, jsonNode)
209209
}
210210
}
@@ -217,7 +217,7 @@ class ParallelRequestExecutor(val schema: DefaultSchema) : RequestExecutor {
217217
value: T,
218218
child: Execution,
219219
type: Type
220-
): Pair<String, JsonNode?> {
220+
): Pair<String, JsonNode?>? {
221221
when (child) {
222222
//Union is subclass of Node so check it first
223223
is Execution.Union -> {
@@ -233,7 +233,7 @@ class ParallelRequestExecutor(val schema: DefaultSchema) : RequestExecutor {
233233
is Execution.Node -> {
234234
val field = type.unwrapped()[child.key]
235235
?: throw IllegalStateException("Execution unit ${child.key} is not contained by operation return type")
236-
return child.aliasOrKey to createPropertyNode(ctx, value, child, field)
236+
return child.aliasOrKey to (createPropertyNode(ctx, value, child, field) ?: return null)
237237
}
238238

239239
else -> {
@@ -256,7 +256,7 @@ class ParallelRequestExecutor(val schema: DefaultSchema) : RequestExecutor {
256256
return container.elements.flatMap { child ->
257257
when (child) {
258258
is Execution.Fragment -> handleFragment(ctx, value, child).toList()
259-
else -> listOf(handleProperty(ctx, value, child, expectedType))
259+
else -> listOfNotNull(handleProperty(ctx, value, child, expectedType))
260260
}
261261
}.fold(mutableMapOf()) { map, entry -> map.merge(entry.first, entry.second) }
262262
}

kgraphql/src/test/kotlin/com/apurebase/kgraphql/DataLoaderTest.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ class DataLoaderTest {
2828
private val beinisson = Person(2, "Høgni", "Beinisson")
2929
private val juul = Person(3, "Høgni", "Juul")
3030
private val otherOne = Person(4, "The other one", "??")
31-
32-
val allPeople = listOf(jogvan, beinisson, juul, otherOne)
31+
private val allPeople = listOf(jogvan, beinisson, juul, otherOne)
3332

3433
private val colleagues = mapOf(
3534
jogvan.id to listOf(beinisson, juul),
@@ -159,12 +158,12 @@ class DataLoaderTest {
159158
loader { keys ->
160159
println("== Running [children] loader with keys: $keys ==")
161160
counters.abcChildren.loader.incrementAndGet()
162-
keys.map {
163-
val (a1, a2) = when (it) {
161+
keys.map { key ->
162+
val (a1, a2) = when (key) {
164163
"Testing 1" -> "Hello" to "World"
165164
"Testing 2" -> "Fizz" to "Buzz"
166165
"Testing 3" -> "Jógvan" to "Høgni"
167-
else -> "${it}Nest-0" to "${it}Nest-1"
166+
else -> "${key}Nest-0" to "${key}Nest-1"
168167
}
169168
delay(1)
170169
ExecutionResult.Success(
@@ -384,7 +383,7 @@ class DataLoaderTest {
384383
val result = schema.executeBlocking(query).also(::println).deserialize()
385384

386385
result.extract<String>("data/abc[0]/person/fullName") shouldBeEqualTo "${jogvan.firstName} ${jogvan.lastName}"
387-
extractOrNull<String>(result, "data/abc[1]/person") shouldBeEqualTo null
386+
result.extract<String?>("data/abc[1]/person") shouldBeEqualTo null
388387
result.extract<String>("data/abc[2]/person/fullName") shouldBeEqualTo "${juul.firstName} ${juul.lastName}"
389388
}
390389
}

kgraphql/src/test/kotlin/com/apurebase/kgraphql/TestUtils.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,20 @@ fun <T> Map<*, *>.extract(path: String): T {
3030
try {
3131
return tokens.fold(this as Any?) { workingMap, token ->
3232
if (token.contains('[')) {
33-
val list = (workingMap as Map<*, *>)[token.substringBefore('[')]
33+
if (!(workingMap as Map<*, *>).containsKey(token.substringBefore('['))) throw IllegalArgumentException()
34+
val list = workingMap[token.substringBefore('[')]
3435
val index = token.substring(token.indexOf('[') + 1, token.length - 1).toInt()
3536
(list as List<*>)[index]
3637
} else {
37-
(workingMap as Map<*, *>)[token]
38+
if (!(workingMap as Map<*, *>).containsKey(token)) throw IllegalArgumentException()
39+
workingMap[token]
3840
}
3941
} as T
4042
} catch (e: Exception) {
4143
throw IllegalArgumentException("Path: $path does not exist in map: $this", e)
4244
}
4345
}
4446

45-
fun <T> extractOrNull(map: Map<*, *>, path: String): T? {
46-
return try {
47-
map.extract(path)
48-
} catch (e: IllegalArgumentException) {
49-
null
50-
}
51-
}
52-
5347
fun defaultSchema(block: SchemaBuilder.() -> Unit): DefaultSchema {
5448
return SchemaBuilder().apply(block).build() as DefaultSchema
5549
}

kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/BaseSchemaTest.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.apurebase.kgraphql.integration
33
import com.apurebase.kgraphql.Actor
44
import com.apurebase.kgraphql.ActorCalculateAgeInput
55
import com.apurebase.kgraphql.ActorInput
6+
import com.apurebase.kgraphql.Context
67
import com.apurebase.kgraphql.Director
78
import com.apurebase.kgraphql.Film
89
import com.apurebase.kgraphql.FilmType
@@ -12,6 +13,7 @@ import com.apurebase.kgraphql.Person
1213
import com.apurebase.kgraphql.Scenario
1314
import com.apurebase.kgraphql.defaultSchema
1415
import com.apurebase.kgraphql.deserialize
16+
import com.apurebase.kgraphql.schema.execution.ExecutionOptions
1517
import org.junit.jupiter.api.AfterEach
1618

1719

@@ -333,9 +335,14 @@ abstract class BaseSchemaTest {
333335
@AfterEach
334336
fun cleanup() = createdActors.clear()
335337

336-
fun execute(query: String, variables: String? = null) = testedSchema
337-
.executeBlocking(query, variables)
338+
fun execute(
339+
query: String,
340+
variables: String? = null,
341+
context: Context = Context(emptyMap()),
342+
options: ExecutionOptions = ExecutionOptions(),
343+
operationName: String? = null,
344+
) = testedSchema
345+
.executeBlocking(query, variables, context, options, operationName)
338346
.also(::println)
339347
.deserialize()
340-
341348
}

kgraphql/src/test/kotlin/com/apurebase/kgraphql/specification/language/FragmentsSpecificationTest.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import org.amshove.kluent.withMessage
1111
import org.hamcrest.CoreMatchers.*
1212
import org.hamcrest.MatcherAssert.assertThat
1313
import org.junit.jupiter.api.Test
14+
import org.junit.jupiter.api.assertThrows
1415

1516
@Specification("2.8 Fragments")
1617
class FragmentsSpecificationTest {
1718

1819
val age = 232
1920

20-
val actorName = "Boguś Linda"
21+
private val actorName = "Boguś Linda"
2122

2223
val id = "BLinda"
2324

@@ -29,7 +30,7 @@ class FragmentsSpecificationTest {
2930
}
3031
}
3132

32-
val BaseTestSchema = object : BaseSchemaTest() {}
33+
private val baseTestSchema = object : BaseSchemaTest() {}
3334

3435
@Test
3536
fun `fragment's fields are added to the query at the same level as the fragment invocation`() {
@@ -76,25 +77,25 @@ class FragmentsSpecificationTest {
7677
)
7778
)
7879
assertNoErrors(response)
79-
assertThat(extractOrNull(response, "data/actor/actualActor/name"), equalTo("Boguś Linda"))
80-
assertThat(extractOrNull(response, "data/actor/actualActor/age"), nullValue())
80+
assertThat(response.extract("data/actor/actualActor/name"), equalTo("Boguś Linda"))
81+
assertThrows<IllegalArgumentException> { response.extract("data/actor/actualActor/age") }
8182
}
8283

8384
@Test
8485
fun `query with inline fragment with type condition`() {
85-
val map = BaseTestSchema.execute("{people{name, age, ... on Actor {isOld} ... on Director {favActors{name}}}}")
86+
val map = baseTestSchema.execute("{people{name, age, ... on Actor {isOld} ... on Director {favActors{name}}}}")
8687
assertNoErrors(map)
8788
for (i in map.extract<List<*>>("data/people").indices) {
8889
val name = map.extract<String>("data/people[$i]/name")
8990
when (name) {
9091
"David Fincher" /* director */ -> {
9192
assertThat(map.extract<List<*>>("data/people[$i]/favActors"), notNullValue())
92-
assertThat(extractOrNull<Boolean>(map, "data/people[$i]/isOld"), nullValue())
93+
assertThrows<IllegalArgumentException> { map.extract("data/people[$i]/isOld") }
9394
}
9495

9596
"Brad Pitt" /* actor */ -> {
9697
assertThat(map.extract<Boolean>("data/people[$i]/isOld"), notNullValue())
97-
assertThat(extractOrNull<List<*>>(map, "data/people[$i]/favActors"), nullValue())
98+
assertThrows<IllegalArgumentException> { map.extract("data/people[$i]/favActors") }
9899
}
99100
}
100101
}
@@ -103,27 +104,27 @@ class FragmentsSpecificationTest {
103104
@Test
104105
fun `query with external fragment with type condition`() {
105106
val map =
106-
BaseTestSchema.execute("{people{name, age ...act ...dir}} fragment act on Actor {isOld} fragment dir on Director {favActors{name}}")
107+
baseTestSchema.execute("{people{name, age ...act ...dir}} fragment act on Actor {isOld} fragment dir on Director {favActors{name}}")
107108
assertNoErrors(map)
108109
for (i in map.extract<List<*>>("data/people").indices) {
109110
val name = map.extract<String>("data/people[$i]/name")
110111
when (name) {
111112
"David Fincher" /* director */ -> {
112113
assertThat(map.extract<List<*>>("data/people[$i]/favActors"), notNullValue())
113-
assertThat(extractOrNull<Boolean>(map, "data/people[$i]/isOld"), nullValue())
114+
assertThrows<IllegalArgumentException> { map.extract("data/people[$i]/isOld") }
114115
}
115116

116117
"Brad Pitt" /* actor */ -> {
117118
assertThat(map.extract<Boolean>("data/people[$i]/isOld"), notNullValue())
118-
assertThat(extractOrNull<List<*>>(map, "data/people[$i]/favActors"), nullValue())
119+
assertThrows<IllegalArgumentException> { map.extract("data/people[$i]/favActors") }
119120
}
120121
}
121122
}
122123
}
123124

124125
@Test
125126
fun `multiple nested fragments are handled`() {
126-
val map = BaseTestSchema.execute(INTROSPECTION_QUERY)
127+
val map = baseTestSchema.execute(INTROSPECTION_QUERY)
127128
val fields = map.extract<List<Map<String, *>>>("data/__schema/types[0]/fields")
128129

129130
fields.forEach { field ->
@@ -134,7 +135,7 @@ class FragmentsSpecificationTest {
134135
@Test
135136
fun `queries with recursive fragments are denied`() {
136137
invoking {
137-
BaseTestSchema.execute(
138+
baseTestSchema.execute(
138139
"""
139140
query IntrospectionQuery {
140141
__schema {
@@ -160,7 +161,7 @@ class FragmentsSpecificationTest {
160161
@Test
161162
fun `queries with duplicated fragments are denied`() {
162163
invoking {
163-
BaseTestSchema.execute(
164+
baseTestSchema.execute(
164165
"""
165166
{
166167
film {

0 commit comments

Comments
 (0)