I want to use Apollo Android to query the GitHub GraphQL api, using Kotlin with the Gradle Kotlin DSL but not on Android.
Error message
I do run into a specific problem, but that's probably because there is something wrong in my setup elsewhere.
To summarize, I have a ViewLogin.graphql
file, from which a ViewLoginQuery
is generated, but when I try to use ViewLoginQuery.builder()
then unresolved reference: builder
is the problem.
Setup
As mentioned in https://github.com/apollographql/apollo-android/issues/1573, it should be possible to use Apollo Android without Android.
Environment
I'm using Arch Linux and IntelliJ, with the JS GraphQL IntelliJ plugin installed.
Gradle Kotlin DSL build file
When writing the build file I already ran into a problem: I did not manage to avoid using the deprecated buildscript
block.
The plugin shown at https://plugins.gradle.org/plugin/com.apollographql.apollo is apparently the incubating plugin as mentioned at https://github.com/apollographql/apollo-android/issues/1573#issuecomment-575613238 which is not yet ready.
The following build file seems to apply the plugin correctly:
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
jcenter()
}
dependencies {
classpath("com.apollographql.apollo:apollo-gradle-plugin:1.2.2")
}
}
apply(plugin = "com.apollographql.android")
plugins {
val kotlinVersion = "1.3.60"
application
kotlin("jvm") version kotlinVersion
java
idea
}
dependencies {
implementation(kotlin("stdlib"))
// Apollo and dependencies
implementation("com.apollographql.apollo:apollo-runtime:1.2.2")
implementation("com.squareup.okio:okio:2.4.3")
implementation("org.jetbrains:annotations:13.0")
testImplementation("org.jetbrains:annotations:13.0")
}
repositories {
mavenCentral()
mavenLocal()
jcenter()
}
tasks.withType<com.apollographql.apollo.gradle.ApolloCodegenTask> {
generateKotlinModels.set(true)
}
so after syncing, I have the apollo Gradle tasks available.
Downloading the schema
I installed the Apollo cli interface and then, following the Apollo docs, I ran
apollo schema:download --endpoint=https://api.github.com/graphql --header="Authorization: Bearer mytoken"
which gave me a schema.json
that I put in src/main/graphql/nl/mypackage/myapplication/schema.json
.
Example query
I have a file src/main/graphql/nl/mypackage/myapplication/ViewLogin.graphql
which contains query ViewLogin { viewer { login }}
.
IntelliJ shows an error on both viewer
and login
, saying Unknown field "viewer" on object type "Query". Did you mean "viewer"?
. But this may be a misconfiguration of the JS GraphQL plugin.
I tried to configure JS GraphQL by adding a file src/main/graphql/nl/mypackage/myapplication/.graphqlconfig
containing
{
"name": "GitHub GraphQL Schema",
"schemaPath": "schema.json",
"extensions": {
"endpoints": {
"Default GraphQL Endpoint": {
"url": "https://api.github.com/graphql",
"headers": {
"user-agent": "JS GraphQL"
},
"introspect": true
}
}
}
}
but the errors remain.
Then I executed the Gradle task generateApolloClasses
which generated a build/generated/source/apollo/classes/main/nl/mypackage/myapplication/ViewLoginQuery.kt
file.
When I open that file, no errors are shown and everything looks ok.
Execute the query
In a file src/main/kotlin/nl/mypackage/myapplication/GraphqlExample.kt
I tried to use the query, following the docs at https://www.apollographql.com/docs/android/essentials/get-started/#consuming-code
but when I try to use ViewLoginQuery.builder()
then builder
is not recognized (by IntelliJ, and also when I try to run it).
I tried searching in superclasses of ViewLoginQuery but I couldn't find anything about a builder.