Here's your chance to speak at GraphQL Summit in New York City, October 8 - 10, 2024! 🏙️ Submit your proposal by May 31.
Launch GraphOS Studio
You're viewing documentation for an upcoming version of this software. Switch to the latest stable version.

Using with Java

This article describes how to use in Java projects.

Use the Java codegen

Apollo Kotlin generates Kotlin code by default, but you can configure it to use the Java codegen instead:

apollo {
service("service") {

The Java runtime

The default runtime for Apollo Kotlin, apollo-runtime, exposes a coroutines / Flow-based API that isn't well suited to be consumed from Java. That is why a specific runtime, apollo-runtime-java is available to use Apollo Kotlin from Java. To use it, add a dependency on this runtime instead of the default one:

dependencies {
// ...
// Use apollo-runtime-java instead of apollo-runtime

The Java runtime has a callbacks based API. This snippet demonstrates initializing and using an ApolloClient in Java:

// (...)
ApolloClient client = ApolloClient.Builder builder = new ApolloClient.Builder()
// Use enqueue to execute a query asynchronously
apolloClient.query(new GetRandomQuery()).enqueue(response -> {
if ( != null) {
// No errors
} else {
// Errors
if (response.exception instanceof ApolloGraphQLException) {
// GraphQL errors
System.out.println(((ApolloGraphQLException) response.exception).getErrors().get(0));
} else {
// Network error

Note that as of now, the Java runtime doesn't support the Http or Normalized caches.

Cancelling requests

euqueue returns an ApolloDisposable that can be used to cancel the request:

ApolloDisposable disposable = apolloClient.subscription(new MyQuery()).enqueue(response -> ...)
// ...

RxJava extensions

If your project uses RxJava, you can use Apollo's RxJava extensions with the Java runtime.

Using aliases
Kotlin native
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy