Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Kotlin Multiplatform into apollo-api module #2062

Merged
merged 13 commits into from Mar 13, 2020
36 changes: 27 additions & 9 deletions apollo-api/build.gradle.kts
@@ -1,18 +1,36 @@
plugins {
`java-library`
kotlin("jvm")
kotlin("multiplatform")
}

dependencies {
add("compileOnly", groovy.util.Eval.x(project, "x.dep.kotlin.stdLib"))
add("compileOnly", groovy.util.Eval.x(project, "x.dep.okHttp.okHttp"))
kotlin {
jvm {
withJava()
}

add("api", groovy.util.Eval.x(project, "x.dep.kotlin.stdLib"))
add("api", groovy.util.Eval.x(project, "x.dep.okio"))
sourceSets {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
api("com.squareup.okio:okio-multiplatform:2.4.3")
tasomaniac marked this conversation as resolved.
Show resolved Hide resolved
}
}

add("testImplementation", groovy.util.Eval.x(project, "x.dep.junit"))
add("testImplementation", groovy.util.Eval.x(project, "x.dep.truth"))
add("testImplementation", groovy.util.Eval.x(project, "x.dep.okHttp.okHttp"))
val jvmMain by getting {
dependsOn(commonMain)
dependencies {
implementation(kotlin("stdlib"))
}
}
val jvmTest by getting {
dependsOn(jvmMain)
dependencies {
implementation(groovy.util.Eval.x(project, "x.dep.junit"))
implementation(groovy.util.Eval.x(project, "x.dep.truth"))
implementation(groovy.util.Eval.x(project, "x.dep.okHttp.okHttp"))
}
}
}
}

