Skip to content

Commit

Permalink
Allow nullable & optional variables and variables with default values (
Browse files Browse the repository at this point in the history
…#2949)

* allow nullable non-required arguments and input fields

* also make variables optional if they have a default value
  • Loading branch information
martinbonnin authored Feb 25, 2021
1 parent c0be16a commit 8a1ea6f
Show file tree
Hide file tree
Showing 28 changed files with 403 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .idea/runConfigurations/Codegen.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ internal class AstBuilder private constructor(
deprecationReason = null,
type = fieldType,
description = "",
isRequired = !fieldType.nullable,
isRequired = !fieldType.nullable && !hasDefaultValue,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.squareup.kotlinpoet.asTypeName
internal fun CodeGenerationAst.InputField.asInputTypeName() = if (isRequired) {
type.asTypeName()
} else {
Input::class.asClassName().parameterizedBy(type.asTypeName().copy(nullable = false))
Input::class.asClassName().parameterizedBy(type.asTypeName())
}

internal fun CodeGenerationAst.InputField.toParameterSpec(): ParameterSpec {
Expand Down Expand Up @@ -66,12 +66,6 @@ fun notImplementedFromResponseFunSpec(returnTypeName: TypeName) = FunSpec.builde
.addCode("throw %T(%S)", ClassName("kotlin", "IllegalStateException"), "Input type used in output position")
.build()

private fun CodeGenerationAst.InputField.actualType() = if (isRequired) {
type.nonNullable()
} else {
type
}

internal fun List<CodeGenerationAst.InputField>.serializerTypeSpec(
packageName: String,
name: String,
Expand All @@ -91,7 +85,7 @@ internal fun List<CodeGenerationAst.InputField>.serializerTypeSpec(
)

map {
it.actualType()
it.type
}.distinct()
.forEach {
builder.addProperty(it.adapterPropertySpec())
Expand All @@ -108,11 +102,11 @@ internal fun List<CodeGenerationAst.InputField>.serializerTypeSpec(
if (!it.isRequired) {
beginControlFlow("if (value.%L is %T)", kotlinNameForVariable(it.name), Input.Present::class)
addStatement("writer.name(%S)", it.name)
addStatement("%L.toResponse(writer, value.%L.value)", kotlinNameForAdapterField(it.actualType()), kotlinNameForVariable(it.name))
addStatement("%L.toResponse(writer, value.%L.value)", kotlinNameForAdapterField(it.type), kotlinNameForVariable(it.name))
endControlFlow()
} else {
addStatement("writer.name(%S)", it.name)
addStatement("%L.toResponse(writer, value.%L)", kotlinNameForAdapterField(it.actualType()), kotlinNameForVariable(it.name))
addStatement("%L.toResponse(writer, value.%L)", kotlinNameForAdapterField(it.type), kotlinNameForVariable(it.name))
}
}
addStatement("writer.endObject()")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal data class BackendIr(
data class Variable(
val name: String,
val type: IntrospectionSchema.TypeRef,
val hasDefaultValue: Boolean
)

data class Field(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ internal class BackendIrBuilder constructor(
val variables = this.variables.map { variable ->
BackendIr.Variable(
name = variable.name,
type = variable.type.toSchemaType()
type = variable.type.toSchemaType(),
hasDefaultValue = variable.defaultValue != null
)
}

Expand Down Expand Up @@ -197,7 +198,8 @@ internal class BackendIrBuilder constructor(
variables = this.variables.map { variable ->
BackendIr.Variable(
name = variable.name,
type = variable.type.toSchemaType()
type = variable.type.toSchemaType(),
hasDefaultValue = false
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import kotlin.collections.List
"RemoveExplicitTypeArguments", "NestedLambdaShadowedImplicitParameter", "PropertyName",
"RemoveRedundantQualifierName")
data class TestQuery(
val operation: Input<String> = Input.Absent
val operation: Input<String?> = Input.Absent
) : Query<TestQuery.Data> {
override fun operationId(): String = OPERATION_ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import kotlin.collections.List
"RemoveExplicitTypeArguments", "NestedLambdaShadowedImplicitParameter", "PropertyName",
"RemoveRedundantQualifierName")
data class TestQuery(
val episode: Input<Episode> = Input.Absent,
val episode: Input<Episode?> = Input.Absent,
val stars: Int,
val greenValue: Double
) : Query<TestQuery.Data> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import kotlin.collections.List
"RemoveExplicitTypeArguments", "NestedLambdaShadowedImplicitParameter", "PropertyName",
"RemoveRedundantQualifierName")
data class TestQuery(
val episode: Input<Episode> = Input.Absent,
val episode: Input<Episode?> = Input.Absent,
val includeName: Boolean,
val friendsCount: Int,
val listOfListOfStringArgs: List<List<String?>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlin.String
import kotlin.collections.List

data class HeroDetailsImpl(
val friendsCount: Input<Int> = Input.Absent,
val friendsCount: Input<Int?> = Input.Absent,
val includeName: Boolean
) : Fragment<HeroDetailsImpl.Data> {
override fun adapter(responseAdapterCache: ResponseAdapterCache): ResponseAdapter<Data> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import kotlin.collections.List
"RemoveExplicitTypeArguments", "NestedLambdaShadowedImplicitParameter", "PropertyName",
"RemoveRedundantQualifierName")
data class TestQuery(
val episode: Input<Episode> = Input.Absent
val episode: Input<Episode?> = Input.Absent
) : Query<TestQuery.Data> {
override fun operationId(): String = OPERATION_ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import kotlin.collections.List
"RemoveRedundantQualifierName")
data class TestQueryWithAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryLongName(
val episodeAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryLongName:
Input<Episode> = Input.Absent
Input<Episode?> = Input.Absent
) :
Query<TestQueryWithAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryAVeryLongName.Data>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ data class ColorInput(
/**
* Green color
*/
val green: Input<Double> = Input.Absent,
val green: Input<Double?> = Input.Absent,
/**
* Blue color
*/
val blue: Input<Double> = Input.Absent,
/**
* for test purpose only
*/
val enumWithDefaultValue: Input<Episode> = Input.Absent,
val enumWithDefaultValue: Input<Episode?> = Input.Absent,
/**
* Circle ref to review input
*/
val reviewRefInput: Input<ReviewRefInput> = Input.Absent
val reviewRefInput: Input<ReviewRefInput?> = Input.Absent
) : InputType
Original file line number Diff line number Diff line change
Expand Up @@ -28,85 +28,85 @@ data class ReviewInput(
/**
* for test purpose only
*/
val nullableIntFieldWithDefaultValue: Input<Int> = Input.Absent,
val nullableIntFieldWithDefaultValue: Input<Int?> = Input.Absent,
/**
* Comment about the movie, optional
*/
val commentary: Input<String> = Input.Absent,
val commentary: Input<String?> = Input.Absent,
/**
* Favorite color, optional
*/
val favoriteColor: ColorInput,
/**
* for test purpose only
*/
val enumWithDefaultValue: Input<Episode> = Input.Absent,
val enumWithDefaultValue: Input<Episode?> = Input.Absent,
/**
* for test purpose only
*/
val nonNullableEnumWithDefaultValue: Input<Episode> = Input.Absent,
/**
* for test purpose only
*/
val nullableEnum: Input<Episode> = Input.Absent,
val nullableEnum: Input<Episode?> = Input.Absent,
/**
* for test purpose only
*/
val listOfCustomScalar: Input<List<Date?>> = Input.Absent,
val listOfCustomScalar: Input<List<Date?>?> = Input.Absent,
/**
* for test purpose only
*/
val customScalar: Input<Date> = Input.Absent,
val customScalar: Input<Date?> = Input.Absent,
/**
* for test purpose only
*/
val listOfEnums: Input<List<Episode?>> = Input.Absent,
val listOfEnums: Input<List<Episode?>?> = Input.Absent,
/**
* for test purpose only
*/
val listOfInt: Input<List<Int?>> = Input.Absent,
val listOfInt: Input<List<Int?>?> = Input.Absent,
/**
* for test purpose only
*/
val listOfString: Input<List<String?>> = Input.Absent,
val listOfString: Input<List<String?>?> = Input.Absent,
/**
* for test purpose only
*/
val listOfStringNonOptional: List<String?>,
/**
* for test purpose only
*/
val listOfInputTypes: Input<List<ColorInput?>> = Input.Absent,
val listOfInputTypes: Input<List<ColorInput?>?> = Input.Absent,
/**
* for test purpose only
*/
val booleanWithDefaultValue: Input<Boolean> = Input.Absent,
val booleanWithDefaultValue: Input<Boolean?> = Input.Absent,
/**
* for test purpose only
*/
val booleanNonOptional: Input<Boolean> = Input.Absent,
val booleanNonOptional: Input<Boolean?> = Input.Absent,
/**
* for test purpose only
*/
val listOfListOfString: Input<List<List<String>>> = Input.Absent,
val listOfListOfString: Input<List<List<String>>?> = Input.Absent,
/**
* for test purpose only
*/
val listOfListOfEnum: Input<List<List<Episode>>> = Input.Absent,
val listOfListOfEnum: Input<List<List<Episode>>?> = Input.Absent,
/**
* for test purpose only
*/
val listOfListOfCustom: Input<List<List<Date>>> = Input.Absent,
val listOfListOfCustom: Input<List<List<Date>>?> = Input.Absent,
/**
* for test purpose only
*/
val listOfListOfObject: Input<List<List<ColorInput>>> = Input.Absent,
val listOfListOfObject: Input<List<List<ColorInput>>?> = Input.Absent,
/**
* for test purpose only
*/
val capitalizedField: Input<String> = Input.Absent,
val capitalizedField: Input<String?> = Input.Absent,
/**
* for test purpose only
*/
val capitalizedInt: Input<Int> = Input.Absent
val capitalizedInt: Input<Int?> = Input.Absent
) : InputType
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ import kotlin.Suppress
"RemoveExplicitTypeArguments", "NestedLambdaShadowedImplicitParameter", "PropertyName",
"RemoveRedundantQualifierName")
data class ReviewRefInput(
val reviewInput: Input<ReviewInput> = Input.Absent
val reviewInput: Input<ReviewInput?> = Input.Absent
) : InputType
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ internal data class ColorInput(
/**
* Green color
*/
val green: Input<Double> = Input.Absent,
val green: Input<Double?> = Input.Absent,
/**
* Blue color
*/
val blue: Input<Double> = Input.Absent,
/**
* for test purpose only
*/
val enumWithDefaultValue: Input<Episode> = Input.Absent,
val enumWithDefaultValue: Input<Episode?> = Input.Absent,
/**
* Circle ref to review input
*/
val reviewRefInput: Input<ReviewRefInput> = Input.Absent
val reviewRefInput: Input<ReviewRefInput?> = Input.Absent
) : InputType
Loading

0 comments on commit 8a1ea6f

Please sign in to comment.