How to use Apollo Android from Kotlin, but without Android? - kotlin

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.

I don't know where that builder is supposed to be, but you can instantiate the query directly and use that.
This is an example of consumer code (last tested with version 2.1.0):
import com.apollographql.apollo.ApolloCall
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.api.Response
import com.apollographql.apollo.exception.ApolloException
import okhttp3.OkHttpClient
fun main(args: Array<String>) {
val serverUrl = "https://api.github.com/graphql"
val authHeader = args[0]
// Add authentication header for GitHub
val okHttpClient = OkHttpClient.Builder()
.addInterceptor { chain ->
val builder = chain.request().newBuilder()
builder.header("Authorization", "Bearer $authHeader")
chain.proceed(builder.build())
}
.build()
val apolloClient = ApolloClient.builder()
.serverUrl(serverUrl)
.okHttpClient(okHttpClient)
.build()
val query = ViewLoginQuery()
apolloClient.query(query).enqueue(object : ApolloCall.Callback<ViewLoginQuery.Data?>() {
override fun onResponse(dataResponse: Response<ViewLoginQuery.Data?>) {
val data = dataResponse.data
if (data == null) {
println("No data received")
println(dataResponse.errors)
} else {
println(dataResponse.data?.viewer?.login)
}
}
override fun onFailure(e: ApolloException) {
println(e.message)
}
})
}
To fix the 'unknown field' errors in the .graphql file, you have to download the schema as a schema.graphql instead of schema.json, see https://github.com/jimkyndemeyer/js-graphql-intellij-plugin/issues/305 for the issue report.
You also have to provide the github token in the header. Change your .graphqlconfig to the following:
{
"name": "GitHub GraphQL Schema",
"schemaPath": "./schema.graphql",
"extensions": {
"endpoints": {
"Default GraphQL Endpoint": {
"url": "https://api.github.com/graphql",
"headers": {
"user-agent": "JS GraphQL",
"Authorization": "Bearer ${env:GITHUB_GRAPHQL_TOKEN}"
},
"introspect": true
}
}
}
}
You can now easily download the new schema by clicking the 'run introspection query' gutter icon next to the "url": line.
You will get a popup asking for the token. Unfortunately the plugin does not yet support reading the environment variable from anywhere, see for example https://github.com/jimkyndemeyer/js-graphql-intellij-plugin/issues/285
However, Apollo Android requires a schema.json, so you need to keep the one you downloaded!
Since Apollo Android will (at least for me) error on the schema.graphql file, tell it to ignore that file by changing the Gradle task configuration to
apollo {
generateKotlinModels.set(true)
graphqlSourceDirectorySet.srcDir("src/main/graphql")
graphqlSourceDirectorySet.include("**/*.graphql")
graphqlSourceDirectorySet.exclude("**/schema.graphql")
}
For documentation on those options, see https://www.apollographql.com/docs/android/essentials/plugin-configuration/
To summarize: for the JS GraphQL plugin, which you want in order to easily work with graphql files, you need the schema.graphql which you download from the .graphqlconfig. For Apollo Android, you need the schema.json, which you download using the Apollo cli interface.
[Edit June 2020] For Apollo Android 2, you can use the new Gradle plugin, so the complete build.gradle.kts becomes as follows:
Remember to always check for dependency updates before using, for example using the help/useLatestVersions Gradle task from the use-latest-versions Gradle plugin.
plugins {
val kotlinVersion = "1.3.72"
application
kotlin("jvm") version kotlinVersion
java
idea
// Plugin which checks for dependency updates with help/dependencyUpdates task.
id("com.github.ben-manes.versions") version "0.28.0"
// Plugin which can update Gradle dependencies, use help/useLatestVersions
id("se.patrikerdes.use-latest-versions") version "0.2.14"
id("com.apollographql.apollo") version "2.1.0"
}
dependencies {
implementation(kotlin("stdlib"))
// Apollo and dependencies
implementation("com.apollographql.apollo:apollo-runtime:2.1.0")
implementation("com.squareup.okio:okio:2.4.3")
implementation("org.jetbrains:annotations:19.0.0")
testImplementation("org.jetbrains:annotations:19.0.0")
}
repositories {
mavenCentral()
mavenLocal()
jcenter()
}
apollo {
generateKotlinModels.set(true)
graphqlSourceDirectorySet.srcDir("src/main/graphql")
graphqlSourceDirectorySet.include("**/*.graphql")
graphqlSourceDirectorySet.exclude("**/schema.graphql")
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}

