From 90db233eb6c0db061dd9d8f1dcca320fb195af6f Mon Sep 17 00:00:00 2001 From: Steven Barker Date: Wed, 6 Mar 2024 12:05:23 +1300 Subject: [PATCH 1/5] add defer types to test fixture --- .../kotlin/graphql/nadel/tests/EngineTests.kt | 7 +- .../kotlin/graphql/nadel/tests/TestFixture.kt | 36 +++++++++- .../fixtures/defer/defer-with-label.yml | 72 ++++++++++++++----- 3 files changed, 93 insertions(+), 22 deletions(-) diff --git a/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt b/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt index 12396c7b1..2d0268d4c 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt @@ -180,7 +180,12 @@ private suspend fun execute( if (indexOfCall != null) { val serviceCall = serviceCalls.removeAt(indexOfCall) - serviceCall.response + if( serviceCall.incrementalResponse != null){ + serviceCall.incrementalResponse.initialResponse //for now, just return initial response + } + else { + serviceCall.response!! + } } else { fail( """Unable to match service call diff --git a/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt b/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt index ef183a65d..db193def0 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt @@ -27,8 +27,14 @@ data class TestFixture( @JsonInclude(NON_NULL) val operationName: String? = null, val serviceCalls: List, + + @JsonProperty("response") val responseJsonString: String?, + + @JsonProperty("incrementalResponse") + val incrementalResponseJsonString: IncrementalResponse?, + @JsonInclude(NON_NULL) val exception: ExpectedException?, ) { @@ -41,12 +47,17 @@ data class TestFixture( data class ServiceCall( val serviceName: String, val request: Request, + @JsonProperty("response") - val responseJsonString: String, + val responseJsonString: String?, + + val incrementalResponse: IncrementalResponse?, ) { @get:JsonIgnore - val response: JsonMap by lazy { - jsonObjectMapper.readValue(responseJsonString) + val response: JsonMap? by lazy { + responseJsonString?.let { + jsonObjectMapper.readValue(it) + } } data class Request( @@ -88,3 +99,22 @@ data class ExpectedException( ) } } +data class IncrementalResponse( + @JsonProperty("initialResponse") + val initialResponseJsonString: String, + @JsonProperty("delayedResponses") + val delayedResponsesJsonString: String?, +) +{ + @get:JsonIgnore + val initialResponse: JsonMap by lazy { + jsonObjectMapper.readValue(initialResponseJsonString) + } + + @get:JsonIgnore + val delayedResponses: JsonMap? by lazy { + delayedResponsesJsonString?.let { + jsonObjectMapper.readValue(it) + } + } +} \ No newline at end of file diff --git a/test/src/test/resources/fixtures/defer/defer-with-label.yml b/test/src/test/resources/fixtures/defer/defer-with-label.yml index d4c9d7399..c4a78a5e5 100644 --- a/test/src/test/resources/fixtures/defer/defer-with-label.yml +++ b/test/src/test/resources/fixtures/defer/defer-with-label.yml @@ -48,25 +48,61 @@ serviceCalls: } variables: { } # language=JSON - response: |- - { - "data": { - "defer": { - "hello": "world" - } - }, - "extensions": {} - } + incrementalResponse: + # Note: currently this just returns initialResponse, but have included delayedResponse to ensure no errors with the TestFixture types + initialResponse: |- + { + "hasNext": true, + "data": { + "defer": { + "hello": "world" + } + }, + "extensions": {} + } + delayedResponses: |- + { + "hasNext": false, + "incremental": [{ + "data": { + "slow": "snail" + + } + }] + } + # language=JSON # This is just checking that can generate queries containing @defer. # The response below will change once defer work is implemented. -response: |- - { - "data": { - "defer": { - "hello": "world" - } - }, - "extensions": {} - } +#response: |- +# { +# "data": { +# "defer": { +# "hello": "world" +# } +# }, +# "extensions": {} +# } + +incrementalResponse: + initialResponse: |- + { + "hasNext": true, + "data": { + "defer": { + "hello": "world" + } + }, + "extensions": {} + } + delayedResponses: |- + { + "hasNext": false, + "incremental": [{ + "data": { + "slow": "snail" + + } + }] + } From 61e400f911c03c1d130aee3375e2fb45557bfc3d Mon Sep 17 00:00:00 2001 From: Steven Barker Date: Wed, 6 Mar 2024 12:34:00 +1300 Subject: [PATCH 2/5] change defer delayedResponses to a list --- .../test/kotlin/graphql/nadel/tests/TestFixture.kt | 9 ++++----- .../resources/fixtures/defer/defer-with-label.yml | 13 +++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt b/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt index db193def0..71a5fffaf 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.annotation.JsonInclude.Include.NON_DEFAULT import com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.module.kotlin.readValue import graphql.language.AstSorter import graphql.language.Document @@ -103,7 +104,7 @@ data class IncrementalResponse( @JsonProperty("initialResponse") val initialResponseJsonString: String, @JsonProperty("delayedResponses") - val delayedResponsesJsonString: String?, + val delayedResponsesJsonString: String, ) { @get:JsonIgnore @@ -112,9 +113,7 @@ data class IncrementalResponse( } @get:JsonIgnore - val delayedResponses: JsonMap? by lazy { - delayedResponsesJsonString?.let { - jsonObjectMapper.readValue(it) - } + val delayedResponses: List by lazy { + jsonObjectMapper.readValue(delayedResponsesJsonString, object : TypeReference>() {}) } } \ No newline at end of file diff --git a/test/src/test/resources/fixtures/defer/defer-with-label.yml b/test/src/test/resources/fixtures/defer/defer-with-label.yml index c4a78a5e5..ab386ef51 100644 --- a/test/src/test/resources/fixtures/defer/defer-with-label.yml +++ b/test/src/test/resources/fixtures/defer/defer-with-label.yml @@ -61,15 +61,16 @@ serviceCalls: "extensions": {} } delayedResponses: |- - { + [{ "hasNext": false, "incremental": [{ "data": { "slow": "snail" - + } }] - } + }] + # language=JSON @@ -97,12 +98,12 @@ incrementalResponse: "extensions": {} } delayedResponses: |- - { + [{ "hasNext": false, "incremental": [{ "data": { "slow": "snail" - + } }] - } + }] From 9f3cdfc2a1cc4e828987aefd5d162d09db9e4e6f Mon Sep 17 00:00:00 2001 From: Steven Barker Date: Wed, 6 Mar 2024 13:48:22 +1300 Subject: [PATCH 3/5] reformat --- .../src/test/kotlin/graphql/nadel/tests/EngineTests.kt | 5 ++--- .../src/test/kotlin/graphql/nadel/tests/TestFixture.kt | 10 ++-------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt b/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt index 2d0268d4c..dd9d4212c 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt @@ -180,10 +180,9 @@ private suspend fun execute( if (indexOfCall != null) { val serviceCall = serviceCalls.removeAt(indexOfCall) - if( serviceCall.incrementalResponse != null){ + if (serviceCall.incrementalResponse != null) { serviceCall.incrementalResponse.initialResponse //for now, just return initial response - } - else { + } else { serviceCall.response!! } } else { diff --git a/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt b/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt index 71a5fffaf..68b5b2085 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt @@ -28,14 +28,10 @@ data class TestFixture( @JsonInclude(NON_NULL) val operationName: String? = null, val serviceCalls: List, - - @JsonProperty("response") val responseJsonString: String?, - @JsonProperty("incrementalResponse") val incrementalResponseJsonString: IncrementalResponse?, - @JsonInclude(NON_NULL) val exception: ExpectedException?, ) { @@ -48,10 +44,8 @@ data class TestFixture( data class ServiceCall( val serviceName: String, val request: Request, - @JsonProperty("response") val responseJsonString: String?, - val incrementalResponse: IncrementalResponse?, ) { @get:JsonIgnore @@ -100,13 +94,13 @@ data class ExpectedException( ) } } + data class IncrementalResponse( @JsonProperty("initialResponse") val initialResponseJsonString: String, @JsonProperty("delayedResponses") val delayedResponsesJsonString: String, -) -{ +) { @get:JsonIgnore val initialResponse: JsonMap by lazy { jsonObjectMapper.readValue(initialResponseJsonString) From fcc3d015a5187f8b834d265017fa14d718f90eab Mon Sep 17 00:00:00 2001 From: Steven Barker Date: Thu, 7 Mar 2024 12:57:40 +1300 Subject: [PATCH 4/5] remove commented code --- .../resources/fixtures/defer/defer-with-label.yml | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/test/src/test/resources/fixtures/defer/defer-with-label.yml b/test/src/test/resources/fixtures/defer/defer-with-label.yml index ab386ef51..2b9273a1c 100644 --- a/test/src/test/resources/fixtures/defer/defer-with-label.yml +++ b/test/src/test/resources/fixtures/defer/defer-with-label.yml @@ -71,21 +71,7 @@ serviceCalls: }] }] - # language=JSON - - # This is just checking that can generate queries containing @defer. - # The response below will change once defer work is implemented. -#response: |- -# { -# "data": { -# "defer": { -# "hello": "world" -# } -# }, -# "extensions": {} -# } - incrementalResponse: initialResponse: |- { From 48cfbc0612673b95211ea6bf47a35d7c0f55d12b Mon Sep 17 00:00:00 2001 From: Steven Barker Date: Mon, 11 Mar 2024 16:05:41 +1100 Subject: [PATCH 5/5] ensure test fixture does not have both an incremental and non-incremental response --- .../kotlin/graphql/nadel/tests/EngineTests.kt | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt b/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt index dd9d4212c..48804048b 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt @@ -168,6 +168,18 @@ private suspend fun execute( ) printSyncLine(actualQuery) + fun failWithFixtureContext(message: String): Nothing { + fail( + """${message} + | fixture : '${fixture.name}' + | service : '${serviceName}' + | query : '${actualQuery}' + | variables : '${actualVariables}' + | operation : '${actualOperationName}' + """.trimMargin() + ) + } + val response = synchronized(serviceCalls) { val indexOfCall = serviceCalls .indexOfFirst { @@ -180,21 +192,20 @@ private suspend fun execute( if (indexOfCall != null) { val serviceCall = serviceCalls.removeAt(indexOfCall) - if (serviceCall.incrementalResponse != null) { + val response = serviceCall.response + if (serviceCall.incrementalResponse != null && response != null) { + failWithFixtureContext("Fixture cannot have both a response and an incrementalResponse.") + } + else if (serviceCall.incrementalResponse != null) { serviceCall.incrementalResponse.initialResponse //for now, just return initial response - } else { - serviceCall.response!! + } else if (response != null) { + response + } + else { + failWithFixtureContext("Fixture had no response") } } else { - fail( - """Unable to match service call - | fixture : '${fixture.name}' - | service : '${serviceName}' - | query : '${actualQuery}' - | variables : '${actualVariables}' - | operation : '${actualOperationName}' - """.trimMargin() - ) + failWithFixtureContext("Unable to match service call") } }