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

add defer types to test fixture #520

Merged
merged 5 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 25 additions & 10 deletions test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ private suspend fun execute(
)
printSyncLine(actualQuery)

fun failWithFixtureContext(message: String): Nothing {
Copy link
Collaborator Author

@sbarker2 sbarker2 Mar 11, 2024

Choose a reason for hiding this comment

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

moved this into function because the fixture context is now useful for multiple fail messages

fail(
"""${message}
| fixture : '${fixture.name}'
| service : '${serviceName}'
| query : '${actualQuery}'
| variables : '${actualVariables}'
| operation : '${actualOperationName}'
""".trimMargin()
)
}

val response = synchronized(serviceCalls) {
val indexOfCall = serviceCalls
.indexOfFirst {
Expand All @@ -180,17 +192,20 @@ private suspend fun execute(

if (indexOfCall != null) {
val serviceCall = serviceCalls.removeAt(indexOfCall)
serviceCall.response
val response = serviceCall.response
Copy link
Collaborator Author

@sbarker2 sbarker2 Mar 11, 2024

Choose a reason for hiding this comment

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

assigning serviceCall.response to a variable, because kotlin thinks the state can change due to the way it was defined:

val response: JsonMap? by lazy {
...
}

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 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")
}
}

Expand Down
29 changes: 26 additions & 3 deletions test/src/test/kotlin/graphql/nadel/tests/TestFixture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,6 +30,8 @@ data class TestFixture(
val serviceCalls: List<ServiceCall>,
@JsonProperty("response")
val responseJsonString: String?,
@JsonProperty("incrementalResponse")
val incrementalResponseJsonString: IncrementalResponse?,
@JsonInclude(NON_NULL)
val exception: ExpectedException?,
) {
Expand All @@ -42,11 +45,14 @@ 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(
Expand Down Expand Up @@ -88,3 +94,20 @@ data class ExpectedException(
)
}
}

data class IncrementalResponse(
@JsonProperty("initialResponse")
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm happy to merge this as it is. But as we discussed, it's worth trying to use the actual graphql-java types here to make deserialization easier.

val initialResponseJsonString: String,
@JsonProperty("delayedResponses")
val delayedResponsesJsonString: String,
) {
@get:JsonIgnore
val initialResponse: JsonMap by lazy {
jsonObjectMapper.readValue(initialResponseJsonString)
}

@get:JsonIgnore
val delayedResponses: List<JsonMap> by lazy {
jsonObjectMapper.readValue(delayedResponsesJsonString, object : TypeReference<List<JsonMap>>() {})
}
}
65 changes: 44 additions & 21 deletions test/src/test/resources/fixtures/defer/defer-with-label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,48 @@ serviceCalls:
}
variables: { }
# language=JSON
response: |-
{
"data": {
"defer": {
"hello": "world"
}
},
"extensions": {}
}
# language=JSON
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"

}
}]
}]

# 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": {}
}
# language=JSON
incrementalResponse:
initialResponse: |-
{
"hasNext": true,
"data": {
"defer": {
"hello": "world"
}
},
"extensions": {}
}
delayedResponses: |-
[{
"hasNext": false,
"incremental": [{
"data": {
"slow": "snail"

}
}]
}]
Loading