GraphQL Summit is back for three days of insights, hands-on learning, and fun to celebrate the GraphQL community. Join us in San Diego Oct 3-5.
Docs
Try Apollo Studio

Using the models without apollo-runtime


apollo-runtime and ApolloClient provides support for doing the network requests and interacting with the cache but you can use the generated models and parsers without the runtime and use your network layer of choice for the HTTP calls.

For this, remove the com.apollographql.apollo3:apollo-runtime dependency and replace it with:

build.gradle
implementation("com.apollographql.apollo3:apollo-api:3.6.0")

Composing an HTTP request body

To compose an HTTP POST request body to be sent to your server, use composeJsonRequest:

/**
* jsonRequest contains data to be sent to the server
* {
* "query": ...
* "variables": ...
* "extensions": ...
* }
*/
val body = buildJsonString {
query.composeJsonRequest(this, customScalarAdapters)
}
/**
* Send it to your backend
*/
val httpResponse = sendHttpRequest(
"POST",
"https://com.example/graphql",
"application/json",
body
)

Parsing an HTTP response body

To parse a network response into the type safe models, use parseJsonResponse:

/**
* jsonResponse should contain a json with data and possibly errors:
* {
* "data": ...
* "errors": ...
* "extensions": ...
* }
**/
val jsonReader = httpResponse.body.source().buffer().jsonReader()
val response = operation.parseJsonResponse(jsonReader)
println(response.data)

Composing an HTTP response body

For your integration tests, you can also compose a complete response from a programmatically built data. For this, use composeJsonResponse:

/**
* Build a fake data object
**/
val data = SomeQuery.Data(...)
/**
* jsonResponse contains a json with data and possibly errors:
* {
* "data": ...
* "errors": ...
* "extensions": ...
* }
**/
val jsonResponse = buildJsonString {
operation.composeJsonResponse(this, data)
}
mockServer.enqueue(jsonResponse)
Edit on GitHub
Previous
Custom HTTP clients
Next
Authentication