awslabs / aws-mobile-appsync-sdk-android

Android SDK for AWS AppSync.
https://docs.amplify.aws/sdk/api/graphql/q/platform/android/
Apache License 2.0
105 stars 58 forks source link

RXJava support #74

Open leijdekkers opened 5 years ago

leijdekkers commented 5 years ago

Hi,

Does appsync sdk support RxJava like Apollo? I experimented adapting the Rx2Apollo.java class to AppSync and I got the queries part working but not mutation.

So does AppSync properly support RxJava?

Thanks

scb01 commented 5 years ago

@leijdekkers We currently do not have support for Rx Java within AppSync. I am adding @undefobj to this thread to comment on whether this would be a future roadmap item.

leijdekkers commented 5 years ago

Hope you will implement it soon. The reason is that in many Android apps, the AppSync part goes into the "Repository" part (i.e. Model in MVVM architecture) of the app where many developers use RxJava to communicate between the Repository component and the ViewModel. It is really bad practice to use the AppSync SDK in fragments/activities (i.e. the View part of the MVVM model).

Please have a look at Apollo where they already support RxJava. It should not be too hard to fully support it in AppSync SDK.

ghost commented 5 years ago

@leijdekkers I'm using Rx with AppSync. Please take a look my sample code below.

/**
     * getUser
     */
override fun getUser(userId: String): Single<GetUserQuery.GetUser> {
        Timber.d("GetUserQuery Request: %s", userId)
        return Single.create { emitter ->
            appSyncClient.query(GetUserQuery.builder().user_id(userId).build())
                .responseFetcher(AppSyncResponseFetchers.NETWORK_ONLY)
                .enqueue(getUserCallback(emitter))
        }
    }

private fun getUserCallback(emitter: SingleEmitter<GetUserQuery.GetUser>): GraphQLCall.Callback<GetUserQuery.Data> {
        return object : GraphQLCall.Callback<GetUserQuery.Data>() {
            override fun onFailure(e: ApolloException) {
                Timber.e(e)
                emitter.onError(e)
            }

            override fun onResponse(response: Response<GetUserQuery.Data>) {
                val result = response.data()?.user ?: run {
                    val e = Throwable("GetUserQuery Response is null")
                    Timber.e(e)
                    emitter.onError(e)
                    return
                }
                Timber.d("GetUserQuery Response: %s", result)
                emitter.onSuccess(result)
            }
        }
    }
i-haz-teh-codez commented 5 years ago

Any progress almost a year later? The approach suggested by @norio-agoop is a starting point but it's an approach that doesn't scale that well with an increase in the number of queries — just too much onFailure() and onResponse() boilerplate.

Right now, I have a REST API for my Android app, and using Retrofit to make the HTTP calls. Now, Retrofit has a simple class, RxJava2CallAdapterFactory.java (~100 SLOC) which has a single dependency, CallAdapter.java (~50 SLOC). Those classes are responsible for converting the HTTP calls to observables. Without RxJava2CallAdapterFactory, the code looks like this (for brevity):

query.enqueue(new Callback<List<GitHubRepo>>() {  
    @Override
    public void onResponse(Call<List<GitHubRepo>> call, Response<List<GitHubRepo>> response) {}

    @Override
    public void onFailure(Call<List<GitHubRepo>> call, Throwable t) { }
});

But with RxJava2CallAdapterFactory included, it becomes:

query()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(success -> {}, failure -> {})

And that's a massive improvement! Is there something similar in the works for your SDK?