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

Switch to the latest stable version.

Using with Java


This article describes how to use Apollo Kotlin 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:

Kotlin
build.gradle[.kts]
1apollo {
2  service("service") {
3    generateKotlinModels.set(false)
4  }
5}

Build the client

This snippet demonstrates initializing an ApolloClient instance in Java:

Java
1import com.apollographql.apollo3.cache.normalized.NormalizedCache;
2import com.apollographql.apollo3.cache.http.HttpCache;
3// (...)
4
5ApolloClient.Builder builder = new ApolloClient.Builder()
6  .serverUrl("http://localhost:4000/graphql")
7
8// Optionally, set an http cache
9HttpCache.configureApolloClientBuilder(builder, cacheDirectory, cacheMaxSize);
10
11// Optionally, set a normalized cache
12NormalizedCache.configureApolloClientBuilder(
13  builder,
14  new MemoryCacheFactory(10 * 1024 * 1024, -1),
15  TypePolicyCacheKeyGenerator.INSTANCE,
16  FieldPolicyCacheResolver.INSTANCE,
17  false
18);
19
20ApolloClient client = builder.build();

Use RxJava extensions

Apollo Kotlin has a coroutines / Flow-based API that isn't well suited to using with Java. To achieve a similar effect, you can use Apollo's RxJava extensions.

Feedback

Edit on GitHub

Forums