You are viewing documentation for a previous version of this software.

Switch to the latest stable version.

Using the client 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 queries without the runtime if you want.

For this, remove the com.apollographql.apollo:apollo-runtimedependency and replace it with:

Kotlin
build.gradle
1implementation("com.apollographql.apollo:apollo-api:x.y.z")

Composing HTTP request body

To compose HTTP POST request body Operation provides such API:

Kotlin
Java
1val query = ...
2val payload = query.composeRequestBody()
3val  mediaType = MediaType.parse("application/json; charset=utf-8");
4val requestBody = RequestBody.create(mediaType, payload);

If GraphQL operation defines any variable with custom scalar type, you must provide properly configured instance of com.apollographql.apollo.response.ScalarTypeAdapters:

Java
1ScalarTypeAdapters scalarTypeAdapters = new ScalarTypeAdapters(<provide your custom scalar type adapters>);
2Query query = ...
3ByteString payload = query.composeRequestBody(scalarTypeAdapters);
4okhttp3.MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
5okhttp3.RequestBody requestBody = RequestBody.create(mediaType, payload);

In case when GraphQL server supports auto persistence query:

Java
1Query query = ...
2boolean autoPersistQueries = ... // encode extensions attributes required by query auto persistence or not
3withQueryDocument = ... // encode query document or not
4ScalarTypeAdapters scalarTypeAdapters = ...
5ByteString payload = query.composeRequestBody(autoPersistQueries, withQueryDocument, scalarTypeAdapters);
6okhttp3.MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
7okhttp3.RequestBody requestBody = RequestBody.create(mediaType, payload);

Parsing HTTP response body

All Operation instances provide an API to parse Response from raw okio.BufferedSource source that represents http response body returned by the GraphQL server.

If for some reason you want to use your own network layer and don't want to use fully featured ApolloClient provided by apollo-runtime you can use this API:

Java
1okhttp3.Response httpResponse = ...;
2
3Response<Operation.Data> response = new Query().parse(httpResponse.body().source());

If you do have custom GraphQL scalar types, pass properly configured instance of com.apollographql.apollo.response.ScalarTypeAdapters:

Java
1okhttp3.Response httpResponse = ...;
2
3ScalarTypeAdapters scalarTypeAdapters = new ScalarTypeAdapters(<provide your custom scalar type adapters>);
4
5Response<Operation.Data> response = new Query().parse(httpResponse.body().source(), scalarTypeAdapters);

With Kotlin Multiplatform support, you can use Swift's NSData type to parse the response

Kotlin
Kotlin-Common
1val data: NSData = ...;
2
3val response = query.parse(data.toByteString())

Converting Query.Data back to JSON

In case you have an instance of Operation.Data and want to convert it back to JSON representation, you can use OperationDataJsonSerializer.serialize static method.

Java
1Operation.Data data = ...;
2
3String json = OperationDataJsonSerializer.serialize(data, "  ");

Just like above, you can provide instance of custom ScalarTypeAdapters as last argument.

Simpler extension function is available for Kotlin users:

Kotlin
1val json = data.toJson()
2
3// or
4val json = data.toJson(indent = "  ")

Creating request payload for POST request

To compose a GraphQL POST request along with operation variables to be sent to the server, you can use Operation.Variables#marshal() API:

Java
1// Generated GraphQL query, mutation, subscription
2Query query = ...;
3
4String requestPayload = "{" +
5    "\"operationName\": " + query.name().name() + ", " +
6    "\"query\": " + query.queryDocument() + ", " +
7    "\"variables\": " + query.variables().marshal() +
8    "}";

The same to serialize variables with the custom GraphQL scalar type adapters:

Java
1// Generated GraphQL query, mutation, subscription
2Query query = ...;
3
4ScalarTypeAdapters scalarTypeAdapters = new ScalarTypeAdapters(<provide your custom scalar type adapters>);
5
6String requestPayload = "{" +
7    "\"operationName\": " + query.name().name() + ", " +
8    "\"query\": " + query.queryDocument() + ", " +
9    "\"variables\": " + query.variables().marshal(scalarTypeAdapters) +
10    "}";
Feedback

Edit on GitHub

Forums