Skip to content

Commit 38871d6

Browse files
authored
Merge pull request #66 from aPureBase/65-top-field-directives
Fixed issue top level fields with directives are being ignored
2 parents cf6942e + 8671e24 commit 38871d6

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,19 @@ class ParallelRequestExecutor(val schema: DefaultSchema) : RequestExecutor, Coro
5252
val data = root.putObject("data")
5353

5454
val resultMap = plan.toMapAsync {
55-
writeOperation(
55+
val ctx = ExecutionContext(Variables(schema, variables, it.variables), context)
56+
if (determineInclude(ctx, it)) writeOperation(
5657
isSubscription = plan.isSubscription,
57-
ctx = ExecutionContext(Variables(schema, variables, it.variables), context),
58+
ctx = ctx,
5859
node = it,
5960
operation = it.field as Field.Function<*, *>
60-
)
61+
) else null
6162
}
6263

6364
for (operation in plan) {
64-
data.set(operation.aliasOrKey, resultMap[operation])
65+
if (resultMap[operation] != null) { // Remove all by skip/include directives
66+
data.set(operation.aliasOrKey, resultMap[operation])
67+
}
6568
}
6669

6770
return objectWriter.writeValueAsString(root)

kgraphql/src/test/kotlin/com/apurebase/kgraphql/specification/typesystem/DirectivesSpecificationTest.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.apurebase.kgraphql.specification.typesystem
22

3-
import com.apurebase.kgraphql.Specification
4-
import com.apurebase.kgraphql.extractOrNull
3+
import com.apurebase.kgraphql.*
54
import com.apurebase.kgraphql.integration.BaseSchemaTest
5+
import org.amshove.kluent.shouldEqual
66
import org.hamcrest.CoreMatchers.notNullValue
77
import org.hamcrest.CoreMatchers.nullValue
88
import org.hamcrest.MatcherAssert.assertThat
@@ -38,6 +38,21 @@ class DirectivesSpecificationTest : BaseSchemaTest() {
3838
assertThat(extractOrNull(mapNeither, "data/film/year"), notNullValue())
3939
}
4040

41+
@Test
42+
fun `query with @include and @skip directive on field object`() {
43+
val mapWithSkip = execute("{ number(big: true), film @skip(if: true) { title } }")
44+
mapWithSkip.extract<String?>("data/film") shouldEqual null
45+
46+
val mapWithoutSkip = execute("{ number(big: true), film @skip(if: false) { title } }")
47+
mapWithoutSkip.extract<String>("data/film/title") shouldEqual "Prestige"
48+
49+
val mapWithInclude = execute("{ number(big: true), film @include(if: true) { title } }")
50+
mapWithInclude.extract<String?>("data/film/title") shouldEqual "Prestige"
51+
52+
val mapWithoutInclude = execute("{ number(big: true), film @include(if: false) { title } }")
53+
mapWithoutInclude.extract<String>("data/film") shouldEqual null
54+
}
55+
4156
@Test
4257
fun `query with @include directive on field with variable`(){
4358
val map = execute(

0 commit comments

Comments
 (0)