Skip to content

Commit 974391c

Browse files
authored
Java runtime doc (#5119)
1 parent be4b78a commit 974391c

File tree

2 files changed

+72
-25
lines changed

2 files changed

+72
-25
lines changed

docs/source/advanced/java.mdx

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,61 @@ apollo {
1616
}
1717
```
1818

19-
## Build the client
19+
## The Java runtime
2020

21-
This snippet demonstrates initializing an `ApolloClient` instance in Java:
21+
The default runtime for Apollo Kotlin, `apollo-runtime`, exposes a coroutines / Flow-based API that isn't well suited to be consumed from Java.
22+
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:
23+
24+
```kotlin title="build.gradle[.kts]"
25+
dependencies {
26+
// ...
27+
28+
// Use apollo-runtime-java instead of apollo-runtime
29+
implementation("com.apollographql.apollo3:apollo-runtime-java:4.0.0-alpha.2")
30+
}
31+
```
32+
33+
The Java runtime has a callbacks based API. This snippet demonstrates initializing and using an `ApolloClient` in Java:
2234

2335
```java
24-
import com.apollographql.apollo3.cache.normalized.NormalizedCache;
25-
import com.apollographql.apollo3.cache.http.HttpCache;
36+
import com.apollographql.apollo3.runtime.java.ApolloClient;
2637
// (...)
2738

28-
ApolloClient.Builder builder = new ApolloClient.Builder()
39+
ApolloClient client = ApolloClient.Builder builder = new ApolloClient.Builder()
2940
.serverUrl("http://localhost:4000/graphql")
41+
.build();
3042

31-
// Optionally, set an http cache
32-
HttpCache.configureApolloClientBuilder(builder, cacheDirectory, cacheMaxSize);
43+
// Use enqueue to execute a query asynchronously
44+
apolloClient.query(new GetRandomQuery()).enqueue(response -> {
45+
if (response.data != null) {
46+
// No errors
47+
System.out.println(response.data);
48+
} else {
49+
// Errors
50+
if (response.exception instanceof ApolloGraphQLException) {
51+
// GraphQL errors
52+
System.out.println(((ApolloGraphQLException) response.exception).getErrors().get(0));
53+
} else {
54+
// Network error
55+
response.exception.printStackTrace();
56+
}
57+
}
58+
});
59+
```
60+
61+
Note that as of now, the Java runtime doesn't support the Http or Normalized caches.
3362

34-
// Optionally, set a normalized cache
35-
NormalizedCache.configureApolloClientBuilder(
36-
builder,
37-
new MemoryCacheFactory(10 * 1024 * 1024, -1),
38-
TypePolicyCacheKeyGenerator.INSTANCE,
39-
FieldPolicyCacheResolver.INSTANCE,
40-
false
41-
);
63+
### Cancelling requests
64+
65+
`euqueue` returns an `ApolloDisposable` that can be used to cancel the request:
66+
67+
```java
68+
ApolloDisposable disposable = apolloClient.subscription(new MyQuery()).enqueue(response -> ...)
69+
// ...
70+
disposable.dispose();
4271

43-
ApolloClient client = builder.build();
4472
```
4573

46-
## Use RxJava extensions
74+
### RxJava extensions
4775

48-
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](./rxjava/).
76+
If your project uses RxJava, you can use Apollo's [RxJava extensions](./rxjava/) with the Java runtime.

docs/source/advanced/rxjava.mdx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,23 @@
22
title: RxJava support
33
---
44

5-
If you're using Apollo Kotlin in a [Java project](java) or a Kotlin project that uses RxJava, you can use Apollo's RxJava extensions.
5+
If you're using Apollo Kotlin in a [Java project](java) or Kotlin project that uses RxJava, you can use Apollo's RxJava extensions.
66

7-
To do so, add the `apollo-rx2-support` or `apollo-rx3-support` dependency to your project:
7+
To do so, add the `apollo-rx2-support-java` / `apollo-rx3-support-java` (Java) or `apollo-rx2-support` / `apollo-rx3-support` (Kotlin) dependency to your project:
8+
9+
<MultiCodeBlock>
10+
11+
```java title="build.gradle[.kts]"
12+
dependencies {
13+
// ...
14+
15+
// For RxJava 2
16+
implementation("com.apollographql.apollo3:apollo-rx2-support-java:4.0.0-alpha.2")
17+
18+
// For RxJava 3
19+
implementation("com.apollographql.apollo3:apollo-rx3-support-java:4.0.0-alpha.2")
20+
}
21+
```
822

923
```kotlin title="build.gradle[.kts]"
1024
dependencies {
@@ -18,30 +32,35 @@ dependencies {
1832
}
1933
```
2034

35+
</MultiCodeBlock>
36+
37+
2138
## Executing operations
2239

23-
Use the `rxSingle` / `rxFlowable` extensions to execute GraphQL operations and get RxJava observables:
40+
In Java, Use the `Rx2Apollo` or `Rx3Apollo` classes to execute GraphQL operations and get RxJava observables.
41+
42+
In Kotlin, use the `rxSingle()` / `rxFlowable()` extensions.
2443

2544
<MultiCodeBlock>
2645

2746
```java
28-
import com.apollographql.apollo3.rx2.Rx2Apollo;
47+
import com.apollographql.apollo3.rx3.java.Rx3Apollo;
2948

3049
// (...)
3150

3251
// Query
3352
ApolloCall<MyQuery.Data> queryCall = client.query(new MyQuery());
34-
Single<ApolloResponse<MyQuery.Data>> queryResponse = Rx2Apollo.single(queryCall);
53+
Single<ApolloResponse<MyQuery.Data>> queryResponse = Rx3Apollo.single(queryCall);
3554
queryResponse.subscribe( /* ... */ );
3655

3756
// Mutation
3857
ApolloCall<MyMutation.Data> mutationCall = client.mutation(new MyMutation("my-parameter"));
39-
Single<ApolloResponse<MyMutation.Data>> mutationResponse = Rx2Apollo.single(mutationCall);
58+
Single<ApolloResponse<MyMutation.Data>> mutationResponse = Rx3Apollo.single(mutationCall);
4059
mutationResponse.subscribe( /* ... */ );
4160

4261
// Subscription
4362
ApolloCall<MySubscription.Data> subscriptionCall = client.subscription(new MySubscription());
44-
Flowable<ApolloResponse<MySubscription.Data>> subscriptionResponse = Rx2Apollo.flowable(subscriptionCall);
63+
Flowable<ApolloResponse<MySubscription.Data>> subscriptionResponse = Rx3Apollo.flowable(subscriptionCall);
4564
subscriptionResponse.subscribe( /* ... */ );
4665
```
4766

0 commit comments

Comments
 (0)