Related

Kotest + Multiplatform Kotlin/JS project: Unable to initialize main class io.kotest.launcher.LauncherKt

Having troubles setting up Kotest in my multiplatform project (only using JS, multiplatform part is needed for some libraries).
Here's relevant parts of my Gradle file. If this is relevant, it's in one of the modules of a project, I have multiple projects that create a separate html and js files, and then collect them in one place with a separate task (using template from Kromex to write a browser extension)
js(IR) {
binaries.executable()
useCommonJs()
browser {
webpackTask {
outputFileName = "base.js"
sourceMaps = false
report = true
}
distribution {
directory = File("$projectDir/../build/distributions/")
}
}
}
sourceSets {
val jsMain by getting {
dependencies {
...
}
}
val jsTest by getting {
dependencies {
implementation("io.kotest:kotest-framework-engine:5.5.1")
implementation("io.kotest:kotest-assertions-core:5.5.1")
implementation("io.kotest:kotest-property:5.5.1")
}
}
}
I tried both this, and also with kotest-framework-engine in commonTest (as specified in quickstart guide) - result is the same.
At first, after trying to run it (with Intellij Plugin) it complains that there's no JDK specified. After I manually go to project structure and switch jsTest module SDK from Kotlin SDK that it uses by default to "Project SDK 15", it stops, but then a new problem happens:
Error: Unable to initialize main class io.kotest.launcher.LauncherKt
Caused by: java.lang.NoClassDefFoundError: io/kotest/core/engine/TestEngineListener
each time I try to launch a test. Also i've checked, io.kotest.core.engine.TestEngineListener exists and can be accessed, while io.kotest.launcher.LauncherKt does not
What I might be doing wrong?

Runing coroutines in IntelliJ Kotlinx error

I'm trying to run a native Kotlin project using coroutines using IntelliJ IDEA Community 2020.
Here is how my build.gradle looks:
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.3.72'
}
repositories {
mavenCentral()
}
kotlin {
// For ARM, should be changed to iosArm32 or iosArm64
// For Linux, should be changed to e.g. linuxX64
// For MacOS, should be changed to e.g. macosX64
// For Windows, should be changed to e.g. mingwX64
mingwX64("mingw") {
binaries {
executable {
// Change to specify fully qualified name of your application's entry point:
entryPoint = 'sample.main'
// Specify command-line arguments, if necessary:
runTask?.args('')
}
}
}
sourceSets {
// Note: To enable common source sets please comment out 'kotlin.import.noCommonSourceSets' property
// in gradle.properties file and re-import your project in IDE.
mingwMain {
}
mingwTest {
}
}
}
// Use the following Gradle tasks to run your application:
// :runReleaseExecutableMingw - without debug symbols
// :runDebugExecutableMingw - with debug symbols
And here is a simple KT file:
package sample
import kotlinx.coroutines.*
fun main() = runBlocking<Unit> {
val deferred = async(Dispatchers.Unconfined, CoroutineStart.LAZY) {
println("Running Async Unconfined: on thread ${Thread.currentThread().name} has run.")
42
}
val result = deferred.await()
println("Async Unconfined Result is ${result}")
}
I installed the maven plugin under Project Structure | Module and screenshot attached.
Nevertheless, I'm getting "Unresolved References..." error. Attached screenshot...
Request if someone can help me to resolve this please?
Thanks
In your Project Structure > Modules > Dependencies tab, you selected Runtime as the scope, which makes the dependency only available at runtime (usually used for transitive dependencies). Try selecting Compile here.

Create fat jar from kotlin multiplatform project

