Skip to content

Commit

Permalink
Backport #4785 (skip-variable-default-false) to v3 branch (#4792)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD authored Mar 27, 2023
1 parent b6a6289 commit 0677eca
Show file tree
Hide file tree
Showing 67 changed files with 1,661 additions and 89 deletions.
4 changes: 3 additions & 1 deletion libraries/apollo-api/api/apollo-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public abstract interface class com/apollographql/apollo3/api/Adapter {
}

public final class com/apollographql/apollo3/api/AdapterContext {
public synthetic fun <init> (Lcom/apollographql/apollo3/api/Executable$Variables;Ljava/util/Set;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Lcom/apollographql/apollo3/api/Executable$Variables;Ljava/util/Set;ZLkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getSerializeVariablesWithDefaultBooleanValues ()Z
public final fun hasDeferredFragment (Ljava/util/List;Ljava/lang/String;)Z
public final fun newBuilder ()Lcom/apollographql/apollo3/api/AdapterContext$Builder;
public final fun variables ()Ljava/util/Set;
Expand All @@ -17,6 +18,7 @@ public final class com/apollographql/apollo3/api/AdapterContext$Builder {
public fun <init> ()V
public final fun build ()Lcom/apollographql/apollo3/api/AdapterContext;
public final fun mergedDeferredFragmentIds (Ljava/util/Set;)Lcom/apollographql/apollo3/api/AdapterContext$Builder;
public final fun serializeVariablesWithDefaultBooleanValues (Ljava/lang/Boolean;)Lcom/apollographql/apollo3/api/AdapterContext$Builder;
public final fun variables (Lcom/apollographql/apollo3/api/Executable$Variables;)Lcom/apollographql/apollo3/api/AdapterContext$Builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import kotlin.jvm.JvmName
class AdapterContext private constructor(
private val variables: Executable.Variables?,
private val mergedDeferredFragmentIds: Set<DeferredFragmentIdentifier>?,
val serializeVariablesWithDefaultBooleanValues: Boolean,
) {
fun newBuilder() = Builder()
.variables(variables)
.mergedDeferredFragmentIds(mergedDeferredFragmentIds)
.serializeVariablesWithDefaultBooleanValues(serializeVariablesWithDefaultBooleanValues)

fun variables(): Set<String> {
if (variables == null) {
Expand All @@ -34,6 +36,7 @@ class AdapterContext private constructor(
class Builder {
private var variables: Executable.Variables? = null
private var mergedDeferredFragmentIds: Set<DeferredFragmentIdentifier>? = null
private var serializeVariablesWithDefaultBooleanValues: Boolean? = null

fun variables(variables: Executable.Variables?) = apply {
this.variables = variables
Expand All @@ -43,8 +46,16 @@ class AdapterContext private constructor(
this.mergedDeferredFragmentIds = mergedDeferredFragmentIds
}

fun serializeVariablesWithDefaultBooleanValues(serializeVariablesWithDefaultBooleanValues: Boolean?) = apply {
this.serializeVariablesWithDefaultBooleanValues = serializeVariablesWithDefaultBooleanValues
}

fun build(): AdapterContext {
return AdapterContext(variables = variables, mergedDeferredFragmentIds = mergedDeferredFragmentIds)
return AdapterContext(
variables = variables,
mergedDeferredFragmentIds = mergedDeferredFragmentIds,
serializeVariablesWithDefaultBooleanValues = serializeVariablesWithDefaultBooleanValues == true
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.apollographql.apollo3.api

import com.apollographql.apollo3.api.Executable.Variables
import com.apollographql.apollo3.api.json.BufferedSinkJsonWriter
import com.apollographql.apollo3.api.json.MapJsonWriter
import com.apollographql.apollo3.api.json.JsonWriter
import okio.Buffer
import okio.IOException

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,45 @@

package com.apollographql.apollo3.api

import com.apollographql.apollo3.annotations.ApolloInternal
import com.apollographql.apollo3.api.json.BufferedSinkJsonWriter
import com.apollographql.apollo3.api.json.MapJsonWriter
import okio.Buffer
import kotlin.jvm.JvmName

@Suppress("UNCHECKED_CAST")
fun <D : Executable.Data> Executable<D>.variables(customScalarAdapters: CustomScalarAdapters): Executable.Variables {
val valueMap = MapJsonWriter().apply {
beginObject()
serializeVariables(this, customScalarAdapters)
endObject()
}.root() as Map<String, Any?>
return Executable.Variables(valueMap)
return variables(customScalarAdapters, false)
}

fun <D : Executable.Data> Executable<D>.variablesJson(customScalarAdapters: CustomScalarAdapters): String {
val buffer = Buffer()
BufferedSinkJsonWriter(buffer).apply {
beginObject()
serializeVariables(this, customScalarAdapters)
serializeVariables(this, customScalarAdapters.serializeVariablesWithDefaultBooleanValues())
endObject()
}
return buffer.readUtf8()
}

@Suppress("UNCHECKED_CAST")
@ApolloInternal
fun <D : Executable.Data> Executable<D>.variables(
customScalarAdapters: CustomScalarAdapters,
withDefaultBooleanValues: Boolean,
): Executable.Variables {
val valueMap = MapJsonWriter().apply {
beginObject()
serializeVariables(this, customScalarAdapters.let { if (withDefaultBooleanValues) it.serializeVariablesWithDefaultBooleanValues() else it })
endObject()
}.root() as Map<String, Any?>
return Executable.Variables(valueMap)
}

private fun CustomScalarAdapters.serializeVariablesWithDefaultBooleanValues() = newBuilder()
.adapterContext(
adapterContext.newBuilder()
.serializeVariablesWithDefaultBooleanValues(true)
.build()
)
.build()
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fun <D : Operation.Data> Operation<D>.parseJsonResponse(
jsonReader: JsonReader,
customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty,
): ApolloResponse<D> {
val variables = variables(customScalarAdapters)
val variables = variables(customScalarAdapters, withDefaultBooleanValues = true)
return ResponseParser.parse(
jsonReader,
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import com.apollographql.apollo3.compiler.codegen.java.JavaContext
import com.apollographql.apollo3.compiler.codegen.java.S
import com.apollographql.apollo3.compiler.codegen.java.T
import com.apollographql.apollo3.compiler.codegen.java.helpers.NamedType
import com.apollographql.apollo3.compiler.codegen.java.helpers.writeToResponseCodeBlock
import com.apollographql.apollo3.compiler.codegen.java.helpers.suppressDeprecatedAnnotation
import com.apollographql.apollo3.compiler.codegen.java.helpers.writeToResponseCodeBlock
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.ParameterizedTypeName
import com.squareup.javapoet.TypeName
Expand All @@ -27,13 +27,14 @@ internal fun List<NamedType>.inputAdapterTypeSpec(
context: JavaContext,
adapterName: String,
adaptedTypeName: TypeName,
withDefaultBooleanValues: Boolean,
): TypeSpec {
return TypeSpec.enumBuilder(adapterName)
.addModifiers(Modifier.PUBLIC)
.addEnumConstant("INSTANCE")
.addSuperinterface(ParameterizedTypeName.get(JavaClassNames.Adapter, adaptedTypeName))
.addMethod(notImplementedFromResponseMethodSpec(adaptedTypeName))
.addMethod(writeToResponseMethodSpec(context, adaptedTypeName))
.addMethod(writeToResponseMethodSpec(context, adaptedTypeName, withDefaultBooleanValues))
.apply {
if (this@inputAdapterTypeSpec.any { it.deprecationReason != null }) {
addAnnotation(suppressDeprecatedAnnotation())
Expand All @@ -56,6 +57,7 @@ private fun notImplementedFromResponseMethodSpec(adaptedTypeName: TypeName) = Me
private fun List<NamedType>.writeToResponseMethodSpec(
context: JavaContext,
adaptedTypeName: TypeName,
withDefaultBooleanValues: Boolean,
): MethodSpec {
return MethodSpec.methodBuilder(toJson)
.addModifiers(Modifier.PUBLIC)
Expand All @@ -64,7 +66,7 @@ private fun List<NamedType>.writeToResponseMethodSpec(
.addParameter(JavaClassNames.JsonWriter, writer)
.addParameter(JavaClassNames.CustomScalarAdapters, customScalarAdapters)
.addParameter(adaptedTypeName, value)
.addCode(writeToResponseCodeBlock(context))
.addCode(writeToResponseCodeBlock(context, withDefaultBooleanValues))
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ internal class FragmentVariablesAdapterBuilder(
.inputAdapterTypeSpec(
context = context,
adapterName = simpleName,
adaptedTypeName = context.resolver.resolveFragment(fragment.name)
adaptedTypeName = context.resolver.resolveFragment(fragment.name),
withDefaultBooleanValues = true,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ internal class InputObjectAdapterBuilder(
}.inputAdapterTypeSpec(
context,
simpleName,
context.resolver.resolveSchemaType(inputObject.name)
context.resolver.resolveSchemaType(inputObject.name),
withDefaultBooleanValues = false,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ internal class OperationVariablesAdapterBuilder(
.inputAdapterTypeSpec(
context = context,
adapterName = simpleName,
adaptedTypeName = context.resolver.resolveOperation(operation.name)
adaptedTypeName = context.resolver.resolveOperation(operation.name),
withDefaultBooleanValues = true,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import com.apollographql.apollo3.compiler.codegen.java.JavaContext
import com.apollographql.apollo3.compiler.codegen.java.L
import com.apollographql.apollo3.compiler.codegen.java.S
import com.apollographql.apollo3.compiler.codegen.java.T
import com.apollographql.apollo3.compiler.ir.IrBooleanValue
import com.apollographql.apollo3.compiler.ir.IrInputField
import com.apollographql.apollo3.compiler.ir.IrType
import com.apollographql.apollo3.compiler.ir.IrValue
import com.apollographql.apollo3.compiler.ir.IrVariable
import com.apollographql.apollo3.compiler.ir.isOptional
import com.squareup.javapoet.CodeBlock
Expand All @@ -22,6 +24,8 @@ internal class NamedType(
val description: String?,
val deprecationReason: String?,
val type: IrType,
// Relevant only for variables
val defaultValue: IrValue?,
)


Expand All @@ -42,25 +46,27 @@ internal fun IrInputField.toNamedType() = NamedType(
type = type,
description = description,
deprecationReason = deprecationReason,
defaultValue = null,
)

internal fun IrVariable.toNamedType() = NamedType(
graphQlName = name,
type = type,
description = null,
deprecationReason = null,
defaultValue = defaultValue,
)


internal fun List<NamedType>.writeToResponseCodeBlock(context: JavaContext): CodeBlock {
internal fun List<NamedType>.writeToResponseCodeBlock(context: JavaContext, withDefaultBooleanValues: Boolean): CodeBlock {
val builder = CodeBlock.builder()
forEach {
builder.add(it.writeToResponseCodeBlock(context))
builder.add(it.writeToResponseCodeBlock(context, withDefaultBooleanValues))
}
return builder.build()
}

internal fun NamedType.writeToResponseCodeBlock(context: JavaContext): CodeBlock {
internal fun NamedType.writeToResponseCodeBlock(context: JavaContext, withDefaultBooleanValues: Boolean): CodeBlock {
val adapterInitializer = context.resolver.adapterInitializer(type, false)
val builder = CodeBlock.builder()
val propertyName = context.layout.propertyName(graphQlName)
Expand All @@ -72,6 +78,12 @@ internal fun NamedType.writeToResponseCodeBlock(context: JavaContext): CodeBlock
builder.addStatement("$L.${Identifier.toJson}($writer, $customScalarAdapters, $value.$propertyName)", adapterInitializer)
if (type.isOptional()) {
builder.endControlFlow()
if (withDefaultBooleanValues && defaultValue is IrBooleanValue) {
builder.beginControlFlow("else if ($customScalarAdapters.getAdapterContext().getSerializeVariablesWithDefaultBooleanValues())")
builder.addStatement("$writer.name($S)", graphQlName)
builder.addStatement("$L.${Identifier.toJson}($writer, $customScalarAdapters, $L)", CodeBlock.of("$T.$L", JavaClassNames.Adapters, "BooleanAdapter"), defaultValue.value)
builder.endControlFlow()
}
}

return builder.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import com.apollographql.apollo3.compiler.codegen.Identifier.fromJson
import com.apollographql.apollo3.compiler.codegen.Identifier.toJson
import com.apollographql.apollo3.compiler.codegen.Identifier.value
import com.apollographql.apollo3.compiler.codegen.Identifier.writer
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.suppressDeprecationAnnotationSpec
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinContext
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSymbols
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.NamedType
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.requiresOptInAnnotation
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.suppressDeprecationAnnotationSpec
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.writeToResponseCodeBlock
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
Expand All @@ -26,11 +26,12 @@ internal fun List<NamedType>.inputAdapterTypeSpec(
context: KotlinContext,
adapterName: String,
adaptedTypeName: TypeName,
withDefaultBooleanValues: Boolean,
): TypeSpec {
return TypeSpec.objectBuilder(adapterName)
.addSuperinterface(KotlinSymbols.Adapter.parameterizedBy(adaptedTypeName))
.addFunction(notImplementedFromResponseFunSpec(adaptedTypeName))
.addFunction(writeToResponseFunSpec(context, adaptedTypeName))
.addFunction(writeToResponseFunSpec(context, adaptedTypeName, withDefaultBooleanValues))
.apply {
if (this@inputAdapterTypeSpec.any { it.deprecationReason != null }) {
addAnnotation(suppressDeprecationAnnotationSpec)
Expand All @@ -57,13 +58,14 @@ private fun notImplementedFromResponseFunSpec(adaptedTypeName: TypeName) = FunSp
private fun List<NamedType>.writeToResponseFunSpec(
context: KotlinContext,
adaptedTypeName: TypeName,
withDefaultBooleanValues: Boolean,
): FunSpec {
return FunSpec.builder(toJson)
.addModifiers(KModifier.OVERRIDE)
.addParameter(writer, KotlinSymbols.JsonWriter)
.addParameter(customScalarAdapters, KotlinSymbols.CustomScalarAdapters)
.addParameter(customScalarAdapters, KotlinSymbols.CustomScalarAdapters)
.addParameter(value, adaptedTypeName)
.addCode(writeToResponseCodeBlock(context))
.addCode(writeToResponseCodeBlock(context, withDefaultBooleanValues))
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.apollographql.apollo3.compiler.codegen.Identifier.serializeVariables
import com.apollographql.apollo3.compiler.codegen.Identifier.toJson
import com.apollographql.apollo3.compiler.codegen.Identifier.writer
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinContext
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinMemberNames
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinResolver
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSymbols
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.patchKotlinNativeOptionalArrayProperties
Expand All @@ -18,8 +17,6 @@ import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.LambdaTypeName
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeSpec
Expand Down Expand Up @@ -90,4 +87,3 @@ internal fun TypeSpec.maybeAddFilterNotNull(generateFilterNotNull: Boolean): Typ
}
return patchKotlinNativeOptionalArrayProperties()
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ internal class FragmentVariablesAdapterBuilder(
.inputAdapterTypeSpec(
context = context,
adapterName = simpleName,
adaptedTypeName = context.resolver.resolveFragment(fragment.name)
adaptedTypeName = context.resolver.resolveFragment(fragment.name),
withDefaultBooleanValues = true,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ internal class InputObjectAdapterBuilder(
}.inputAdapterTypeSpec(
context,
simpleName,
context.resolver.resolveSchemaType(inputObject.name)
context.resolver.resolveSchemaType(inputObject.name),
withDefaultBooleanValues = false,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.apollographql.apollo3.compiler.codegen.Identifier
import com.apollographql.apollo3.compiler.codegen.Identifier.OPERATION_DOCUMENT
import com.apollographql.apollo3.compiler.codegen.Identifier.OPERATION_ID
import com.apollographql.apollo3.compiler.codegen.Identifier.OPERATION_NAME
import com.apollographql.apollo3.compiler.codegen.Identifier.block
import com.apollographql.apollo3.compiler.codegen.Identifier.document
import com.apollographql.apollo3.compiler.codegen.Identifier.id
import com.apollographql.apollo3.compiler.codegen.Identifier.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ internal class OperationVariablesAdapterBuilder(
.inputAdapterTypeSpec(
context = context,
adapterName = simpleName,
adaptedTypeName = context.resolver.resolveOperation(operation.name)
adaptedTypeName = context.resolver.resolveOperation(operation.name),
withDefaultBooleanValues = true,
)
}
}
Loading

0 comments on commit 0677eca

Please sign in to comment.