https://kotlinlang.org logo
d

dr.dreigh

07/23/2019, 1:38 PM
Hi, we're wondering if anyone knows of a good GraphQL client for calling graphql endpoints? Ideally would generate types from the schema and simplify argument/variable passing
d

Dariusz Kuc

07/23/2019, 1:59 PM
I think the most popular one is https://github.com/apollographql/apollo-android
ignore the name as it is not android specific
IMHO generated code is not that great (e.g. it would be nice to have some K data classes instead) but it is just a generated code
d

dr.dreigh

07/23/2019, 2:39 PM
Thanks @Dariusz Kuc - does it need you to pull in the android plugin?
I think the kotlin data classes are experimental?
d

Dariusz Kuc

07/23/2019, 2:40 PM
Oo thats new, didn't use it in a while
d

dr.dreigh

07/23/2019, 2:41 PM
have you used it in a non android project?
d

Dariusz Kuc

07/23/2019, 2:41 PM
you can either use apollo gradle plugin or open source maven plugin (use fork as original one is unsupported)
yes it works fine
in the end it just runs node module to download schema and generate the code
so technically you could just run npm directly (from a plugin)
d

dr.dreigh

07/23/2019, 2:43 PM
oh I see, thanks!
d

Dariusz Kuc

07/23/2019, 2:44 PM
looks like they still haven't fixed this though -> https://github.com/apollographql/apollo-android/issues/1042
a

Anand Elangovan

07/24/2019, 2:11 PM
Thanks Dariusz. Do you know if there is github link to the project which does not use the plugin, please?
d

Dariusz Kuc

07/24/2019, 2:13 PM
you mean a project that use the plugin?
a

Anand Elangovan

07/24/2019, 7:24 PM
a project which does not use the plugin and uses only data classes.
d

Dariusz Kuc

07/24/2019, 7:25 PM
you have to generate those classes somehow
technically graphql request is just a POST with
{ "query": "<your query>", "variables": {}, "operationName": "someName" }
*or get with query params
so you could easily manually craft those requests from code
that being said
by using apollo plugin you are getting type safety as generated types are based on your schema
a

Anand Elangovan

07/25/2019, 9:14 AM
Thanks Dariusz. We will give it a go.
m

mbonnin

07/27/2019, 4:51 PM
Sorry just seeing this now. apollo-android works fine on non-android project, I've done that myself for a few projects. It does pull the android plugin in the buildscript classpath though. There's an issue open there for that and other plugin issue: https://github.com/apollographql/apollo-android/issues/817.
As for generated classes, they can now be generated in Kotlin with apollo.generateKotlinModels = true. IIRC, the generated code is using data classes
(which are not experimental anymore)
11 Views