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

Using the models without apollo-runtime


apollo-runtime and ApolloClient provide 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:4.0.0-beta.7")

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)
Previous
Custom HTTP clients
Next
Authentication
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company