Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apollo-api/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ package com.apollographql.apollo.api {
method public error.NonExistentClass hashCode();
}

public static final class CustomTypeValue.GraphQLNull extends com.apollographql.apollo.api.CustomTypeValue {
field public static final com.apollographql.apollo.api.CustomTypeValue.GraphQLNull INSTANCE;
}

public static final class CustomTypeValue.GraphQLNumber extends com.apollographql.apollo.api.CustomTypeValue {
ctor public CustomTypeValue.GraphQLNumber(error.NonExistentClass);
method public error.NonExistentClass equals(error.NonExistentClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import kotlin.jvm.JvmStatic
**/
sealed class CustomTypeValue<T>(@JvmField val value: T) {

object GraphQLNull: CustomTypeValue<Unit>(Unit)

/**
* Represents a `String` value
*/
Expand Down Expand Up @@ -91,6 +93,7 @@ sealed class CustomTypeValue<T>(@JvmField val value: T) {

companion object {

// TODO: should this method accept nullable values in case a scalar type is represented as 'null'?
@JvmStatic
@Suppress("UNCHECKED_CAST")
fun fromRawValue(value: Any): CustomTypeValue<*> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ class ScalarTypeAdapters(val customAdapters: Map<ScalarType, CustomTypeAdapter<*
} +
mapOf("com.apollographql.apollo.api.FileUpload" to object : CustomTypeAdapter<FileUpload> {
override fun decode(value: CustomTypeValue<*>): FileUpload {
// TODO: is there a valid use case for decoding a FileUpload or should we throw here?
return FileUpload("", value.value?.toString() ?: "")
}

override fun encode(value: FileUpload): CustomTypeValue<*> {
return GraphQLString(value.mimetype)
return GraphQLNull
}
}) +
createDefaultScalarTypeAdapter("java.util.Map", "kotlin.collections.Map") { value ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class InputFieldJsonWriter(
is GraphQLString -> writeString(fieldName, customTypeValue.value)
is GraphQLBoolean -> writeBoolean(fieldName, customTypeValue.value)
is GraphQLNumber -> writeNumber(fieldName, customTypeValue.value)
is GraphQLNull -> writeString(fieldName, null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should we introduce writeNull to our JsonWriter? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JsonWriter already has nullValue, would that be ok?

We could also add writeNull to InputFieldWriter but that would be a much more invasive change that I'm not too keen on doing in 2.x. Falling into the rabbit hole of InputFieldsWriters, I realized that SortedInputFieldMapWriter doesn't handle custom scalar types. So custom scalars as field arguments will most likely fill the cache with different values. I guess something to keep in mind for 3.x

is GraphQLJsonObject -> jsonWriter.name(fieldName).apply { writeToJson(customTypeValue.value, this) }
is GraphQLJsonList -> jsonWriter.name(fieldName).apply { writeToJson(customTypeValue.value, this) }
}
Expand Down Expand Up @@ -195,6 +196,7 @@ class InputFieldJsonWriter(
is GraphQLNumber -> writeNumber(customTypeValue.value)
is GraphQLJsonObject -> writeToJson(customTypeValue.value, jsonWriter)
is GraphQLJsonList -> writeToJson(customTypeValue.value, jsonWriter)
is GraphQLNull -> writeString(null)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ class InputFieldJsonWriterTest {
assertEquals("{\"someField\":\"someValue\",\"someNullField\":null", jsonBuffer.readUtf8())
}

@Test
fun writeCustomNull() {
val customTypeAdapters: MutableMap<ScalarType, CustomTypeAdapter<*>> = HashMap()
val scalarType = MockCustomScalarType(CustomTypeValue.GraphQLNumber::class, "com.apollographql.apollo.api.CustomTypeValue.GraphQLNull")
customTypeAdapters[scalarType] = object : MockCustomTypeAdapter() {
override fun encode(value: Any?): CustomTypeValue<*> {
return CustomTypeValue.GraphQLNull
}
}
val inputFieldJsonWriter = InputFieldJsonWriter(jsonWriter, ScalarTypeAdapters(customTypeAdapters))
inputFieldJsonWriter.writeCustom("someField", scalarType, null)
assertEquals("{\"someField\":null", jsonBuffer.readUtf8())
}

@Test
fun writeCustomJsonObject() {
val value = mapOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"operationName":"MultipleUpload","variables":{"files":["image/jpg","image/png"]},"query":"mutation MultipleUpload($files: [Upload!]!) { multipleUpload(files: $files) { __typename id path filename mimetype } }"}
{"operationName":"MultipleUpload","variables":{"files":[null,null]},"query":"mutation MultipleUpload($files: [Upload!]!) { multipleUpload(files: $files) { __typename id path filename mimetype } }"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"operationName":"NestedUpload","variables":{"topFile":"image/png","topFileList":["image/jpg","plain/txt"],"nested":{"recursiveNested":[{"file":"plain/txt","fileList":["image/jpg","image/png"]},{"file":"image/jpg","fileList":["plain/txt","image/png"]}],"file":"image/png","fileList":["plain/txt","image/jpg"]}},"query":"mutation NestedUpload($topFile: Upload, $topFileList: [Upload], $nested: NestedObject) { nestedUpload(topFile: $topFile, topFileList: $topFileList, nested: $nested) }"}
{"operationName":"NestedUpload","variables":{"topFile":null,"topFileList":[null,null],"nested":{"recursiveNested":[{"file":null,"fileList":[null,null]},{"file":null,"fileList":[null,null]}],"file":null,"fileList":[null,null]}},"query":"mutation NestedUpload($topFile: Upload, $topFileList: [Upload], $nested: NestedObject) { nestedUpload(topFile: $topFile, topFileList: $topFileList, nested: $nested) }"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"operationName":"SingleUpload","variables":{"file":"image/jpg"},"query":"mutation SingleUpload($file: Upload!) { singleUpload(file: $file) { __typename id path filename mimetype } }"}
{"operationName":"SingleUpload","variables":{"file":null},"query":"mutation SingleUpload($file: Upload!) { singleUpload(file: $file) { __typename id path filename mimetype } }"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"operationName":"SingleUploadTwice","variables":{"file1":"image/jpg","file2":"image/png"},"query":"mutation SingleUploadTwice($file1: Upload!, $file2: Upload!) { file1: singleUpload(file: $file1) { __typename id path filename mimetype } file2: singleUpload(file: $file2) { __typename id path filename mimetype } }"}
{"operationName":"SingleUploadTwice","variables":{"file1":null,"file2":null},"query":"mutation SingleUploadTwice($file1: Upload!, $file2: Upload!) { file1: singleUpload(file: $file1) { __typename id path filename mimetype } file2: singleUpload(file: $file2) { __typename id path filename mimetype } }"}

This file was deleted.

Loading