graphql-kotlin icon indicating copy to clipboard operation
graphql-kotlin copied to clipboard

Kotlin multiplatform client

Open jillesvangurp opened this issue 5 years ago • 11 comments

Is your feature request related to a problem? Please describe.

The current ktor client (4.0.0-alpha.3) is close to what we need to target IOS and Android with a multiplatform client for our graphql server but not quite. However, a few simple fixes would make it possible to turn this into a multiplatform project allowing the generated code to be used from web, native, wasm, and jvm platforms.

Describe the solution you'd like

  • Change the gradle build file for the ktor client to use the multiplatform plugin. Define e.g. jvm, ios-x86 and ios-arm, and js variants.
  • Move configurations for ktor engine and json serialization into the plaftorm specific part of the source tree and keep the rest of the client code in the common part. From what I've seen there are no big obstacles to doing this as the generated code is cross platform and the ktor client is already cross platform.
  • For the jvm builds, switch to using gson, which is the defacto serialization on Android instead of jackson.

Describe alternatives you've considered

  • We could build our own client and simply use the same class name and packages so the imports for the generated code work. This should not be hard. Short term this is probably what we will do.

  • Alternatively, the generated code could be changed to simply require the user to provide their own ktor httpclient instance. It would then be up to them to configure that properly and the generated code would have no com.expedia.* dependencies at all. Given how small this single class is, there is actually limited value in having a (wrong) default implementation there. At best its a convenience that users will probably want to override.

jillesvangurp avatar Sep 09 '20 09:09 jillesvangurp

Making this a multiplatform client would be amazing! I'm looking for a GraphQL client for a KotlinJS project and got very excited when I saw this project! I don't think there's much out there in terms of solid GraphQL HTTP clients so it'd might be an opportunity for this project to take a lead.

frankkoornstra avatar Oct 15 '20 12:10 frankkoornstra

@frankkoornstra you may want to check out the appollo gradle plugin which does something similar to this client for kotlin.

For this project, I've currently got a jvm only client. There are a few blockers that make multiplatform a bit tricky that I'm investigating workarounds for:

  • the plugin only works with a jvm style project and breaks if you use it with a multiplatform project. A potential workaround would be letting it generate source code and then use a second gradle module that grabs the generated source folder and adds it to common/main in a multiplatform setup. The proper fix would be making the plugin be a bit more flexible.
  • you need a custom GraphqlClient implementation. The jar that contains that interface has a few rogue jvm specific dependencies. Workaround: just lose the dependency and copy the interface definition with the same package and name.
  • parsing generic types in a crossplaform way is tricky (things like GraphqlResponse<T> are challenging) because reflection is not fully multiplatform yet. Potential workaround I have in mind: try to use kotlinx serialization for this (they released a few weeks ago).

Other than that, we acutally integrated a first version of this in our android app last week. We are still looking to do the same for web, and will likely get around to that at some point.

jillesvangurp avatar Oct 15 '20 13:10 jillesvangurp

Currently our handling of polymorphic types as well as default enum values relies on Jackson functionality. As mentioned above I believe we can probably re-implement it using kotlinx.serialization which would allow for multiplatform use.

dariuszkuc avatar Oct 17 '20 04:10 dariuszkuc

👍 for moving from jackson to kotlinx.serialization instead of gson

smyrick avatar Oct 19 '20 03:10 smyrick

Moving the specific issue of jackson to over here: https://github.com/ExpediaGroup/graphql-kotlin/issues/929

smyrick avatar Nov 03 '20 23:11 smyrick

Any hope for targeting kotlin/js as well?

andylamax avatar Apr 25 '21 10:04 andylamax

@andylamax In 4.0.0 we have official released the client with kotlinx.serialization support. That means that the client Kotlin compiler you use in your project could be any Kotlin implementation.

@dariuszkuc Just to confirm, we can now close this issue with the latest release for multi platform support in the client, right?

smyrick avatar Apr 25 '21 18:04 smyrick

@smyrick this issue is still valid. With the latest change it should be much easier to use on android but we don't publish multiplatform artifacts (yet).

dariuszkuc avatar Apr 25 '21 19:04 dariuszkuc

Thanks for the response. Is there a reason why you don't publish multiplatform artifacts (yet)? also is there an ETA to that publication?

andylamax avatar Apr 26 '21 01:04 andylamax

@andylamax currently it is not a priority for us and most likely we won't be working on this in the near future. We are always open to contributions!

dariuszkuc avatar Apr 29 '21 16:04 dariuszkuc

something i wrote for our selmade multiplatform graphql client, might be useful for other people it generates the body of a graphql request based on the kotlinx-serializer descriptor, it works for js and jvm

https://gist.github.com/NikkyAI/3b711373ffc4061f1933803114aacd04

i probably missed some edgecases, but it works for our api so far

NikkyAI avatar May 10 '21 11:05 NikkyAI