Skip to content

Commit

Permalink
Add JavaDoc generation for input types (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
sav007 authored Jun 15, 2017
1 parent 59f7901 commit d254b21
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class BuilderTypeSpecBuilder(
val targetObjectClassName: ClassName,
val fields: List<Pair<String, TypeName>>,
val fieldDefaultValues: Map<String, Any?>,
val fieldJavaDocs: Map<String, String>,
val typeDeclarations: List<TypeDeclaration>
) {
fun build(): TypeSpec {
Expand Down Expand Up @@ -43,9 +44,16 @@ class BuilderTypeSpecBuilder(
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(builderClassName)
.addStatement("this.\$L = \$L", fieldName, fieldName)
.addStatement("return this")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class InputObjectTypeSpecBuilder(
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())

Expand All @@ -55,12 +61,16 @@ class InputObjectTypeSpecBuilder(
} else {
val builderFields = fields.map { it.name.decapitalize() to it.javaTypeName(context) }
val builderFieldDefaultValues = fields.associate { it.name.decapitalize() to it.defaultValue }
val javaDocs = fields
.filter { !it.description.isNullOrBlank() }
.associate { it.name.decapitalize() to it.description }
return addMethod(BuilderTypeSpecBuilder.builderFactoryMethod())
.addType(
BuilderTypeSpecBuilder(
targetObjectClassName = objectClassName,
fields = builderFields,
fieldDefaultValues = builderFieldDefaultValues,
fieldJavaDocs = javaDocs,
typeDeclarations = context.typeDeclarations
).build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class OperationTypeSpecBuilder(
targetObjectClassName = ClassName.get("", OPERATION_TYPE_NAME),
fields = emptyList(),
fieldDefaultValues = emptyMap(),
fieldJavaDocs = emptyMap(),
typeDeclarations = context.typeDeclarations
).let { addType(it.build()) }
}
Expand All @@ -185,6 +186,7 @@ class OperationTypeSpecBuilder(
targetObjectClassName = ClassName.get("", OPERATION_TYPE_NAME),
fields = it,
fieldDefaultValues = emptyMap(),
fieldJavaDocs = emptyMap(),
typeDeclarations = context.typeDeclarations
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class VariablesTypeSpecBuilder(
targetObjectClassName = VARIABLES_TYPE_NAME,
fields = builderFields,
fieldDefaultValues = emptyMap(),
fieldJavaDocs = emptyMap(),
typeDeclarations = context.typeDeclarations
).build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,19 @@
"fields": [
{
"name": "red",
"description": "",
"description": "Red color",
"type": "Int!",
"defaultValue": 1
},
{
"name": "green",
"description": "",
"description": "Green color",
"type": "Float",
"defaultValue": 0
},
{
"name": "blue",
"description": "",
"description": "Blue color",
"type": "Float!",
"defaultValue": 1.5
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ public final class ColorInput {
this.blue = blue;
}

/**
* Red color
*/
public int red() {
return this.red;
}

/**
* Green color
*/
public @Nullable Double green() {
return this.green;
}

/**
* Blue color
*/
public double blue() {
return this.blue;
}
Expand All @@ -44,16 +53,25 @@ public static final class Builder {
Builder() {
}

/**
* Red color
*/
public Builder red(int red) {
this.red = red;
return this;
}

/**
* Green color
*/
public Builder green(@Nullable Double green) {
this.green = green;
return this;
}

/**
* Blue color
*/
public Builder blue(double blue) {
this.blue = blue;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,37 @@ public final class ReviewInput {
this.enumWithDefaultValue = enumWithDefaultValue;
}

/**
* 0-5 stars
*/
public int stars() {
return this.stars;
}

/**
* for test purpose only
*/
public @Nullable Integer nullableIntFieldWithDefaultValue() {
return this.nullableIntFieldWithDefaultValue;
}

/**
* Comment about the movie, optional
*/
public @Nullable String commentary() {
return this.commentary;
}

/**
* Favorite color, optional
*/
public @Nonnull ColorInput favoriteColor() {
return this.favoriteColor;
}

/**
* for test purpose only
*/
public @Nullable Episode enumWithDefaultValue() {
return this.enumWithDefaultValue;
}
Expand All @@ -67,26 +82,41 @@ public static final class Builder {
Builder() {
}

/**
* 0-5 stars
*/
public Builder stars(int stars) {
this.stars = stars;
return this;
}

/**
* for test purpose only
*/
public Builder nullableIntFieldWithDefaultValue(@Nullable Integer nullableIntFieldWithDefaultValue) {
this.nullableIntFieldWithDefaultValue = nullableIntFieldWithDefaultValue;
return this;
}

/**
* Comment about the movie, optional
*/
public Builder commentary(@Nullable String commentary) {
this.commentary = commentary;
return this;
}

/**
* Favorite color, optional
*/
public Builder favoriteColor(@Nonnull ColorInput favoriteColor) {
this.favoriteColor = favoriteColor;
return this;
}

/**
* for test purpose only
*/
public Builder enumWithDefaultValue(@Nullable Episode enumWithDefaultValue) {
this.enumWithDefaultValue = enumWithDefaultValue;
return this;
Expand Down
6 changes: 3 additions & 3 deletions apollo-compiler/src/test/graphql/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1667,7 +1667,7 @@
"inputFields": [
{
"name": "red",
"description": "",
"description": "Red color",
"type": {
"kind": "NON_NULL",
"name": null,
Expand All @@ -1681,7 +1681,7 @@
},
{
"name": "green",
"description": "",
"description": "Green color",
"type": {
"kind": "SCALAR",
"name": "Float",
Expand All @@ -1691,7 +1691,7 @@
},
{
"name": "blue",
"description": "",
"description": "Blue color",
"type": {
"kind": "NON_NULL",
"name": null,
Expand Down

0 comments on commit d254b21

Please sign in to comment.