Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free

Mocking GraphQL responses


The test network transports are currently experimental in Apollo Kotlin. If you have feedback on them, please let us know via GitHub issues or in the Kotlin Slack community.

QueueTestNetworkTransport is a high-level test API that lets you specify the responses that are returned by your ApolloClient instance.

See also MockServer, which creates a full HTTP server and helps test specific server behaviors, such as error cases, HTTP headers, and timeouts.

Add the dependency to your project's build.gradle file:

build.gradle[.kts]
dependencies {
testImplementation("com.apollographql.apollo3:apollo-testing-support:4.0.0-beta.7")
}

Enable the QueueTestNetworkTransport by passing it to the ApolloClient builder:

val apolloClient = ApolloClient.Builder()
.networkTransport(QueueTestNetworkTransport())
.build()

You can then use the enqueueTestResponse extension function to specify the GraphQL responses to return:

val testQuery = GetHeroQuery("001")
val testData = GetHeroQuery.Data {
hero = droidHero {
name = "R2D2"
}
}
apolloClient.enqueueTestResponse(testQuery, testData)
val actual = apolloClient.query(testQuery).execute().data!!
assertEquals(testData.hero.name, actual.hero.name)

You can pass an ApolloResponse to the enqueueTestResponse function, or as a shortcut, you can pass a Data directly as shown above. If you do pass a Data, you also need to pass an , because an ApolloResponse is built under the hood, which needs a reference to it.

To help create your mocked response data, you can use data builders.

Advanced usage

QueueTestNetworkTransport returns responses in the order they've been enqueued.

In certain tests, it might be more convenient to map responses to .

To achieve this, use MapTestNetworkTransport instead and call the registerTestResponse extension function:

val apolloClient = ApolloClient.Builder().networkTransport(MapTestNetworkTransport()).build()
apolloClient.registerTestResponse(query1, testData1)
apolloClient.registerTestResponse(query2, testData2)
val actual1 = apolloClient.query(query1).execute().data
val actual2 = apolloClient.query(query2).execute().data
// Execute query1 again, which is mapped to testData1
val actual3 = apolloClient.query(query1).execute().data
assertEquals(testData1, actual1)
assertEquals(testData2, actual2)
assertEquals(testData1, actual3)

If you need more control over the responses to return, you can implement your own NetworkTransport and pass it to the ApolloClient builder:

private class CustomTestNetworkTransport : NetworkTransport {
override fun <D : Operation.Data> execute(request: ApolloRequest<D>): Flow<ApolloResponse<D>> {
return flowOf(
ApolloResponse.Builder(
/* Your custom logic here */
).build() as ApolloResponse<D>
)
}
override fun dispose() {}
}
apolloClient = ApolloClient.Builder()
.networkTransport(CustomTestNetworkTransport())
.build()

Note that if you use a custom NetworkTransport, calling enqueueTestResponse() or registerTestResponse() is no longer possible because they expect the transport to be QueueTestNetworkTransport or MapTestNetworkTransport.

Previous
Mocking HTTP responses
Next
Data builders
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company