Skip to content

Instantly share code, notes, and snippets.

@BoD
Last active October 25, 2022 14:54
Show Gist options
  • Save BoD/e2d36076a0befbb93c3d08abafc63612 to your computer and use it in GitHub Desktop.
Save BoD/e2d36076a0befbb93c3d08abafc63612 to your computer and use it in GitHub Desktop.
Java runtime vs without

1. Simple query

Kotlin

// Async code looks like sync code

val result = try {
  val response = apolloClient.query(query).execute()
  println(response.data)
} catch (e: Exception) {
  e.printStackTrace()
}

Java, currently

// Rx adpaters are currently the only way to use the client from Java
// If you don't use/like Rx you're out of luck

Rx2Apollo.single(apolloClient.query(query)).subscribe(
  response -> {
    System.out.println(response.data);
  },
  e -> {
    e.printStackTrace();
  }
);

Java, new runtime

// We now have a callbacks based API

apolloClient.query(query).enqueue(new ApolloCallback<GetRandomQuery.Data>() {
  @Override public void onResponse(@NotNull ApolloResponse<GetRandomQuery.Data> response) {
    System.out.println(response.data);
  }

  @Override public void onFailure(@NotNull ApolloException e) {
    e.printStackTrace();
  }
});

// If you use/like Rx you can also still use the adapters like above

2. Normalized Cache

Kotlin

// Use of extensions

val cacheFactory = MemoryCacheFactory(cacheSize)
val apolloClient = ApolloClient.Builder()
  .serverUrl("http://...")
  .normalizedCache(cacheFactory)
  .build()

Java, currently

// Use of a helper class

NormalizedCacheFactory cacheFactory = new MemoryCacheFactory(cacheSize);
ApolloClient.Builder apolloClientBuilder = new ApolloClient.Builder().serverUrl("http://...");
NormalizedCache.configureApolloClientBuilder(apolloClientBuilder, cacheFactory);
ApolloClient apolloClient = apolloClientBuilder.build();

Java, new runtime

// A method directly on the builder

NormalizedCacheFactory cacheFactory = new MemoryCacheFactory(cacheSize);
ApolloClient apolloClient = new ApolloClient.Builder()
  .serverUrl("http://...")
  .normalizedCache(cacheFactory)
  .build();







More use cases

3. Http Cache

Kotlin

// Use of extensions

val apolloClient = ApolloClient.Builder()
  .serverUrl("http://...)
  .httpCache(dir, cacheSize)
  .build()

Java, currently

// Use of a helper class

ApolloClient.Builder apolloClientBuilder = new ApolloClient.Builder().serverUrl("http://...");
HttpCache.configureApolloClientBuilder(apolloClientBuilder, dir, cacheSize);
ApolloClient apolloClient = apolloClientBuilder.build();

Java, new runtime

// A method directly on the builder

ApolloClient apolloClient = new ApolloClient.Builder()
  .serverUrl("http://...")
  .httpCache(dir, cacheSize)
  .build();

4. Subscriptions

Kotlin

// Use of Flows

apolloClient.subscription(MySubscription())
  .toFlow()
  .collect {
    println(it.data)
  }

Java, currently

// Must use the Rx adapters

ApolloCall<MySubscription.Data> subscriptionCall = client.subscription(new MySubscription());
Flowable<ApolloResponse<MySubscription.Data>> subscriptionFlowable = Rx2Apollo.flowable(subscriptionCall);
subscriptionFlowable.subscribe(
  response -> {
    System.out.println(response.data);
  },
  e -> {
    e.printStackTrace();
  }
);

Java, new runtime

// Callbacks based
// You also can still use Rx if your project likes it

apolloClient.subscription(new MySubscription()).enqueue(
  new ApolloCallback<MySubscription.Data>() {
    @Override public void onResponse(@NotNull ApolloResponse<MySubscription.Data> response) {
      System.out.println(response.data);
    }
    
    @Override public void onFailure(@NotNull ApolloException e) {
      e.printStackTrace();
    }
  }
);

5. Interceptors

Kotlin

// Use of Flows

val interceptor = object: ApolloInterceptor {
  override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
    return chain.proceed(request.newBuilder().addHttpHeader("header", "value").build())
  }
}
apolloClient = ApolloClient.Builder()
  .serverUrl("http://...")
  .addInterceptor(interceptor)
  .build()

Java, currently

// Manipulating Kotlin Flows from Java works for simple cases but is quite limitating

ApolloInterceptor interceptor = new ApolloInterceptor() {
  @NotNull @Override public <D extends Operation.Data> Flow<ApolloResponse<D>> intercept(@NotNull ApolloRequest<D> request, @NotNull ApolloInterceptorChain chain) {
    return chain.proceed(request.newBuilder().addHttpHeader("header", "value").build());
  }
};
ApolloClient apolloClient = new ApolloClient.Builder()
  .serverUrl("http://...")
  .addInterceptor(interceptor)
  .build();

Java, new runtime

// Callbacks based

ApolloInterceptor interceptor = new ApolloInterceptor() {
  @Override public <D extends Operation.Data> void intercept(@NotNull ApolloRequest<D> request, @NotNull ApolloInterceptorChain chain, @NotNull ApolloCallback<D> callback) {
    chain.proceed(request.newBuilder().addHttpHeader("header", "value").build(), callback);
  }
};
ApolloClient apolloClient = new ApolloClient.Builder()
  .serverUrl("http://...")
  .addInterceptor(interceptor)
  .build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment