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 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:

build.gradle[.kts]
apollo {
service("service") {
generateKotlinModels.set(false)
}
}

Build the client

This snippet demonstrates initializing an ApolloClient instance in Java:

import com.apollographql.apollo3.cache.normalized.NormalizedCache;
import com.apollographql.apollo3.cache.http.HttpCache;
// (...)
ApolloClient.Builder builder = new ApolloClient.Builder()
.serverUrl("http://localhost:4000/graphql")
// Optionally, set an http cache
HttpCache.configureApolloClientBuilder(builder, cacheDirectory, cacheMaxSize);
// Optionally, set a normalized cache
NormalizedCache.configureApolloClientBuilder(
builder,
new MemoryCacheFactory(10 * 1024 * 1024, -1),
TypePolicyCacheKeyGenerator.INSTANCE,
FieldPolicyCacheResolver.INSTANCE,
false
);
ApolloClient 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.

Previous
Using aliases
Next
RxJava support
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company