title: How to use Apollo Android from Kotlin, but without Android?
body: <p>I want to use Apollo Android to query the GitHub GraphQL api, using Kotlin with the Gradle Kotlin DSL but <em>not</em> on Android.</p>
  
  <p><strong>Error message</strong></p>
  
  <p>I do run into a specific problem, but that's probably because there is something wrong in my setup elsewhere.</p>
  
  <p>To summarize, I have a <code>ViewLogin.graphql</code> file, from which a <code>ViewLoginQuery</code> is generated, but when I try to use <code>ViewLoginQuery.builder()</code> then <code>unresolved reference: builder</code> is the problem.</p>
  
  <h1>Setup</h1>
  
  <p>As mentioned in <a href="https://github.com/apollographql/apollo-android/issues/1573" rel="nofollow noreferrer">https://github.com/apollographql/apollo-android/issues/1573</a>, it should be possible to use Apollo Android without Android.</p>
  
  <h2>Environment</h2>
  
  <p>I'm using Arch Linux and IntelliJ, with the JS GraphQL IntelliJ plugin installed.</p>
  
  <h3>Gradle Kotlin DSL build file</h3>
  
  <p>When writing the build file I already ran into a problem: I did not manage to avoid using the <a href="https://docs.gradle.org/current/userguide/plugins.html#sec:old_plugin_application" rel="nofollow noreferrer">deprecated</a> <code>buildscript</code> block. </p>
  
  <p>The plugin shown at <a href="https://plugins.gradle.org/plugin/com.apollographql.apollo" rel="nofollow noreferrer">https://plugins.gradle.org/plugin/com.apollographql.apollo</a> is apparently the incubating plugin as mentioned at <a href="https://github.com/apollographql/apollo-android/issues/1573#issuecomment-575613238" rel="nofollow noreferrer">https://github.com/apollographql/apollo-android/issues/1573#issuecomment-575613238</a> which is not yet ready.</p>
  
  <p>The following build file seems to apply the plugin correctly:</p>
  
  <pre><code>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&lt;com.apollographql.apollo.gradle.ApolloCodegenTask&gt; {
      generateKotlinModels.set(true)
  }
  </code></pre>
  
  <p>so after syncing, I have the apollo Gradle tasks available.</p>
  
  <h2>Downloading the schema</h2>
  
  <p>I installed the <a href="https://www.apollographql.com/docs/devtools/cli/" rel="nofollow noreferrer">Apollo cli interface</a> and then, following the <a href="https://www.apollographql.com/docs/android/essentials/get-started/#downloading-a-schema" rel="nofollow noreferrer">Apollo docs</a>, I ran </p>
  
  <pre><code>apollo schema:download --endpoint=https://api.github.com/graphql --header="Authorization: Bearer mytoken"
  </code></pre>
  
  <p>which gave me a <code>schema.json</code> that I put in <code>src/main/graphql/nl/mypackage/myapplication/schema.json</code>.</p>
  
  <h2>Example query</h2>
  
  <p>I have a file <code>src/main/graphql/nl/mypackage/myapplication/ViewLogin.graphql</code> which contains <code>query ViewLogin { viewer { login }}</code>.
  IntelliJ shows an error on both <code>viewer</code> and <code>login</code>, saying <code>Unknown field "viewer" on object type "Query". Did you mean "viewer"?</code>. But this may be a misconfiguration of the JS GraphQL plugin.
  I tried to configure JS GraphQL by adding a file <code>src/main/graphql/nl/mypackage/myapplication/.graphqlconfig</code> containing</p>
  
  <pre><code>{
    "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
        }
      }
    }
  }
  </code></pre>
  
  <p>but the errors remain.</p>
  
  
  
  <p>Then I executed the Gradle task <code>generateApolloClasses</code> which generated a <code>build/generated/source/apollo/classes/main/nl/mypackage/myapplication/ViewLoginQuery.kt</code> file.
  When I open that file, no errors are shown and everything looks ok.</p>
  
  <h2>Execute the query</h2>
  
  <p>In a file <code>src/main/kotlin/nl/mypackage/myapplication/GraphqlExample.kt</code> I tried to use the query, following the docs at <a href="https://www.apollographql.com/docs/android/essentials/get-started/#consuming-code" rel="nofollow noreferrer">https://www.apollographql.com/docs/android/essentials/get-started/#consuming-code</a>
  but when I try to use <code>ViewLoginQuery.builder()</code> then <code>builder</code> is not recognized (by IntelliJ, and also when I try to run it).</p>
  
  <p>I tried searching in superclasses of ViewLoginQuery but I couldn't find anything about a builder.</p>