tasks.withType<Checkstyle> {
Expand Down
4 changes: 2 additions & 2 deletions apollo-api/gradle.properties
@@ -1,4 +1,4 @@
POM_ARTIFACT_ID=apollo-api
POM_ARTIFACT_ID=apollo-api-jvm
tasomaniac marked this conversation as resolved.
Show resolved Hide resolved
POM_NAME=Apollo GraphQL API
POM_DESCRIPTION=Apollo GraphQL API classes
POM_PACKAGING=jar
POM_PACKAGING=jar
@@ -0,0 +1,7 @@
package com.apollographql.apollo.api

expect class BigDecimal(value: String?) {
tasomaniac marked this conversation as resolved.
Show resolved Hide resolved
fun toInt(): Int
fun toLong(): Long
fun toDouble(): Double
}
@@ -1,5 +1,8 @@
package com.apollographql.apollo.api

import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic

/**
* A wrapper class for representation of custom GraphQL type value, used in user provided [CustomTypeAdapter]
* encoding / decoding functions.
Expand All @@ -13,11 +16,12 @@ sealed class CustomTypeValue<T>(@JvmField val value: T) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is GraphQLString) return false
if (value != other.value) return false
return true
}

override fun hashCode(): Int {
return javaClass.hashCode()
tasomaniac marked this conversation as resolved.
Show resolved Hide resolved
return value.hashCode()
}
}

Expand All @@ -28,11 +32,12 @@ sealed class CustomTypeValue<T>(@JvmField val value: T) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is GraphQLBoolean) return false
if (value != other.value) return false
return true
}

override fun hashCode(): Int {
return javaClass.hashCode()
return value.hashCode()
}
}

Expand All @@ -43,11 +48,12 @@ sealed class CustomTypeValue<T>(@JvmField val value: T) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is GraphQLNumber) return false
if (value != other.value) return false
return true
}

override fun hashCode(): Int {
return javaClass.hashCode()
return value.hashCode()
}
}

Expand All @@ -58,11 +64,12 @@ sealed class CustomTypeValue<T>(@JvmField val value: T) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is GraphQLJsonObject) return false
if (value != other.value) return false
return true
}

override fun hashCode(): Int {
return javaClass.hashCode()
return value.hashCode()
}
}

Expand All @@ -73,11 +80,12 @@ sealed class CustomTypeValue<T>(@JvmField val value: T) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is GraphQLJsonList) return false
if (value != other.value) return false
return true
}

override fun hashCode(): Int {
return javaClass.hashCode()
return value.hashCode()
}
}

Expand Down
@@ -0,0 +1,3 @@
package com.apollographql.apollo.api

expect class FileUpload
@@ -1,5 +1,8 @@
package com.apollographql.apollo.api

import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic

/**
* A wrapper class of provide [value] with a [defined] state.
*
Expand Down
Expand Up @@ -4,5 +4,9 @@

package com.apollographql.apollo.api

import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic

@JvmSynthetic
inline fun <T : Any> T?.toInput(): Input<T> = Input.optional(this)
Expand Up @@ -4,11 +4,14 @@ import com.apollographql.apollo.api.internal.InputFieldMarshaller
import com.apollographql.apollo.api.internal.InputFieldWriter
import com.apollographql.apollo.api.internal.ResponseFieldMapper
import com.apollographql.apollo.api.internal.ResponseFieldMarshaller
import com.apollographql.apollo.api.internal.Throws
import com.apollographql.apollo.api.internal.json.InputFieldJsonWriter
import com.apollographql.apollo.api.internal.json.JsonWriter
import com.apollographql.apollo.api.internal.json.use
import okio.Buffer
import okio.BufferedSource
import java.io.IOException
import okio.IOException
import kotlin.jvm.JvmField

/**
* Represents a GraphQL operation (mutation or query).
Expand Down
Expand Up @@ -4,7 +4,9 @@ package com.apollographql.apollo.api

import com.apollographql.apollo.api.ScalarTypeAdapters.Companion.DEFAULT
import com.apollographql.apollo.api.internal.SimpleResponseWriter
import java.io.IOException
import okio.IOException
import kotlin.jvm.JvmName
import kotlin.jvm.JvmOverloads

/**
* Serializes GraphQL operation response data into its equivalent Json representation.
Expand Down
@@ -1,5 +1,7 @@
package com.apollographql.apollo.api

import kotlin.jvm.JvmStatic

/**
* Represents either a successful or failed response received from the GraphQL server.
*/
Expand Down
@@ -1,5 +1,7 @@
package com.apollographql.apollo.api

import kotlin.jvm.JvmStatic

/**
* An abstraction for a field in a graphQL operation. Field can refer to:
* - GraphQL
Expand Down
Expand Up @@ -3,8 +3,6 @@ package com.apollographql.apollo.api
/**
* Represents a custom GraphQL scalar type
*/
interface ScalarType {
expect interface ScalarType {
fun typeName(): String

fun javaType(): Class<*>
tasomaniac marked this conversation as resolved.
Show resolved Hide resolved
}
@@ -0,0 +1,10 @@
package com.apollographql.apollo.api

expect class ScalarTypeAdapters(customAdapters: Map<ScalarType, CustomTypeAdapter<*>>) {
tasomaniac marked this conversation as resolved.
Show resolved Hide resolved

fun <T : Any> adapterFor(scalarType: ScalarType): CustomTypeAdapter<T>

companion object {
val DEFAULT: ScalarTypeAdapters
}
}
@@ -1,6 +1,7 @@
package com.apollographql.apollo.api.internal

import java.io.IOException
import okio.IOException
import kotlin.jvm.JvmSynthetic

interface InputFieldMarshaller {
@Throws(IOException::class)
Expand Down
@@ -1,7 +1,8 @@
package com.apollographql.apollo.api.internal

import com.apollographql.apollo.api.ScalarType
import java.io.IOException
import okio.IOException
import kotlin.jvm.JvmSynthetic

interface InputFieldWriter {
@Throws(IOException::class)
Expand Down
@@ -1,5 +1,7 @@
package com.apollographql.apollo.api.internal

import kotlin.jvm.JvmStatic

object QueryDocumentMinifier {
@JvmStatic
fun minify(queryDocument: String): String {
Expand Down
@@ -1,5 +1,7 @@
package com.apollographql.apollo.api.internal

import kotlin.jvm.JvmSynthetic

/**
* ResponseFieldMapper is an abstraction for mapping the response data returned by
* the server back to generated models.
Expand Down
@@ -1,5 +1,7 @@
package com.apollographql.apollo.api.internal

import kotlin.jvm.JvmSynthetic

interface ResponseFieldMarshaller {
fun marshal(writer: ResponseWriter)

Expand Down
Expand Up @@ -2,6 +2,7 @@ package com.apollographql.apollo.api.internal

import com.apollographql.apollo.api.ResponseField
import com.apollographql.apollo.api.ScalarType
import kotlin.jvm.JvmSynthetic

/*
* ResponseReader is an abstraction for reading GraphQL fields.
Expand Down
Expand Up @@ -2,6 +2,7 @@ package com.apollographql.apollo.api.internal

import com.apollographql.apollo.api.ResponseField
import com.apollographql.apollo.api.ScalarType
import kotlin.jvm.JvmSynthetic

interface ResponseWriter {
fun writeString(field: ResponseField, value: String?)
Expand Down
Expand Up @@ -6,8 +6,10 @@ import com.apollographql.apollo.api.Response
import com.apollographql.apollo.api.ScalarTypeAdapters
import com.apollographql.apollo.api.internal.json.BufferedSourceJsonReader
import com.apollographql.apollo.api.internal.json.ResponseJsonStreamReader
import com.apollographql.apollo.api.internal.json.use
import okio.BufferedSource
import java.io.IOException
import okio.IOException
import kotlin.jvm.JvmStatic

object SimpleOperationResponseParser {

Expand Down
@@ -1,7 +1,6 @@
package com.apollographql.apollo.api.internal

import com.apollographql.apollo.api.*
import java.math.BigDecimal

class SimpleResponseReader private constructor(
private val recordSet: Map<String, Any?>,
Expand Down Expand Up @@ -125,12 +124,12 @@ class SimpleResponseReader private constructor(
val conditionValue = variableValues[condition.variableName] as Boolean
if (condition.inverted) {
// means it's a skip directive
if (java.lang.Boolean.TRUE == conditionValue) {
tasomaniac marked this conversation as resolved.
Show resolved Hide resolved
if (conditionValue) {
return true
}
} else {
// means it's an include directive
if (java.lang.Boolean.FALSE == conditionValue) {
if (!conditionValue) {
return true
}
}
Expand Down
Expand Up @@ -5,8 +5,9 @@ import com.apollographql.apollo.api.ScalarType
import com.apollographql.apollo.api.ScalarTypeAdapters
import com.apollographql.apollo.api.internal.json.JsonWriter
import com.apollographql.apollo.api.internal.json.Utils.writeToJson
import com.apollographql.apollo.api.internal.json.use
import okio.Buffer
import java.io.IOException
import okio.IOException

class SimpleResponseWriter(private val scalarTypeAdapters: ScalarTypeAdapters) : ResponseWriter {
private val data: MutableMap<String, Any?> = LinkedHashMap()
Expand Down
@@ -0,0 +1,5 @@
package com.apollographql.apollo.api.internal

import kotlin.reflect.KClass

expect annotation class Throws(vararg val exceptionClasses: KClass<out Throwable>)
@@ -1,5 +1,8 @@
package com.apollographql.apollo.api.internal

import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic

/**
* Contains utility methods for checking Preconditions
*/
Expand Down
Expand Up @@ -15,12 +15,13 @@
*/
package com.apollographql.apollo.api.internal.json

import com.apollographql.apollo.api.internal.Throws
import okio.Buffer
import okio.BufferedSource
import okio.ByteString
import okio.ByteString.Companion.encodeUtf8
import java.io.EOFException
import java.io.IOException
import okio.EOFException
import okio.IOException

class BufferedSourceJsonReader(private val source: BufferedSource) : JsonReader {
private val buffer: Buffer = source.buffer
Expand Down Expand Up @@ -530,7 +531,7 @@ class BufferedSourceJsonReader(private val source: BufferedSource) : JsonReader
throw JsonDataException("Expected a double but was $peekedString at path ${getPath()}")
}

if (!lenient && (java.lang.Double.isNaN(result) || java.lang.Double.isInfinite(result))) {
if (!lenient && (result.isNaN() || result.isInfinite())) {
throw JsonEncodingException("JSON forbids NaN and infinities: $result at path ${getPath()}")
}

Expand Down Expand Up @@ -588,7 +589,7 @@ class BufferedSourceJsonReader(private val source: BufferedSource) : JsonReader
* sequences encountered along the way. The opening quote should have already been read. This
* consumes the closing quote, but does not include it in the returned string.
*
* @throws IOException if any unicode escape sequences are malformed.
* @throws okio.IOException if any unicode escape sequences are malformed.
*/
@Throws(IOException::class)
private fun nextQuotedValue(runTerminator: ByteString): String {
Expand Down Expand Up @@ -856,7 +857,7 @@ class BufferedSourceJsonReader(private val source: BufferedSource) : JsonReader
* Unescapes the character identified by the character or characters that immediately follow a backslash. The backslash '\' should have
* already been read. This supports both unicode escapes "u000A" and two-character escapes "\n".
*
* @throws IOException if any unicode escape sequences are malformed.
* @throws okio.IOException if any unicode escape sequences are malformed.
*/
@Throws(IOException::class)
private fun readEscapeCharacter(): Char {
Expand Down
@@ -0,0 +1,5 @@
package com.apollographql.apollo.api.internal.json

expect interface Closeable {
fun close()
}