diff --git a/apollo-api/src/main/java/com/apollographql/apollo/api/Input.java b/apollo-api/src/main/java/com/apollographql/apollo/api/Input.java new file mode 100644 index 00000000000..b7f3a0d1eba --- /dev/null +++ b/apollo-api/src/main/java/com/apollographql/apollo/api/Input.java @@ -0,0 +1,21 @@ +package com.apollographql.apollo.api; + +import javax.annotation.Nullable; + +public final class Input { + public final V value; + public final boolean defined; + + private Input(V value, boolean defined) { + this.value = value; + this.defined = defined; + } + + public static Input fromNullable(@Nullable V value) { + return new Input<>(value, true); + } + + public static Input absent() { + return new Input<>(null, false); + } +} diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/BuilderTypeSpecBuilder.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/BuilderTypeSpecBuilder.kt index 9467d15ab31..a1543b48183 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/BuilderTypeSpecBuilder.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/BuilderTypeSpecBuilder.kt @@ -21,44 +21,47 @@ class BuilderTypeSpecBuilder( .build() } - private fun TypeSpec.Builder.addBuilderFields(): TypeSpec.Builder = - addFields(fields.map { - val fieldName = it.first - val fieldType = it.second - val defaultValue = fieldDefaultValues[fieldName]?.let { - (it as? Number)?.castTo(fieldType.withoutAnnotations()) ?: it - } - val initializerCode = defaultValue?.let { - if (fieldType.isEnum(typeDeclarations)) - CodeBlock.of("\$T.\$L", fieldType.withoutAnnotations(), defaultValue) - else - CodeBlock.of("\$L", defaultValue) - } ?: CodeBlock.of("") - FieldSpec.builder(fieldType, fieldName) - .addModifiers(Modifier.PRIVATE) - .initializer(initializerCode) - .build() - }) + private fun TypeSpec.Builder.addBuilderFields(): TypeSpec.Builder { + return addFields(fields.map { + val fieldName = it.first + val fieldType = it.second + val initializerCode = fieldDefaultValues[fieldName] + ?.let { (it as? Number)?.castTo(fieldType.unwrapOptionalType(true)) ?: it } + ?.let { defaultValue -> + if (fieldType.unwrapOptionalType(true).isEnum(typeDeclarations)) + CodeBlock.of("\$T.\$L", fieldType.unwrapOptionalType(true), defaultValue) + else + CodeBlock.of("\$L", defaultValue) + } + ?.let { fieldType.wrapOptionalValue(it) } + ?: fieldType.defaultOptionalValue() + FieldSpec.builder(fieldType, fieldName) + .addModifiers(Modifier.PRIVATE) + .initializer(initializerCode) + .build() + }) + } - private fun TypeSpec.Builder.addBuilderMethods(): TypeSpec.Builder = - addMethods(fields.map { - val fieldName = it.first - val fieldType = it.second - val javaDoc = fieldJavaDocs[fieldName] - MethodSpec.methodBuilder(fieldName) - .addModifiers(Modifier.PUBLIC) - .addParameter(ParameterSpec.builder(fieldType, fieldName).build()) - .let { - if (!javaDoc.isNullOrBlank()) - it.addJavadoc(CodeBlock.of("\$L\n", javaDoc)) - else - it - } - .returns(builderClass) - .addStatement("this.\$L = \$L", fieldName, fieldName) - .addStatement("return this") - .build() - }) + private fun TypeSpec.Builder.addBuilderMethods(): TypeSpec.Builder { + return addMethods(fields.map { + val fieldName = it.first + val fieldType = it.second + val javaDoc = fieldJavaDocs[fieldName] + MethodSpec.methodBuilder(fieldName) + .addModifiers(Modifier.PUBLIC) + .addParameter(ParameterSpec.builder(fieldType.unwrapOptionalType(), fieldName).build()) + .let { + if (!javaDoc.isNullOrBlank()) + it.addJavadoc(CodeBlock.of("\$L\n", javaDoc)) + else + it + } + .returns(builderClass) + .addStatement("this.\$L = \$L", fieldName, fieldType.wrapOptionalValue(CodeBlock.of("\$L", fieldName))) + .addStatement("return this") + .build() + }) + } private fun TypeSpec.Builder.addBuilderBuildMethod(): TypeSpec.Builder { val validationCodeBuilder = fields.filter { @@ -83,24 +86,27 @@ class BuilderTypeSpecBuilder( val CLASS_NAME: String = "Builder" private val builderClass = ClassName.get("", CLASS_NAME) - fun builderFactoryMethod(): MethodSpec = - MethodSpec - .methodBuilder("builder") - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .returns(builderClass) - .addStatement("return new \$T()", builderClass) - .build() + fun builderFactoryMethod(): MethodSpec { + return MethodSpec + .methodBuilder("builder") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .returns(builderClass) + .addStatement("return new \$T()", builderClass) + .build() + } - private fun Number.castTo(type: TypeName) = - if (type == TypeName.INT || type == TypeName.INT.box()) { - toInt() - } else if (type == TypeName.FLOAT || type == TypeName.FLOAT.box()) { - toDouble() - } else { - this - } + private fun Number.castTo(type: TypeName): Number { + return if (type == TypeName.INT || type == TypeName.INT.box()) { + toInt() + } else if (type == TypeName.FLOAT || type == TypeName.FLOAT.box()) { + toDouble() + } else { + this + } + } - private fun TypeName.isEnum(typeDeclarations: List) = - ((this is ClassName) && typeDeclarations.count { it.kind == "EnumType" && it.name == simpleName() } > 0) + private fun TypeName.isEnum(typeDeclarations: List): Boolean { + return ((this is ClassName) && typeDeclarations.count { it.kind == "EnumType" && it.name == simpleName() } > 0) + } } } diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ClassNames.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ClassNames.kt index b3856941170..eb475806e6f 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ClassNames.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ClassNames.kt @@ -1,9 +1,6 @@ package com.apollographql.apollo.compiler -import com.apollographql.apollo.api.GraphqlFragment -import com.apollographql.apollo.api.Mutation -import com.apollographql.apollo.api.Operation -import com.apollographql.apollo.api.Query +import com.apollographql.apollo.api.* import com.apollographql.apollo.api.internal.Optional import com.apollographql.apollo.api.internal.UnmodifiableMapBuilder import com.apollographql.apollo.api.internal.Utils @@ -29,6 +26,7 @@ object ClassNames { val JAVA_OPTIONAL: ClassName = ClassName.get("java.util", "Optional") val API_UTILS: ClassName = ClassName.get(Utils::class.java) val FRAGMENT: ClassName = ClassName.get(GraphqlFragment::class.java) + val INPUT_TYPE: ClassName = ClassName.get(Input::class.java) fun parameterizedListOf(type: Class): TypeName = ParameterizedTypeName.get(LIST, ClassName.get(type)) @@ -55,13 +53,13 @@ object ClassNames { fun parameterizedOptional(type: TypeName): TypeName = ParameterizedTypeName.get(OPTIONAL, type) - fun parameterizedGuavaOptional(type: Class): TypeName = - ParameterizedTypeName.get(GUAVA_OPTIONAL, ClassName.get(type)) - fun parameterizedGuavaOptional(type: TypeName): TypeName = ParameterizedTypeName.get(GUAVA_OPTIONAL, type) fun parameterizedJavaOptional(type: TypeName): TypeName = ParameterizedTypeName.get(JAVA_OPTIONAL, type) + fun parameterizedInputType(type: TypeName): TypeName = + ParameterizedTypeName.get(INPUT_TYPE, type) + } \ No newline at end of file diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/InputFieldSpec.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/InputFieldSpec.kt index f8a05750788..231d6ffb1af 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/InputFieldSpec.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/InputFieldSpec.kt @@ -28,11 +28,21 @@ class InputFieldSpec( Type.SCALAR_LIST -> writeScalarList(writerParam) Type.CUSTOM_LIST -> writeCustomList(writerParam) Type.OBJECT_LIST -> writeObjectList(writerParam, marshaller) + }.let { + if (javaType.isOptional()) { + CodeBlock.builder() + .beginControlFlow("if (\$L.defined)", name) + .add(it) + .endControlFlow() + .build() + } else { + it + } } } private fun writeScalarCode(writerParam: CodeBlock): CodeBlock { - val valueCode = javaType.unwrapOptionalValue(name) + val valueCode = javaType.unwrapOptionalValue(varName = name, checkIfPresent = false) return CodeBlock.of("\$L.\$L(\$S, \$L);\n", writerParam, WRITE_METHODS[type], name, valueCode) } @@ -59,7 +69,7 @@ class InputFieldSpec( } private fun writeScalarList(writerParam: CodeBlock): CodeBlock { - val rawFieldType = with(javaType) { if (isList()) listParamType() else this } + val rawFieldType = with(javaType.unwrapOptionalType(true)) { if (isList()) listParamType() else this } val writeMethod = SCALAR_LIST_ITEM_WRITE_METHODS[rawFieldType] ?: "writeString" val writeStatement = CodeBlock.builder() .beginControlFlow("for (\$T \$L : \$L)", rawFieldType, "\$item", @@ -90,7 +100,7 @@ class InputFieldSpec( } private fun writeCustomList(writerParam: CodeBlock): CodeBlock { - val rawFieldType = javaType.let { if (it.isList()) it.listParamType() else it } + val rawFieldType = with(javaType.unwrapOptionalType(true)) { if (isList()) listParamType() else this } val customScalarEnum = CustomEnumTypeSpecBuilder.className(context) val customScalarEnumConst = normalizeGraphQlType(graphQLType).toUpperCase(Locale.ENGLISH) val writeStatement = CodeBlock.builder() @@ -118,7 +128,7 @@ class InputFieldSpec( } private fun writeObjectList(writerParam: CodeBlock, marshaller: CodeBlock): CodeBlock { - val rawFieldType = with(javaType) { if (isList()) listParamType() else this } + val rawFieldType = with(javaType.unwrapOptionalType(true)) { if (isList()) listParamType() else this } val writeStatement = CodeBlock.builder() .beginControlFlow("for (\$T \$L : \$L)", rawFieldType, "\$item", javaType.unwrapOptionalValue(name, false)) @@ -170,10 +180,11 @@ class InputFieldSpec( private val LIST_ITEM_WRITER_PARAM = ParameterSpec.builder(InputFieldWriter.ListItemWriter::class.java, "listItemWriter").build() - fun build(name: String, graphQLType: String, context: CodeGenerationContext): InputFieldSpec { + fun build(name: String, graphQLType: String, context: CodeGenerationContext, + nullableValueType: NullableValueType = NullableValueType.INPUT_TYPE): InputFieldSpec { val javaType = JavaTypeResolver(context = context, packageName = "") - .resolve(typeName = graphQLType, nullableValueType = NullableValueType.ANNOTATED) - val normalizedJavaType = javaType.withoutAnnotations() + .resolve(typeName = graphQLType, nullableValueType = nullableValueType) + val normalizedJavaType = javaType.unwrapOptionalType(true) val type = when { normalizedJavaType.isList() -> { val rawFieldType = normalizedJavaType.let { if (it.isList()) it.listParamType() else it } diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/InputTypeSpecBuilder.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/InputTypeSpecBuilder.kt index b57b7a9474e..ba8ae14caaf 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/InputTypeSpecBuilder.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/InputTypeSpecBuilder.kt @@ -40,25 +40,6 @@ class InputTypeSpecBuilder( ) } - private fun TypeSpec.Builder.addFieldDefinition(field: TypeDeclarationField): TypeSpec.Builder = - addField(FieldSpec - .builder(field.javaTypeName(context), field.name.decapitalize()) - .addModifiers(Modifier.PRIVATE, Modifier.FINAL) - .build()) - - private fun TypeSpec.Builder.addFieldAccessor(field: TypeDeclarationField) = - addMethod(MethodSpec.methodBuilder(field.name.decapitalize()) - .addModifiers(Modifier.PUBLIC) - .returns(field.javaTypeName(context)) - .let { - if (!field.description.isNullOrBlank()) - it.addJavadoc(CodeBlock.of("\$L\n", field.description)) - else - it - } - .addStatement("return this.\$L", field.name.decapitalize()) - .build()) - private fun TypeSpec.Builder.addBuilder(): TypeSpec.Builder { if (fields.isEmpty()) { return this @@ -83,7 +64,13 @@ class InputTypeSpecBuilder( private fun marshallerMethodSpec(): MethodSpec { val writeCode = fields - .map { InputFieldSpec.build(name = it.name, graphQLType = it.type, context = context) } + .map { + InputFieldSpec.build( + name = it.name, + graphQLType = it.type, + context = context + ) + } .map { it.writeValueCode( writerParam = CodeBlock.of("\$L", WRITER_PARAM.name), @@ -111,17 +98,43 @@ class InputTypeSpecBuilder( } private fun TypeSpec.Builder.addFields(): TypeSpec.Builder { + fun addFieldDefinition(field: TypeDeclarationField) { + addField(FieldSpec + .builder(field.javaTypeName(context), field.name.decapitalize()) + .addModifiers(Modifier.PRIVATE, Modifier.FINAL) + .build()) + } + + fun addFieldAccessor(field: TypeDeclarationField) { + val optional = !field.type.endsWith("!") + addMethod(MethodSpec.methodBuilder(field.name.decapitalize()) + .addModifiers(Modifier.PUBLIC) + .returns(field.javaTypeName(context).unwrapOptionalType()) + .let { + if (!field.description.isNullOrBlank()) + it.addJavadoc(CodeBlock.of("\$L\n", field.description)) + else + it + } + .addStatement("return this.\$L\$L", field.name.decapitalize(), if (optional) ".value" else "") + .build()) + } + fields.forEach { field -> addFieldDefinition(field) addFieldAccessor(field) } + return this } + private fun TypeDeclarationField.javaTypeName(context: CodeGenerationContext): TypeName { + return JavaTypeResolver(context, context.typesPackage) + .resolve(typeName = type, nullableValueType = NullableValueType.INPUT_TYPE) + } + companion object { private val WRITER_PARAM = ParameterSpec.builder(InputFieldWriter::class.java, "writer").build() private const val MARSHALLER_PARAM_NAME = "marshaller" - private fun TypeDeclarationField.javaTypeName(context: CodeGenerationContext) = - JavaTypeResolver(context, context.typesPackage).resolve(type, !type.endsWith("!")).unwrapOptionalType() } } diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/JavaTypeResolver.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/JavaTypeResolver.kt index b0ae9d26198..14a45d3dc26 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/JavaTypeResolver.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/JavaTypeResolver.kt @@ -1,6 +1,7 @@ package com.apollographql.apollo.compiler import com.apollographql.apollo.compiler.ClassNames.parameterizedGuavaOptional +import com.apollographql.apollo.compiler.ClassNames.parameterizedInputType import com.apollographql.apollo.compiler.ClassNames.parameterizedJavaOptional import com.apollographql.apollo.compiler.ClassNames.parameterizedOptional import com.apollographql.apollo.compiler.ir.CodeGenerationContext @@ -35,6 +36,7 @@ class JavaTypeResolver( NullableValueType.APOLLO_OPTIONAL -> parameterizedOptional(javaType) NullableValueType.GUAVA_OPTIONAL -> parameterizedGuavaOptional(javaType) NullableValueType.JAVA_OPTIONAL -> parameterizedJavaOptional(javaType) + NullableValueType.INPUT_TYPE -> parameterizedInputType(javaType) else -> javaType.annotated(Annotations.NULLABLE) }.let { if (deprecated) it.annotated(Annotations.DEPRECATED) else it diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/NullableValueType.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/NullableValueType.kt index 1f9c0ba33d1..c565358b109 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/NullableValueType.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/NullableValueType.kt @@ -4,7 +4,8 @@ enum class NullableValueType(val value: String) { ANNOTATED("annotated"), APOLLO_OPTIONAL("apolloOptional"), GUAVA_OPTIONAL("guavaOptional"), - JAVA_OPTIONAL("javaOptional"); + JAVA_OPTIONAL("javaOptional"), + INPUT_TYPE("inputType"); companion object { fun findByValue(value: String): NullableValueType? = NullableValueType.values().find { it.value == value } diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/Util.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/Util.kt index d79e2cd45d0..fde499e3c16 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/Util.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/Util.kt @@ -241,6 +241,7 @@ fun TypeName.isNullable(): Boolean = isOptional() || annotations.contains(Annota fun TypeName.isOptional(): Boolean { val rawType = (this as? ParameterizedTypeName)?.rawType ?: this return rawType == ClassNames.OPTIONAL || rawType == ClassNames.GUAVA_OPTIONAL || rawType == ClassNames.JAVA_OPTIONAL + || rawType == ClassNames.INPUT_TYPE } fun TypeName.unwrapOptionalType(withoutAnnotations: Boolean = false): TypeName { @@ -251,25 +252,50 @@ fun TypeName.unwrapOptionalType(withoutAnnotations: Boolean = false): TypeName { }.let { if (withoutAnnotations) it.withoutAnnotations() else it } } -fun TypeName.unwrapOptionalValue(valueVarName: String, checkPresent: Boolean = true, +fun TypeName.unwrapOptionalValue(varName: String, checkIfPresent: Boolean = true, transformation: ((CodeBlock) -> CodeBlock)? = null): CodeBlock { return if (isOptional() && this is ParameterizedTypeName) { - val valueCode = CodeBlock.of("\$L.get()", valueVarName) - if (checkPresent) { - CodeBlock.of("\$L.isPresent() ? \$L : null", valueVarName, transformation?.invoke(valueCode) ?: valueCode) + if (rawType == ClassNames.INPUT_TYPE) { + val valueCode = CodeBlock.of("\$L.value", varName) + if (checkIfPresent) { + CodeBlock.of("\$L != null ? \$L : null", valueCode, transformation?.invoke(valueCode) ?: valueCode) + } else { + transformation?.invoke(valueCode) ?: valueCode + } } else { - transformation?.invoke(valueCode) ?: valueCode + val valueCode = CodeBlock.of("\$L.get()", varName) + if (checkIfPresent) { + CodeBlock.of("\$L.isPresent() ? \$L : null", varName, transformation?.invoke(valueCode) ?: valueCode) + } else { + transformation?.invoke(valueCode) ?: valueCode + } } } else { - val valueCode = CodeBlock.of("\$L", valueVarName) - if (annotations.contains(Annotations.NULLABLE) && checkPresent && transformation != null) { - CodeBlock.of("\$L != null ? \$L : null", valueVarName, transformation.invoke(valueCode)) + val valueCode = CodeBlock.of("\$L", varName) + if (annotations.contains(Annotations.NULLABLE) && checkIfPresent && transformation != null) { + CodeBlock.of("\$L != null ? \$L : null", varName, transformation.invoke(valueCode)) } else { transformation?.invoke(valueCode) ?: valueCode } } } +fun TypeName.wrapOptionalValue(value: CodeBlock): CodeBlock { + return if (this.isOptional() && this is ParameterizedTypeName) { + CodeBlock.of("\$T.fromNullable(\$L)", rawType, value) + } else { + value + } +} + +fun TypeName.defaultOptionalValue(): CodeBlock { + return if (this.isOptional() && this is ParameterizedTypeName) { + CodeBlock.of("\$T.absent()", rawType) + } else { + CodeBlock.of("") + } +} + fun TypeSpec.removeNestedTypeSpecs(excludeTypeNames: List): TypeSpec = TypeSpec.classBuilder(name) .superclass(superclass) diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/VariablesTypeSpecBuilder.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/VariablesTypeSpecBuilder.kt index f2f112bf308..a84d8c66a2e 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/VariablesTypeSpecBuilder.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/VariablesTypeSpecBuilder.kt @@ -100,11 +100,18 @@ class VariablesTypeSpecBuilder( private fun marshallerMethodSpec(): MethodSpec { val writeCode = variables - .map { InputFieldSpec.build(name = it.name, graphQLType = it.type, context = context) } + .map { + InputFieldSpec.build( + name = it.name, + graphQLType = it.type, + context = context, + nullableValueType = NullableValueType.ANNOTATED + ) + } .map { it.writeValueCode( writerParam = CodeBlock.of("\$L", WRITER_PARAM.name), - marshaller = CodeBlock.of("${MARSHALLER_PARAM_NAME}()") + marshaller = CodeBlock.of("\$L()", MARSHALLER_PARAM_NAME) ) } .fold(CodeBlock.builder(), CodeBlock.Builder::add) diff --git a/apollo-compiler/src/test/graphql/com/example/input_object_type/type/ColorInput.java b/apollo-compiler/src/test/graphql/com/example/input_object_type/type/ColorInput.java index af6a3122372..c212567c4d0 100644 --- a/apollo-compiler/src/test/graphql/com/example/input_object_type/type/ColorInput.java +++ b/apollo-compiler/src/test/graphql/com/example/input_object_type/type/ColorInput.java @@ -1,5 +1,6 @@ package com.example.input_object_type.type; +import com.apollographql.apollo.api.Input; import com.apollographql.apollo.api.InputFieldMarshaller; import com.apollographql.apollo.api.InputFieldWriter; import java.io.IOException; @@ -12,13 +13,13 @@ public final class ColorInput { private final int red; - private final @Nullable Double green; + private final Input green; private final double blue; - private final @Nullable Episode enumWithDefaultValue; + private final Input enumWithDefaultValue; - ColorInput(int red, @Nullable Double green, double blue, @Nullable Episode enumWithDefaultValue) { + ColorInput(int red, Input green, double blue, Input enumWithDefaultValue) { this.red = red; this.green = green; this.blue = blue; @@ -36,7 +37,7 @@ public int red() { * Green color */ public @Nullable Double green() { - return this.green; + return this.green.value; } /** @@ -50,7 +51,7 @@ public double blue() { * for test purpose only */ public @Nullable Episode enumWithDefaultValue() { - return this.enumWithDefaultValue; + return this.enumWithDefaultValue.value; } public static Builder builder() { @@ -62,9 +63,13 @@ public InputFieldMarshaller marshaller() { @Override public void marshal(InputFieldWriter writer) throws IOException { writer.writeInt("red", red); - writer.writeDouble("green", green); + if (green.defined) { + writer.writeDouble("green", green.value); + } writer.writeDouble("blue", blue); - writer.writeString("enumWithDefaultValue", enumWithDefaultValue != null ? enumWithDefaultValue.name() : null); + if (enumWithDefaultValue.defined) { + writer.writeString("enumWithDefaultValue", enumWithDefaultValue.value != null ? enumWithDefaultValue.value.name() : null); + } } }; } @@ -72,11 +77,11 @@ public void marshal(InputFieldWriter writer) throws IOException { public static final class Builder { private int red = 1; - private @Nullable Double green = 0.0; + private Input green = Input.fromNullable(0.0); private double blue = 1.5; - private @Nullable Episode enumWithDefaultValue = Episode.JEDI; + private Input enumWithDefaultValue = Input.fromNullable(Episode.JEDI); Builder() { } @@ -93,7 +98,7 @@ public Builder red(int red) { * Green color */ public Builder green(@Nullable Double green) { - this.green = green; + this.green = Input.fromNullable(green); return this; } @@ -109,7 +114,7 @@ public Builder blue(double blue) { * for test purpose only */ public Builder enumWithDefaultValue(@Nullable Episode enumWithDefaultValue) { - this.enumWithDefaultValue = enumWithDefaultValue; + this.enumWithDefaultValue = Input.fromNullable(enumWithDefaultValue); return this; } diff --git a/apollo-compiler/src/test/graphql/com/example/input_object_type/type/ReviewInput.java b/apollo-compiler/src/test/graphql/com/example/input_object_type/type/ReviewInput.java index 04764ef52db..50c275ffcfb 100644 --- a/apollo-compiler/src/test/graphql/com/example/input_object_type/type/ReviewInput.java +++ b/apollo-compiler/src/test/graphql/com/example/input_object_type/type/ReviewInput.java @@ -1,5 +1,6 @@ package com.example.input_object_type.type; +import com.apollographql.apollo.api.Input; import com.apollographql.apollo.api.InputFieldMarshaller; import com.apollographql.apollo.api.InputFieldWriter; import com.apollographql.apollo.api.internal.Utils; @@ -17,24 +18,24 @@ public final class ReviewInput { private final int stars; - private final @Nullable Integer nullableIntFieldWithDefaultValue; + private final Input nullableIntFieldWithDefaultValue; - private final @Nullable String commentary; + private final Input commentary; private final @Nonnull ColorInput favoriteColor; - private final @Nullable Episode enumWithDefaultValue; + private final Input enumWithDefaultValue; - private final @Nullable Episode nullableEnum; + private final Input nullableEnum; - private final @Nullable List listOfCustomScalar; + private final Input> listOfCustomScalar; - private final @Nullable List listOfEnums; + private final Input> listOfEnums; - ReviewInput(int stars, @Nullable Integer nullableIntFieldWithDefaultValue, - @Nullable String commentary, @Nonnull ColorInput favoriteColor, - @Nullable Episode enumWithDefaultValue, @Nullable Episode nullableEnum, - @Nullable List listOfCustomScalar, @Nullable List listOfEnums) { + ReviewInput(int stars, Input nullableIntFieldWithDefaultValue, Input commentary, + @Nonnull ColorInput favoriteColor, Input enumWithDefaultValue, + Input nullableEnum, Input> listOfCustomScalar, + Input> listOfEnums) { this.stars = stars; this.nullableIntFieldWithDefaultValue = nullableIntFieldWithDefaultValue; this.commentary = commentary; @@ -56,14 +57,14 @@ public int stars() { * for test purpose only */ public @Nullable Integer nullableIntFieldWithDefaultValue() { - return this.nullableIntFieldWithDefaultValue; + return this.nullableIntFieldWithDefaultValue.value; } /** * Comment about the movie, optional */ public @Nullable String commentary() { - return this.commentary; + return this.commentary.value; } /** @@ -77,28 +78,28 @@ public int stars() { * for test purpose only */ public @Nullable Episode enumWithDefaultValue() { - return this.enumWithDefaultValue; + return this.enumWithDefaultValue.value; } /** * for test purpose only */ public @Nullable Episode nullableEnum() { - return this.nullableEnum; + return this.nullableEnum.value; } /** * for test purpose only */ public @Nullable List listOfCustomScalar() { - return this.listOfCustomScalar; + return this.listOfCustomScalar.value; } /** * for test purpose only */ public @Nullable List listOfEnums() { - return this.listOfEnums; + return this.listOfEnums.value; } public static Builder builder() { @@ -110,27 +111,39 @@ public InputFieldMarshaller marshaller() { @Override public void marshal(InputFieldWriter writer) throws IOException { writer.writeInt("stars", stars); - writer.writeInt("nullableIntFieldWithDefaultValue", nullableIntFieldWithDefaultValue); - writer.writeString("commentary", commentary); + if (nullableIntFieldWithDefaultValue.defined) { + writer.writeInt("nullableIntFieldWithDefaultValue", nullableIntFieldWithDefaultValue.value); + } + if (commentary.defined) { + writer.writeString("commentary", commentary.value); + } writer.writeObject("favoriteColor", favoriteColor.marshaller()); - writer.writeString("enumWithDefaultValue", enumWithDefaultValue != null ? enumWithDefaultValue.name() : null); - writer.writeString("nullableEnum", nullableEnum != null ? nullableEnum.name() : null); - writer.writeList("listOfCustomScalar", listOfCustomScalar != null ? new InputFieldWriter.ListWriter() { - @Override - public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { - for (Date $item : listOfCustomScalar) { - listItemWriter.writeCustom(CustomType.DATE, $item); + if (enumWithDefaultValue.defined) { + writer.writeString("enumWithDefaultValue", enumWithDefaultValue.value != null ? enumWithDefaultValue.value.name() : null); + } + if (nullableEnum.defined) { + writer.writeString("nullableEnum", nullableEnum.value != null ? nullableEnum.value.name() : null); + } + if (listOfCustomScalar.defined) { + writer.writeList("listOfCustomScalar", listOfCustomScalar.value != null ? new InputFieldWriter.ListWriter() { + @Override + public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { + for (Date $item : listOfCustomScalar.value) { + listItemWriter.writeCustom(CustomType.DATE, $item); + } } - } - } : null); - writer.writeList("listOfEnums", listOfEnums != null ? new InputFieldWriter.ListWriter() { - @Override - public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { - for (Episode $item : listOfEnums) { - listItemWriter.writeString($item.name()); + } : null); + } + if (listOfEnums.defined) { + writer.writeList("listOfEnums", listOfEnums.value != null ? new InputFieldWriter.ListWriter() { + @Override + public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { + for (Episode $item : listOfEnums.value) { + listItemWriter.writeString($item.name()); + } } - } - } : null); + } : null); + } } }; } @@ -138,19 +151,19 @@ public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOExcep public static final class Builder { private int stars; - private @Nullable Integer nullableIntFieldWithDefaultValue = 10; + private Input nullableIntFieldWithDefaultValue = Input.fromNullable(10); - private @Nullable String commentary; + private Input commentary = Input.absent(); private @Nonnull ColorInput favoriteColor; - private @Nullable Episode enumWithDefaultValue = Episode.JEDI; + private Input enumWithDefaultValue = Input.fromNullable(Episode.JEDI); - private @Nullable Episode nullableEnum; + private Input nullableEnum = Input.absent(); - private @Nullable List listOfCustomScalar; + private Input> listOfCustomScalar = Input.absent(); - private @Nullable List listOfEnums; + private Input> listOfEnums = Input.absent(); Builder() { } @@ -167,7 +180,7 @@ public Builder stars(int stars) { * for test purpose only */ public Builder nullableIntFieldWithDefaultValue(@Nullable Integer nullableIntFieldWithDefaultValue) { - this.nullableIntFieldWithDefaultValue = nullableIntFieldWithDefaultValue; + this.nullableIntFieldWithDefaultValue = Input.fromNullable(nullableIntFieldWithDefaultValue); return this; } @@ -175,7 +188,7 @@ public Builder nullableIntFieldWithDefaultValue(@Nullable Integer nullableIntFie * Comment about the movie, optional */ public Builder commentary(@Nullable String commentary) { - this.commentary = commentary; + this.commentary = Input.fromNullable(commentary); return this; } @@ -191,7 +204,7 @@ public Builder favoriteColor(@Nonnull ColorInput favoriteColor) { * for test purpose only */ public Builder enumWithDefaultValue(@Nullable Episode enumWithDefaultValue) { - this.enumWithDefaultValue = enumWithDefaultValue; + this.enumWithDefaultValue = Input.fromNullable(enumWithDefaultValue); return this; } @@ -199,7 +212,7 @@ public Builder enumWithDefaultValue(@Nullable Episode enumWithDefaultValue) { * for test purpose only */ public Builder nullableEnum(@Nullable Episode nullableEnum) { - this.nullableEnum = nullableEnum; + this.nullableEnum = Input.fromNullable(nullableEnum); return this; } @@ -207,7 +220,7 @@ public Builder nullableEnum(@Nullable Episode nullableEnum) { * for test purpose only */ public Builder listOfCustomScalar(@Nullable List listOfCustomScalar) { - this.listOfCustomScalar = listOfCustomScalar; + this.listOfCustomScalar = Input.fromNullable(listOfCustomScalar); return this; } @@ -215,7 +228,7 @@ public Builder listOfCustomScalar(@Nullable List listOfCustomScalar) { * for test purpose only */ public Builder listOfEnums(@Nullable List listOfEnums) { - this.listOfEnums = listOfEnums; + this.listOfEnums = Input.fromNullable(listOfEnums); return this; } diff --git a/apollo-compiler/src/test/graphql/com/example/mutation_create_review/type/ColorInput.java b/apollo-compiler/src/test/graphql/com/example/mutation_create_review/type/ColorInput.java index f270c6bc875..aa75b193517 100644 --- a/apollo-compiler/src/test/graphql/com/example/mutation_create_review/type/ColorInput.java +++ b/apollo-compiler/src/test/graphql/com/example/mutation_create_review/type/ColorInput.java @@ -1,5 +1,6 @@ package com.example.mutation_create_review.type; +import com.apollographql.apollo.api.Input; import com.apollographql.apollo.api.InputFieldMarshaller; import com.apollographql.apollo.api.InputFieldWriter; import java.io.IOException; @@ -12,13 +13,13 @@ public final class ColorInput { private final int red; - private final @Nullable Double green; + private final Input green; private final double blue; - private final @Nullable Episode enumWithDefaultValue; + private final Input enumWithDefaultValue; - ColorInput(int red, @Nullable Double green, double blue, @Nullable Episode enumWithDefaultValue) { + ColorInput(int red, Input green, double blue, Input enumWithDefaultValue) { this.red = red; this.green = green; this.blue = blue; @@ -36,7 +37,7 @@ public int red() { * Green color */ public @Nullable Double green() { - return this.green; + return this.green.value; } /** @@ -50,7 +51,7 @@ public double blue() { * for test purpose only */ public @Nullable Episode enumWithDefaultValue() { - return this.enumWithDefaultValue; + return this.enumWithDefaultValue.value; } public static Builder builder() { @@ -62,9 +63,13 @@ public InputFieldMarshaller marshaller() { @Override public void marshal(InputFieldWriter writer) throws IOException { writer.writeInt("red", red); - writer.writeDouble("green", green); + if (green.defined) { + writer.writeDouble("green", green.value); + } writer.writeDouble("blue", blue); - writer.writeString("enumWithDefaultValue", enumWithDefaultValue != null ? enumWithDefaultValue.name() : null); + if (enumWithDefaultValue.defined) { + writer.writeString("enumWithDefaultValue", enumWithDefaultValue.value != null ? enumWithDefaultValue.value.name() : null); + } } }; } @@ -72,11 +77,11 @@ public void marshal(InputFieldWriter writer) throws IOException { public static final class Builder { private int red = 1; - private @Nullable Double green = 0.0; + private Input green = Input.fromNullable(0.0); private double blue = 1.5; - private @Nullable Episode enumWithDefaultValue = Episode.JEDI; + private Input enumWithDefaultValue = Input.fromNullable(Episode.JEDI); Builder() { } @@ -93,7 +98,7 @@ public Builder red(int red) { * Green color */ public Builder green(@Nullable Double green) { - this.green = green; + this.green = Input.fromNullable(green); return this; } @@ -109,7 +114,7 @@ public Builder blue(double blue) { * for test purpose only */ public Builder enumWithDefaultValue(@Nullable Episode enumWithDefaultValue) { - this.enumWithDefaultValue = enumWithDefaultValue; + this.enumWithDefaultValue = Input.fromNullable(enumWithDefaultValue); return this; } diff --git a/apollo-compiler/src/test/graphql/com/example/mutation_create_review/type/ReviewInput.java b/apollo-compiler/src/test/graphql/com/example/mutation_create_review/type/ReviewInput.java index fb48ad18c95..fa80391666d 100644 --- a/apollo-compiler/src/test/graphql/com/example/mutation_create_review/type/ReviewInput.java +++ b/apollo-compiler/src/test/graphql/com/example/mutation_create_review/type/ReviewInput.java @@ -1,5 +1,6 @@ package com.example.mutation_create_review.type; +import com.apollographql.apollo.api.Input; import com.apollographql.apollo.api.InputFieldMarshaller; import com.apollographql.apollo.api.InputFieldWriter; import com.apollographql.apollo.api.internal.Utils; @@ -17,24 +18,24 @@ public final class ReviewInput { private final int stars; - private final @Nullable Integer nullableIntFieldWithDefaultValue; + private final Input nullableIntFieldWithDefaultValue; - private final @Nullable String commentary; + private final Input commentary; private final @Nonnull ColorInput favoriteColor; - private final @Nullable Episode enumWithDefaultValue; + private final Input enumWithDefaultValue; - private final @Nullable Episode nullableEnum; + private final Input nullableEnum; - private final @Nullable List listOfCustomScalar; + private final Input> listOfCustomScalar; - private final @Nullable List listOfEnums; + private final Input> listOfEnums; - ReviewInput(int stars, @Nullable Integer nullableIntFieldWithDefaultValue, - @Nullable String commentary, @Nonnull ColorInput favoriteColor, - @Nullable Episode enumWithDefaultValue, @Nullable Episode nullableEnum, - @Nullable List listOfCustomScalar, @Nullable List listOfEnums) { + ReviewInput(int stars, Input nullableIntFieldWithDefaultValue, Input commentary, + @Nonnull ColorInput favoriteColor, Input enumWithDefaultValue, + Input nullableEnum, Input> listOfCustomScalar, + Input> listOfEnums) { this.stars = stars; this.nullableIntFieldWithDefaultValue = nullableIntFieldWithDefaultValue; this.commentary = commentary; @@ -56,14 +57,14 @@ public int stars() { * for test purpose only */ public @Nullable Integer nullableIntFieldWithDefaultValue() { - return this.nullableIntFieldWithDefaultValue; + return this.nullableIntFieldWithDefaultValue.value; } /** * Comment about the movie, optional */ public @Nullable String commentary() { - return this.commentary; + return this.commentary.value; } /** @@ -77,28 +78,28 @@ public int stars() { * for test purpose only */ public @Nullable Episode enumWithDefaultValue() { - return this.enumWithDefaultValue; + return this.enumWithDefaultValue.value; } /** * for test purpose only */ public @Nullable Episode nullableEnum() { - return this.nullableEnum; + return this.nullableEnum.value; } /** * for test purpose only */ public @Nullable List listOfCustomScalar() { - return this.listOfCustomScalar; + return this.listOfCustomScalar.value; } /** * for test purpose only */ public @Nullable List listOfEnums() { - return this.listOfEnums; + return this.listOfEnums.value; } public static Builder builder() { @@ -110,27 +111,39 @@ public InputFieldMarshaller marshaller() { @Override public void marshal(InputFieldWriter writer) throws IOException { writer.writeInt("stars", stars); - writer.writeInt("nullableIntFieldWithDefaultValue", nullableIntFieldWithDefaultValue); - writer.writeString("commentary", commentary); + if (nullableIntFieldWithDefaultValue.defined) { + writer.writeInt("nullableIntFieldWithDefaultValue", nullableIntFieldWithDefaultValue.value); + } + if (commentary.defined) { + writer.writeString("commentary", commentary.value); + } writer.writeObject("favoriteColor", favoriteColor.marshaller()); - writer.writeString("enumWithDefaultValue", enumWithDefaultValue != null ? enumWithDefaultValue.name() : null); - writer.writeString("nullableEnum", nullableEnum != null ? nullableEnum.name() : null); - writer.writeList("listOfCustomScalar", listOfCustomScalar != null ? new InputFieldWriter.ListWriter() { - @Override - public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { - for (Object $item : listOfCustomScalar) { - listItemWriter.writeCustom(CustomType.DATE, $item); + if (enumWithDefaultValue.defined) { + writer.writeString("enumWithDefaultValue", enumWithDefaultValue.value != null ? enumWithDefaultValue.value.name() : null); + } + if (nullableEnum.defined) { + writer.writeString("nullableEnum", nullableEnum.value != null ? nullableEnum.value.name() : null); + } + if (listOfCustomScalar.defined) { + writer.writeList("listOfCustomScalar", listOfCustomScalar.value != null ? new InputFieldWriter.ListWriter() { + @Override + public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { + for (Object $item : listOfCustomScalar.value) { + listItemWriter.writeCustom(CustomType.DATE, $item); + } } - } - } : null); - writer.writeList("listOfEnums", listOfEnums != null ? new InputFieldWriter.ListWriter() { - @Override - public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { - for (Episode $item : listOfEnums) { - listItemWriter.writeString($item.name()); + } : null); + } + if (listOfEnums.defined) { + writer.writeList("listOfEnums", listOfEnums.value != null ? new InputFieldWriter.ListWriter() { + @Override + public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { + for (Episode $item : listOfEnums.value) { + listItemWriter.writeString($item.name()); + } } - } - } : null); + } : null); + } } }; } @@ -138,19 +151,19 @@ public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOExcep public static final class Builder { private int stars; - private @Nullable Integer nullableIntFieldWithDefaultValue = 10; + private Input nullableIntFieldWithDefaultValue = Input.fromNullable(10); - private @Nullable String commentary; + private Input commentary = Input.absent(); private @Nonnull ColorInput favoriteColor; - private @Nullable Episode enumWithDefaultValue = Episode.JEDI; + private Input enumWithDefaultValue = Input.fromNullable(Episode.JEDI); - private @Nullable Episode nullableEnum; + private Input nullableEnum = Input.absent(); - private @Nullable List listOfCustomScalar; + private Input> listOfCustomScalar = Input.absent(); - private @Nullable List listOfEnums; + private Input> listOfEnums = Input.absent(); Builder() { } @@ -167,7 +180,7 @@ public Builder stars(int stars) { * for test purpose only */ public Builder nullableIntFieldWithDefaultValue(@Nullable Integer nullableIntFieldWithDefaultValue) { - this.nullableIntFieldWithDefaultValue = nullableIntFieldWithDefaultValue; + this.nullableIntFieldWithDefaultValue = Input.fromNullable(nullableIntFieldWithDefaultValue); return this; } @@ -175,7 +188,7 @@ public Builder nullableIntFieldWithDefaultValue(@Nullable Integer nullableIntFie * Comment about the movie, optional */ public Builder commentary(@Nullable String commentary) { - this.commentary = commentary; + this.commentary = Input.fromNullable(commentary); return this; } @@ -191,7 +204,7 @@ public Builder favoriteColor(@Nonnull ColorInput favoriteColor) { * for test purpose only */ public Builder enumWithDefaultValue(@Nullable Episode enumWithDefaultValue) { - this.enumWithDefaultValue = enumWithDefaultValue; + this.enumWithDefaultValue = Input.fromNullable(enumWithDefaultValue); return this; } @@ -199,7 +212,7 @@ public Builder enumWithDefaultValue(@Nullable Episode enumWithDefaultValue) { * for test purpose only */ public Builder nullableEnum(@Nullable Episode nullableEnum) { - this.nullableEnum = nullableEnum; + this.nullableEnum = Input.fromNullable(nullableEnum); return this; } @@ -207,7 +220,7 @@ public Builder nullableEnum(@Nullable Episode nullableEnum) { * for test purpose only */ public Builder listOfCustomScalar(@Nullable List listOfCustomScalar) { - this.listOfCustomScalar = listOfCustomScalar; + this.listOfCustomScalar = Input.fromNullable(listOfCustomScalar); return this; } @@ -215,7 +228,7 @@ public Builder listOfCustomScalar(@Nullable List listOfCustomScalar) { * for test purpose only */ public Builder listOfEnums(@Nullable List listOfEnums) { - this.listOfEnums = listOfEnums; + this.listOfEnums = Input.fromNullable(listOfEnums); return this; } diff --git a/apollo-compiler/src/test/graphql/com/example/mutation_create_review_semantic_naming/type/ColorInput.java b/apollo-compiler/src/test/graphql/com/example/mutation_create_review_semantic_naming/type/ColorInput.java index 95a5fb8bcb3..861ec5b8c75 100644 --- a/apollo-compiler/src/test/graphql/com/example/mutation_create_review_semantic_naming/type/ColorInput.java +++ b/apollo-compiler/src/test/graphql/com/example/mutation_create_review_semantic_naming/type/ColorInput.java @@ -1,5 +1,6 @@ package com.example.mutation_create_review_semantic_naming.type; +import com.apollographql.apollo.api.Input; import com.apollographql.apollo.api.InputFieldMarshaller; import com.apollographql.apollo.api.InputFieldWriter; import java.io.IOException; @@ -12,13 +13,13 @@ public final class ColorInput { private final int red; - private final @Nullable Double green; + private final Input green; private final double blue; - private final @Nullable Episode enumWithDefaultValue; + private final Input enumWithDefaultValue; - ColorInput(int red, @Nullable Double green, double blue, @Nullable Episode enumWithDefaultValue) { + ColorInput(int red, Input green, double blue, Input enumWithDefaultValue) { this.red = red; this.green = green; this.blue = blue; @@ -36,7 +37,7 @@ public int red() { * Green color */ public @Nullable Double green() { - return this.green; + return this.green.value; } /** @@ -50,7 +51,7 @@ public double blue() { * for test purpose only */ public @Nullable Episode enumWithDefaultValue() { - return this.enumWithDefaultValue; + return this.enumWithDefaultValue.value; } public static Builder builder() { @@ -62,9 +63,13 @@ public InputFieldMarshaller marshaller() { @Override public void marshal(InputFieldWriter writer) throws IOException { writer.writeInt("red", red); - writer.writeDouble("green", green); + if (green.defined) { + writer.writeDouble("green", green.value); + } writer.writeDouble("blue", blue); - writer.writeString("enumWithDefaultValue", enumWithDefaultValue != null ? enumWithDefaultValue.name() : null); + if (enumWithDefaultValue.defined) { + writer.writeString("enumWithDefaultValue", enumWithDefaultValue.value != null ? enumWithDefaultValue.value.name() : null); + } } }; } @@ -72,11 +77,11 @@ public void marshal(InputFieldWriter writer) throws IOException { public static final class Builder { private int red = 1; - private @Nullable Double green = 0.0; + private Input green = Input.fromNullable(0.0); private double blue = 1.5; - private @Nullable Episode enumWithDefaultValue = Episode.JEDI; + private Input enumWithDefaultValue = Input.fromNullable(Episode.JEDI); Builder() { } @@ -93,7 +98,7 @@ public Builder red(int red) { * Green color */ public Builder green(@Nullable Double green) { - this.green = green; + this.green = Input.fromNullable(green); return this; } @@ -109,7 +114,7 @@ public Builder blue(double blue) { * for test purpose only */ public Builder enumWithDefaultValue(@Nullable Episode enumWithDefaultValue) { - this.enumWithDefaultValue = enumWithDefaultValue; + this.enumWithDefaultValue = Input.fromNullable(enumWithDefaultValue); return this; } diff --git a/apollo-compiler/src/test/graphql/com/example/mutation_create_review_semantic_naming/type/ReviewInput.java b/apollo-compiler/src/test/graphql/com/example/mutation_create_review_semantic_naming/type/ReviewInput.java index e34881651b2..659e9df7484 100644 --- a/apollo-compiler/src/test/graphql/com/example/mutation_create_review_semantic_naming/type/ReviewInput.java +++ b/apollo-compiler/src/test/graphql/com/example/mutation_create_review_semantic_naming/type/ReviewInput.java @@ -1,5 +1,6 @@ package com.example.mutation_create_review_semantic_naming.type; +import com.apollographql.apollo.api.Input; import com.apollographql.apollo.api.InputFieldMarshaller; import com.apollographql.apollo.api.InputFieldWriter; import com.apollographql.apollo.api.internal.Utils; @@ -17,24 +18,24 @@ public final class ReviewInput { private final int stars; - private final @Nullable Integer nullableIntFieldWithDefaultValue; + private final Input nullableIntFieldWithDefaultValue; - private final @Nullable String commentary; + private final Input commentary; private final @Nonnull ColorInput favoriteColor; - private final @Nullable Episode enumWithDefaultValue; + private final Input enumWithDefaultValue; - private final @Nullable Episode nullableEnum; + private final Input nullableEnum; - private final @Nullable List listOfCustomScalar; + private final Input> listOfCustomScalar; - private final @Nullable List listOfEnums; + private final Input> listOfEnums; - ReviewInput(int stars, @Nullable Integer nullableIntFieldWithDefaultValue, - @Nullable String commentary, @Nonnull ColorInput favoriteColor, - @Nullable Episode enumWithDefaultValue, @Nullable Episode nullableEnum, - @Nullable List listOfCustomScalar, @Nullable List listOfEnums) { + ReviewInput(int stars, Input nullableIntFieldWithDefaultValue, Input commentary, + @Nonnull ColorInput favoriteColor, Input enumWithDefaultValue, + Input nullableEnum, Input> listOfCustomScalar, + Input> listOfEnums) { this.stars = stars; this.nullableIntFieldWithDefaultValue = nullableIntFieldWithDefaultValue; this.commentary = commentary; @@ -56,14 +57,14 @@ public int stars() { * for test purpose only */ public @Nullable Integer nullableIntFieldWithDefaultValue() { - return this.nullableIntFieldWithDefaultValue; + return this.nullableIntFieldWithDefaultValue.value; } /** * Comment about the movie, optional */ public @Nullable String commentary() { - return this.commentary; + return this.commentary.value; } /** @@ -77,28 +78,28 @@ public int stars() { * for test purpose only */ public @Nullable Episode enumWithDefaultValue() { - return this.enumWithDefaultValue; + return this.enumWithDefaultValue.value; } /** * for test purpose only */ public @Nullable Episode nullableEnum() { - return this.nullableEnum; + return this.nullableEnum.value; } /** * for test purpose only */ public @Nullable List listOfCustomScalar() { - return this.listOfCustomScalar; + return this.listOfCustomScalar.value; } /** * for test purpose only */ public @Nullable List listOfEnums() { - return this.listOfEnums; + return this.listOfEnums.value; } public static Builder builder() { @@ -110,27 +111,39 @@ public InputFieldMarshaller marshaller() { @Override public void marshal(InputFieldWriter writer) throws IOException { writer.writeInt("stars", stars); - writer.writeInt("nullableIntFieldWithDefaultValue", nullableIntFieldWithDefaultValue); - writer.writeString("commentary", commentary); + if (nullableIntFieldWithDefaultValue.defined) { + writer.writeInt("nullableIntFieldWithDefaultValue", nullableIntFieldWithDefaultValue.value); + } + if (commentary.defined) { + writer.writeString("commentary", commentary.value); + } writer.writeObject("favoriteColor", favoriteColor.marshaller()); - writer.writeString("enumWithDefaultValue", enumWithDefaultValue != null ? enumWithDefaultValue.name() : null); - writer.writeString("nullableEnum", nullableEnum != null ? nullableEnum.name() : null); - writer.writeList("listOfCustomScalar", listOfCustomScalar != null ? new InputFieldWriter.ListWriter() { - @Override - public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { - for (Object $item : listOfCustomScalar) { - listItemWriter.writeCustom(CustomType.DATE, $item); + if (enumWithDefaultValue.defined) { + writer.writeString("enumWithDefaultValue", enumWithDefaultValue.value != null ? enumWithDefaultValue.value.name() : null); + } + if (nullableEnum.defined) { + writer.writeString("nullableEnum", nullableEnum.value != null ? nullableEnum.value.name() : null); + } + if (listOfCustomScalar.defined) { + writer.writeList("listOfCustomScalar", listOfCustomScalar.value != null ? new InputFieldWriter.ListWriter() { + @Override + public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { + for (Object $item : listOfCustomScalar.value) { + listItemWriter.writeCustom(CustomType.DATE, $item); + } } - } - } : null); - writer.writeList("listOfEnums", listOfEnums != null ? new InputFieldWriter.ListWriter() { - @Override - public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { - for (Episode $item : listOfEnums) { - listItemWriter.writeString($item.name()); + } : null); + } + if (listOfEnums.defined) { + writer.writeList("listOfEnums", listOfEnums.value != null ? new InputFieldWriter.ListWriter() { + @Override + public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException { + for (Episode $item : listOfEnums.value) { + listItemWriter.writeString($item.name()); + } } - } - } : null); + } : null); + } } }; } @@ -138,19 +151,19 @@ public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOExcep public static final class Builder { private int stars; - private @Nullable Integer nullableIntFieldWithDefaultValue = 10; + private Input nullableIntFieldWithDefaultValue = Input.fromNullable(10); - private @Nullable String commentary; + private Input commentary = Input.absent(); private @Nonnull ColorInput favoriteColor; - private @Nullable Episode enumWithDefaultValue = Episode.JEDI; + private Input enumWithDefaultValue = Input.fromNullable(Episode.JEDI); - private @Nullable Episode nullableEnum; + private Input nullableEnum = Input.absent(); - private @Nullable List listOfCustomScalar; + private Input> listOfCustomScalar = Input.absent(); - private @Nullable List listOfEnums; + private Input> listOfEnums = Input.absent(); Builder() { } @@ -167,7 +180,7 @@ public Builder stars(int stars) { * for test purpose only */ public Builder nullableIntFieldWithDefaultValue(@Nullable Integer nullableIntFieldWithDefaultValue) { - this.nullableIntFieldWithDefaultValue = nullableIntFieldWithDefaultValue; + this.nullableIntFieldWithDefaultValue = Input.fromNullable(nullableIntFieldWithDefaultValue); return this; } @@ -175,7 +188,7 @@ public Builder nullableIntFieldWithDefaultValue(@Nullable Integer nullableIntFie * Comment about the movie, optional */ public Builder commentary(@Nullable String commentary) { - this.commentary = commentary; + this.commentary = Input.fromNullable(commentary); return this; } @@ -191,7 +204,7 @@ public Builder favoriteColor(@Nonnull ColorInput favoriteColor) { * for test purpose only */ public Builder enumWithDefaultValue(@Nullable Episode enumWithDefaultValue) { - this.enumWithDefaultValue = enumWithDefaultValue; + this.enumWithDefaultValue = Input.fromNullable(enumWithDefaultValue); return this; } @@ -199,7 +212,7 @@ public Builder enumWithDefaultValue(@Nullable Episode enumWithDefaultValue) { * for test purpose only */ public Builder nullableEnum(@Nullable Episode nullableEnum) { - this.nullableEnum = nullableEnum; + this.nullableEnum = Input.fromNullable(nullableEnum); return this; } @@ -207,7 +220,7 @@ public Builder nullableEnum(@Nullable Episode nullableEnum) { * for test purpose only */ public Builder listOfCustomScalar(@Nullable List listOfCustomScalar) { - this.listOfCustomScalar = listOfCustomScalar; + this.listOfCustomScalar = Input.fromNullable(listOfCustomScalar); return this; } @@ -215,7 +228,7 @@ public Builder listOfCustomScalar(@Nullable List listOfCustomScalar) { * for test purpose only */ public Builder listOfEnums(@Nullable List listOfEnums) { - this.listOfEnums = listOfEnums; + this.listOfEnums = Input.fromNullable(listOfEnums); return this; }