I recently switched from old 1.2 multiplatform into 1.3. Difference is, there's one one build.gradle file per multiplatform module (I got 5 of them) so a lot less configuration.
However I can't seem to be able to configure creating runnable fat jar with all dependencies from jvm platform.
I used to use standard "application" plugin in my jvm project and jar task, but that does not work anymore. I found there's "jvmJar" task and I modified it (set Main-class), but created jar doesn't contain dependencies and crashes on ClassNotFoundException. How do I do it?
This is what I have now:
jvm() {
jvmJar {
manifest {
attributes 'Main-Class': 'eu.xx.Runner'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
}
I did hit that bump and used this work around.
1. Restructure your project
Lets call your project Project.
create another submodule say subA, which will have the gradle notation Project:subA
now, subA has your multiplatform code in it (It is the gradle project with apply :kotlin-multiplafrom) in its build.gradle
2. Add Another submodule
create another submodule which targets only jvm say subB, which will have the gradle notation Project:subB
So, subB will have plugins: 'application' and 'org.jetbrains.kotlin.jvm'
3. Add your module as a gradle dependency (see my build.gradle)
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.31'
id "application"
}
apply plugin: "kotlinx-serialization"
group 'tz.or.self'
version '0.0.0'
mainClassName = "com.example.MainKt"
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
dependencies {
implementation project(':subA')
}
you can proceed and build subB as you would a regular java project or even use the existing plugins, it will work
Got it working with the multiplatform plugin in kotlin 1.3.61:
The following works for a main file in src/jvmMain/kotlin/com/example/Hello.kt
Hello.kt must also specify its package as package com.example
I configured my jvm target in this way:
kotlin {
targets {
jvm()
configure([jvm]) {
withJava()
jvmJar {
manifest {
attributes 'Main-Class': 'com.example.HelloKt'
}
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}
}
}
}
Got it to work with a slightly modified version of what luca992 did:
kotlin {
jvm() {
withJava()
jvmJar {
manifest {
attributes 'Main-Class': 'sample.MainKt'
}
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}
}
...
}
The only way to get gradle/multiplatform working appears to be endless trial and error; It's a nightmare, it's not being built as a "build" system so much as a "build system"; to put it another way, these two tools (together or in isolation) are a means of implementing only a single software development life cycle that the plugin maker intended, however, if you've engineered a desired software lifecycle and CI/CD system and now your trying to implement that engineering, it will be MUCH harder to do it with these tools than it would be to do it with scripts, code or maven. There are a number of reasons for this:
Massive changing in coding convention due to the plugin makers only exposing bar minimum configurability, probably only giving access to the things they need for their own personal project.
Very poor documentation updates; Kotlin, gradle and plugins are changing so rapidly I have begun to seriously question the usefulness of these tools.
Thus, at the time of writing this seems to be the correct syntax to use when using kotlin 1.3.72, multiplatform 1.3.72, ktor 1.3.2 and gradle 6.2.2 (using the kts format).
Note the fatJar seems to assemble correctly but won't run, it can't find the class, so I included the second runLocally task I've been using in the mean time.
This isn't a complete solution so I hate posting it on here, but from what I can tell... it is the most complete and up to date solution I can find documented anywhere.
//Import variables from gradle.properties
val environment: String by project
val kotlinVersion: String by project
val ktorVersion: String by project
val kotlinExposedVersion: String by project
val mySqlConnectorVersion: String by project
val logbackVersion: String by project
val romeToolsVersion: String by project
val klaxonVersion: String by project
val kotlinLoggingVersion: String by project
val skrapeItVersion: String by project
val jsoupVersion: String by project
val devWebApiServer: String by project
val devWebApiServerVersion: String by project
//Build File Configuration
plugins {
java
kotlin("multiplatform") version "1.3.72"
}
group = "com.app"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
jcenter()
jcenter {
url = uri("https://kotlin.bintray.com/kotlin-js-wrappers")
}
maven {
url = uri("https://jitpack.io")
}
}
//Multiplatform Configuration
kotlin {
jvm {
compilations {
val main = getByName("main")
tasks {
register<Jar>("buildFatJar") {
group = "application"
manifest {
attributes["Implementation-Title"] = "Gradle Jar File Example"
attributes["Implementation-Version"] = archiveVersion
attributes["Main-Class"] = "com.app.BackendAppKt"
}
archiveBaseName.set("${project.name}-fat")
from(main.output.classesDirs, main.compileDependencyFiles)
with(jar.get() as CopySpec)
}
register<JavaExec>("runLocally") {
group = "application"
setMain("com.app.BackendAppKt")
classpath = main.output.classesDirs
classpath += main.compileDependencyFiles
}
}
}
}
js {
browser { EXCLUDED FOR LENGTH }
}
sourceSets { EXCLUDED FOR LENGTH }
}

