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

Fix missing symbol for input type with default enum value #522

Merged
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.apollographql.apollo.compiler

import com.apollographql.apollo.compiler.ir.TypeDeclaration
import com.squareup.javapoet.*
import javax.lang.model.element.Modifier

class BuilderTypeSpecBuilder(
val targetObjectClassName: ClassName,
val fields: List<Pair<String, TypeName>>,
val fieldDefaultValues: Map<String, Any?>
val fieldDefaultValues: Map<String, Any?>,
val typeDeclarations: List<TypeDeclaration>
) {
fun build(): TypeSpec {
return TypeSpec.classBuilder(builderClassName)
Expand All @@ -25,9 +27,15 @@ class BuilderTypeSpecBuilder(
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(defaultValue?.let { CodeBlock.of("\$L", it) } ?: CodeBlock.of(""))
.initializer(initializerCode)
.build()
})

Expand Down Expand Up @@ -83,5 +91,8 @@ class BuilderTypeSpecBuilder(
} else {
this
}

private fun TypeName.isEnum(typeDeclarations: List<TypeDeclaration>) =
((this is ClassName) && typeDeclarations.count { it.kind == "EnumType" && it.name == simpleName() } > 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ class InputObjectTypeSpecBuilder(
val builderFields = fields.map { it.name.decapitalize() to it.javaTypeName(context) }
val builderFieldDefaultValues = fields.associate { it.name.decapitalize() to it.defaultValue }
return addMethod(BuilderTypeSpecBuilder.builderFactoryMethod())
.addType(BuilderTypeSpecBuilder(objectClassName, builderFields, builderFieldDefaultValues).build())
.addType(
BuilderTypeSpecBuilder(
targetObjectClassName = objectClassName,
fields = builderFields,
fieldDefaultValues = builderFieldDefaultValues,
typeDeclarations = context.typeDeclarations
).build()
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,24 @@ class OperationTypeSpecBuilder(
private fun TypeSpec.Builder.addBuilder(context: CodeGenerationContext): TypeSpec.Builder {
addMethod(BuilderTypeSpecBuilder.builderFactoryMethod())
if (operation.variables.isEmpty()) {
return BuilderTypeSpecBuilder(ClassName.get("", OPERATION_TYPE_NAME), emptyList(), emptyMap())
.let { addType(it.build()) }
return BuilderTypeSpecBuilder(
targetObjectClassName = ClassName.get("", OPERATION_TYPE_NAME),
fields = emptyList(),
fieldDefaultValues = emptyMap(),
typeDeclarations = context.typeDeclarations
).let { addType(it.build()) }
}
return operation.variables
.map { it.name.decapitalize() to it.type }
.map { it.first to JavaTypeResolver(context, context.typesPackage).resolve(it.second).unwrapOptionalType() }
.let { BuilderTypeSpecBuilder(ClassName.get("", OPERATION_TYPE_NAME), it, emptyMap()) }
.let {
BuilderTypeSpecBuilder(
targetObjectClassName = ClassName.get("", OPERATION_TYPE_NAME),
fields = it,
fieldDefaultValues = emptyMap(),
typeDeclarations = context.typeDeclarations
)
}
.let { addType(it.build()) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ class VariablesTypeSpecBuilder(
} else {
val builderFields = variables.map { it.name.decapitalize() to it.javaTypeName(context, context.typesPackage) }
return addMethod(BuilderTypeSpecBuilder.builderFactoryMethod())
.addType(BuilderTypeSpecBuilder(VARIABLES_TYPE_NAME, builderFields, emptyMap()).build())
.addType(
BuilderTypeSpecBuilder(
targetObjectClassName = VARIABLES_TYPE_NAME,
fields = builderFields,
fieldDefaultValues = emptyMap(),
typeDeclarations = context.typeDeclarations
).build()
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@
"name": "favoriteColor",
"description": "Favorite color, optional",
"type": "ColorInput!"
},
{
"name": "enumWithDefaultValue",
"description": "for test purpose only",
"type": "Episode",
"defaultValue": "JEDI"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ public final class ReviewInput {

private final @Nonnull ColorInput favoriteColor;

private final @Nullable Episode enumWithDefaultValue;

ReviewInput(int stars, @Nullable Integer nullableIntFieldWithDefaultValue,
@Nullable String commentary, @Nonnull ColorInput favoriteColor) {
@Nullable String commentary, @Nonnull ColorInput favoriteColor,
@Nullable Episode enumWithDefaultValue) {
this.stars = stars;
this.nullableIntFieldWithDefaultValue = nullableIntFieldWithDefaultValue;
this.commentary = commentary;
this.favoriteColor = favoriteColor;
this.enumWithDefaultValue = enumWithDefaultValue;
}

public int stars() {
Expand All @@ -41,6 +45,10 @@ public int stars() {
return this.favoriteColor;
}

public @Nullable Episode enumWithDefaultValue() {
return this.enumWithDefaultValue;
}

public static Builder builder() {
return new Builder();
}
Expand All @@ -54,6 +62,8 @@ public static final class Builder {

private @Nonnull ColorInput favoriteColor;

private @Nullable Episode enumWithDefaultValue = Episode.JEDI;

Builder() {
}

Expand All @@ -77,9 +87,14 @@ public Builder favoriteColor(@Nonnull ColorInput favoriteColor) {
return this;
}

public Builder enumWithDefaultValue(@Nullable Episode enumWithDefaultValue) {
this.enumWithDefaultValue = enumWithDefaultValue;
return this;
}

public ReviewInput build() {
if (favoriteColor == null) throw new IllegalStateException("favoriteColor can't be null");
return new ReviewInput(stars, nullableIntFieldWithDefaultValue, commentary, favoriteColor);
return new ReviewInput(stars, nullableIntFieldWithDefaultValue, commentary, favoriteColor, enumWithDefaultValue);
}
}
}
10 changes: 10 additions & 0 deletions apollo-compiler/src/test/graphql/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,16 @@
}
},
"defaultValue": null
},
{
"name": "enumWithDefaultValue",
"description": "for test purpose only",
"type": {
"kind": "ENUM",
"name": "Episode",
"ofType": null
},
"defaultValue": "JEDI"
}
],
"interfaces": null,
Expand Down