1

How can I use graphq subscriptions in Apollo Android client. Now I have code on my server:

type Subscription {    
     targetLocationUpdated(id: String!): Target
}

And code of my resolver:

Subscription: {
    locationAdded: {
        subscribe: () => pubsub.asyncIterator(LOCATION_ADDED),
    },
    targetLocationUpdated: {
        subscribe: withFilter(
            () => pubsub.asyncIterator('TARGET_UPDATED'),
            (payload, variables) => {
                console.log(payload.targetLocationUpdated.id === variables.id);
                return payload.targetLocationUpdated.id === variables.id;
            }
        )
    }
}

My Android client have method for rest requests to my graphql server endpoint:

  public static ApolloClient getMyApolloClient() {


    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(loggingInterceptor)
            .build();
    myApolloClient = ApolloClient.builder()
            .serverUrl(BASE_URL)
            .okHttpClient(okHttpClient)
            .build();
    return myApolloClient;
}
}

But I don't know how to use subscription on android client. In official appolo documentation I'm not found examples using subscription in android client. Help me please resolwe this question.

Andriy Yushko
  • 395
  • 5
  • 21

1 Answers1

0

I referred this link, It may help you or others.

GitHuntEntryDetailActivity.java

ApolloSubscriptionCall<RepoCommentAddedSubscription.Data> subscriptionCall = application.apolloClient()
    .subscribe(new RepoCommentAddedSubscription(repoFullName));

disposables.add(Rx2Apollo.from(subscriptionCall)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeWith(
        new DisposableSubscriber<Response<RepoCommentAddedSubscription.Data>>() {
          @Override public void onNext(Response<RepoCommentAddedSubscription.Data> response) {
            commentsListViewAdapter.addItem(response.data().commentAdded().content());
            Toast.makeText(GitHuntEntryDetailActivity.this, "Subscription response received", Toast.LENGTH_SHORT)
                .show();
          }

          @Override public void onError(Throwable e) {
            Log.e(TAG, e.getMessage(), e);
            Toast.makeText(GitHuntEntryDetailActivity.this, "Subscription failure", Toast.LENGTH_SHORT).show();
          }

          @Override public void onComplete() {
            Log.d(TAG, "Subscription exhausted");
            Toast.makeText(GitHuntEntryDetailActivity.this, "Subscription complete", Toast.LENGTH_SHORT).show();
          }
        }
    )
);
AL.
  • 36,815
  • 10
  • 142
  • 281
Siva Sonai
  • 107
  • 12