How to build kotlinx.coroutines in Kotlin/Native (test version 0.23.4-native-1)

This question is a continuation of this thread:
https://github.com/Kotlin/kotlinx.coroutines/issues/246#issuecomment-407023156
I am trying to use org.jetbrains.kotlinx:kotlinx-coroutines-core-native:0.23.4-native-1 in a Kotlin/Native project targeting iOS.
build.gradle:
buildscript {
repositories {
mavenCentral()
maven { url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" }
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-native-gradle-plugin:0.8'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
}
apply plugin: 'kotlin-platform-native'
repositories {
jcenter()
mavenCentral()
maven { url "https://kotlin.bintray.com/kotlinx" }
}
sourceSets {
main {
component {
target 'ios_arm32', 'ios_arm64', 'ios_x64'
outputKinds = [KLIBRARY]
}
}
}
dependencies {
expectedBy project(':common')
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:0.23.4-native-1"
}
The kotlinx:kotlinx-coroutines-core-native dependency doesn't seem to work, as the produces build errors like:
error: unresolved reference: coroutines
import kotlinx.coroutines.experimental.*
^
If I manually include the artifact dependencies such as org.jetbrains.kotlinx:kotlinx-coroutines-core-native_release_ios_x64:0.10.3-native, then I get a complier exception:
exception: java.lang.IllegalStateException: Could not find "atomicfu-native"
This error persists, even if I also add org.jetbrains.kotlinx:atomicfu-native:0.10.3-native dependency.
Here is a list of things to check for (I have been through this, and finally made it work) :
Enable Gradle metadata. It's required to retrieve the coroutines dependencies. To do so, add this line in your "settings.gradle" file, after all the "include" instructions :
enableFeaturePreview('GRADLE_METADATA')
use gradle 4.7 (newer version are incompatible with the meta data of the current coroutines library, they require something with 0.4 version and the current published one uses 0.3)
In the iOS module :
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:0.23.4-native-1"
In your common module :
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.23.4"
If you have a js module, it may fail due to the gradle metadata feature. You can fix it by adding this before each of your "repositories" blocks (https://github.com/srs/gradle-node-plugin/issues/301)
repositories.whenObjectAdded {
if (it instanceof IvyArtifactRepository) {
metadataSources {
artifact()
}
}
}
Hope this will be enough !

How to use Griffon Swingx plugin with IntelliJ?

I've setup a Griffon project using ItelliJ and trying to use SwingX componants in a view but I can't manage to make it work. My griffon-app/conf/BuildConfig.groovy looks like this:
griffon.project.dependency.resolution = {
inherits("global") { }
log "warn"
repositories {
griffonHome()
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
compile 'org.codehaus.griffon.plugins:griffon-swingx-plugin:1.1.0'
}
}
And my view looks like this:
package data.viewer
import java.awt.*
application(title: 'data-viewer',
pack: true,
location: [0,0],
locationByPlatform: true,
iconImage: imageIcon('/griffon-icon-48x48.png').image,
iconImages: [imageIcon('/griffon-icon-48x48.png').image,
imageIcon('/griffon-icon-32x32.png').image,
imageIcon('/griffon-icon-16x16.png').image])
{
panel(){
boxLayout(axis: BoxLayout.Y_AXIS)
jxbutton(text: "Time map")
panel(){
panel(){
label(icon: imageIcon('/europe.jpg'))
}
panel(layout: new GridLayout(10, 1)){
button(text: "Indicatieur1")
}
}
}
}
But I'm still getting an error:
sept. 06, 2015 4:01:10 PM groovy.util.FactoryBuilderSupport createNode
AVERTISSEMENT: Could not find match for name 'jxbutton'
groovy.lang.MissingMethodException: No signature of method: java.lang.Object.jxbutton() is applicable for argument types: (java.util.LinkedHashMap, null) values: [[text:Time map], null]
Can someone tell me what I am missiing?
Griffon plugins served from bintray/jcenter are incompatible with Griffon 1.x. These plugins only work with Griffon 2.x (http://griffon-framework.org/)
Installing plugins for Griffon 1.x requires you to use the Griffon Artifact Portal, for example http://artifacts.griffon-framework.org/plugin/swingx-builder/installation