Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApolloClient.rxQuery configure lambda confusing and error prone #2430

Closed
hameno opened this issue Jul 8, 2020 · 8 comments
Closed

ApolloClient.rxQuery configure lambda confusing and error prone #2430

hameno opened this issue Jul 8, 2020 · 8 comments

Comments

@hameno
Copy link

hameno commented Jul 8, 2020

Summary
It is not clear from documentation and API usage, that you need to chain you calls inside the configure lambda. Normally with this kind of API you only configure the builder and it returns the same instance of the builder.
This lead to at least one bug in our code, becuase we were not using the results of the methods (e.g. responseFetcher)

Version
2.2.2

Description
see above

@hameno
Copy link
Author

hameno commented Jul 8, 2020

It looks like internally it uses a toBuilder() and then build()
I think it would be better to provide the builder instance to the lambda

@martinbonnin
Copy link
Contributor

martinbonnin commented Jul 8, 2020

I feel like the consistency of the builder()/configure patterns can definitely be improved. Definitely something to keep in mind for apollo-runtime-kotlin, I'm not sure how much of it can be changed in the existing RX extensions without breaking the API. Maybe with some overloads/deprecation...

To make things explicit, can you give an exemple of code that doesn't work as expected and how you'd expect it to be written instead?

@hameno
Copy link
Author

hameno commented Jul 8, 2020

I had the following (wrong) code:

apolloClient.rxQuery(this) {
    responseFetcher(responseFetcher)
    if (cacheHeaders != null) {
        cacheHeaders(cacheHeaders)
    } else this
}

I think this style would have been easier to understand:

apolloClient.rxQuery(this) { builder ->
    builder.responseFetcher(responseFetcher)
    if (cacheHeaders != null) {
        builder.cacheHeaders(cacheHeaders)
    }
}

or also possible:

apolloClient.rxQuery(this) { // this: Builder
    responseFetcher(responseFetcher)
    if (cacheHeaders != null) {
        cacheHeaders(cacheHeaders)
    }
}

@martinbonnin
Copy link
Contributor

Tentative improvement there: #2434

It won't directly fix the rxQuery usages but will encourage to use toBuilder() to mutate values of ApolloCall

@martinbonnin
Copy link
Contributor

#2434 has been merged, I'm not sure how to follow up and make the rxQuery API much better. We could find a new name and deprecate rxQuery:

@Deprecated("use rx3Query instead")
inline fun <D : Operation.Data, T, V : Operation.Variables> ApolloClient.rxQuery(
    query: Query<D, T, V>,
    configure: ApolloQueryCall<T>.() -> ApolloQueryCall<T> = { this }
)

inline fun <D : Operation.Data, T, V : Operation.Variables> ApolloClient.rx3Query(
    query: Query<D, T, V>,
    configure: ApolloQueryCall.Builder<T>.() -> ApolloQueryCall.Builder<T> = { this }
)

Is there a convention somewhere about when to use receiver vs parameter?

All in all, that would work but I hope we're not forgetting anything because once we've burnt rxQuery and rx3Query, there won't be a ton of other possibilities left 😅

@hameno
Copy link
Author

hameno commented Jul 16, 2020

Not sure 🤔 I don't reall have a good idea right now

@martinbonnin
Copy link
Contributor

Let's wait a bit until we gather more feedback, if rx3Query looks good, we'll set it in API stone.

@martinbonnin
Copy link
Contributor

Closing this one with #2434. Will reopen if there are more reports and we need to add rx3Query

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants