execute

suspend fun execute(): ApolloResponse<D>

Retrieves a single ApolloResponse from this ApolloCall.

execute calls toFlow and filters out cache or network errors to return a single success ApolloResponse.

If more than one success ApolloResponse is emitted, for an example, if operation is a subscription or a @defer query, the first one is returned. Prefer toFlow for those cases to handle all the responses.

execute may fail due to an I/O error, a cache miss or other reasons. In that case, check ApolloResponse.exception:

val response = apolloClient.execute(ProductQuery())
if (response.data != null) {
// Handle (potentially partial) data
} else {
// Something wrong happened
if (it.exception != null) {
// Handle fetch errors
} else {
// Handle GraphQL errors in response.errors
}
}

The work is executed on the dispatcher configured in ApolloClient.Builder.dispatcher or a default dispatcher else. There is no need to change the coroutine context before calling execute. See ApolloClient.Builder.dispatcher for more details.

See also

Throws

if the call returns zero or multiple valid GraphQL responses.