How do we test Ktor server schemas?
charlee-dev opened this issue · comments
Hi. How do we approach testing the Ktor server with Expedia GraphQL 7+?
In the lower version without GraphQl plugin I had access to the GraphQL, which I was using to get GraphQLRequestHandler and then I was able to send requests.
This is how my test wrapper looked like:
// from test
fun test(
users: List<User> = mockUsers,
query: String,
variables: Map<String, Any?>? = emptyMap(),
graphQLContext: Map<*, Any> = mapOf("id" to user1.id),
assert: suspend (GraphQLResponse<*>) -> Unit,
) {
val userRepository: UserRepository by inject(UserRepository::class.java)
val graphQLRequestHandler = GraphQLRequestHandler(getGraphQLObject()) // getGraphQLObject() cannot be build now
testApplication {
users.forEach { userRepository.create(it) }
val request = GraphQLRequest(query, null, variables, null)
val response = graphQLRequestHandler.executeRequest(
request,
graphQLContext = graphQLContext
) as GraphQLResponse<*>
assert(response)
}
}
// from main
fun getGraphQLObject(): GraphQL = GraphQL.newGraphQL(schema) // Not valid anymore
.valueUnboxer(IDValueUnboxer())
.build()
The test itself looked like this:
private const val QUERY =
"mutation Login(\$authInput: AuthInput!) { register(authInput: \$authInput) { token userMinimal { id displayName imageUrl }}}"
@Test
fun `Sign up new user should return data with AuthResponse`() = test(
users = emptyList(),
query = QUERY,
variables = mapOf(
"authInput" to mapOf(
"email" to user1.email,
"password" to password1
)
),
assert = { response ->
response.data.toString() shouldContain "{signUp={token=" shouldContain "userMinimal={id=" shouldContain ", displayName= , imageUrl=}}}"
assertNull(response.errors)
}
)
The issue is that currently I have no access to the GraphQL
object.