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-runtime
dependency and replace it with:
1implementation("com.apollographql.apollo:apollo-api:x.y.z")
Composing HTTP request body
To compose HTTP POST request body Operation
provides such API:
1val query = ...
2val payload = query.composeRequestBody()
3val mediaType = MediaType.parse("application/json; charset=utf-8");
4val requestBody = RequestBody.create(mediaType, payload);
1Query query = ...
2ByteString payload = query.composeRequestBody();
3okhttp3.MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
4okhttp3.RequestBody 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
:
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:
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:
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
:
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
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.
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:
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:
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:
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 "}";