-
Notifications
You must be signed in to change notification settings - Fork 660
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
[RFC] with-ers
vs Builders
vs DSL-like
APIs
#3301
Comments
For me, DSL looks most natural. (probably biggest cons is for library devs who need to implement it). 2nd place is for the good old However |
I have a strong preference for the builder pattern, its immediately recognizable, it has fluid discovery, and seems to be a pattern for the ages :) I really don't like the idea of errors passing through the compiler if they can be avoided (huge point against I do like the I have a strong preference for the |
I can see another small pro of the But overall I still tend to agree with you that builders are probably the best compromise (the familiarity argument is a strong one for me). |
I think we can support use cases like this and Builders with things like: fun ApolloClient.Builder.userDefined(param: Param) {
//
} We'll need to support extensions in all cases for the normalized cache since it is completely independent from |
The APIs have been migrated to Builders in #3404. |
The main entry points to Apollo Android are ApolloClient and ApolloRequest and support multiple configuration options.
The goal of this issue is to investigate different Builder-like APIs and gather community feedback about which one feels the more natural.
1.
with-ers
This is the current solution. An instance is created first and then copied with additional parameters using
withFoo()
methods:pros:
cons:
withFoo()
is an error that is not caught by the compiler. It might be mitigated with an@CheckReturnValue
annotation (TBC)2.
Builders
This is the same as Apollo Android 2.x and uses Java-style builders:
pros:
cons:
Builder
class with mostly the same properties as the original one.build()
call3.
DSL-like
This is a solution used by Ktor, Anko and other Kotlin DSL-like APIs. The Kotlin doc has a page for them called Type-safe builders
pros:
cons:
Builder
class with mostly the same properties as the original one.html
while Ktor uses CamelCaseHttpClient
4.
default arguments
One option is to use the Kotlin default arguments and let users configure only those options that they need but this wasn't even in the original proposal. I'm adding this now for reference but it has a number of limitations:
@JvmOverload
doesn't allow all combinations of arguments)Bonus: ApolloRequest vs ApolloCall
Related to the above discussion and independently on the chosen solution (assumed as
.configure()
below for clarity), there's a question whether we should configure on anApolloRequest
or anApolloCall
.ApolloRequest is OkHttp like:
ApolloCall would allow to easily chain calls:
Resources/Links
@JvmOverloads
is not a silver bullet for Java compatibility and doesn't allow any arbitrary combination of arguments: https://dzone.com/articles/java-friendly-kotlin-default-argumentsConclusion
My early thoughts would be to go with
Builders
because it's a battle tested solution and forces using the return value by construction. But I'd like to avoid changing that too much so will leave this ticket open for some time before comitting toBuilders
to make sure we're not forgetting anything.Any thoughts? Preferences? Other solutions? Leave a comment below!
The text was updated successfully, but these errors were encountered: