Skip to content

Commit

Permalink
Use layout in data builder names
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin committed Oct 10, 2024
1 parent f33d8bd commit 4a2a50d
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ internal class BuilderFactoryBuilder(
}

private fun IrObject.toObjectBuilderMethodSpec(): MethodSpec {
val builderClassName = ClassName.get(packageName, "${name.capitalizeFirstLetter()}Builder")
return MethodSpec.methodBuilder("build${name.capitalizeFirstLetter()}")
val builderClassName = ClassName.get(packageName, "${layout.schemaTypeName(name)}Builder")
return MethodSpec.methodBuilder("build${layout.schemaTypeName(name)}")
.addModifiers(Modifier.PUBLIC)
.returns(builderClassName)
.addStatement("return new $T($customScalarAdapters)", builderClassName)
.build()
}

private fun IrSchemaType.toUnknownBuilderMethodSpec(): MethodSpec {
val builderClassName = ClassName.get(packageName, "Other${name.capitalizeFirstLetter()}Builder")
return MethodSpec.methodBuilder("buildOther${name.capitalizeFirstLetter()}")
val builderClassName = ClassName.get(packageName, "Other${layout.schemaTypeName(name)}Builder")
return MethodSpec.methodBuilder("buildOther${layout.schemaTypeName(name)}")
.addModifiers(Modifier.PUBLIC)
.addParameter(JavaClassNames.String, Identifier.__typename)
.returns(builderClassName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ internal class InterfaceBuilderBuilder(
) : JavaClassBuilder {
private val layout = context.layout
private val packageName = layout.typeBuilderPackageName()
private val simpleName = "Other${iface.name.capitalizeFirstLetter()}Builder"
private val mapClassName = ClassName.get(packageName, "Other${iface.name.capitalizeFirstLetter()}Map")
private val simpleName = "Other${layout.schemaTypeName(iface.name)}Builder"
private val mapClassName = ClassName.get(packageName, "Other${layout.schemaTypeName(iface.name)}Map")

override fun prepare() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class InterfaceMapBuilder(
) : JavaClassBuilder {
private val layout = context.layout
private val packageName = layout.typeBuilderPackageName()
private val simpleName = "${iface.name.capitalizeFirstLetter()}Map"
private val simpleName = "${layout.schemaTypeName(iface.name)}Map"

override fun prepare() {
context.resolver.registerMapType(iface.name, ClassName.get(packageName, simpleName))
Expand All @@ -35,7 +35,7 @@ internal class InterfaceMapBuilder(
.addModifiers(Modifier.PUBLIC)
.addSuperinterfaces(
implements.map {
ClassName.get(packageName, "${it.capitalizeFirstLetter()}Map")
ClassName.get(packageName, "${layout.schemaTypeName(it)}Map")
}
)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class InterfaceUnknownMapBuilder(
) : JavaClassBuilder {
private val layout = context.layout
private val packageName = layout.typeBuilderPackageName()
private val simpleName = "Other${iface.name.capitalizeFirstLetter()}Map"
private val simpleName = "Other${layout.schemaTypeName(iface.name)}Map"

override fun prepare() {
context.resolver.registerMapType(iface.name, ClassName.get(packageName, simpleName))
Expand All @@ -39,10 +39,10 @@ internal class InterfaceUnknownMapBuilder(
.superclass(JavaClassNames.ObjectMap)
.addSuperinterfaces(
implements.map {
ClassName.get(packageName, "${it.capitalizeFirstLetter()}Map")
ClassName.get(packageName, "${layout.schemaTypeName(it)}Map")
}
)
.addSuperinterface(ClassName.get(packageName, "${iface.name.capitalizeFirstLetter()}Map"))
.addSuperinterface(ClassName.get(packageName, "${layout.schemaTypeName(iface.name)}Map"))
.addMethod(
MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ internal class ObjectBuilderBuilder(
) : JavaClassBuilder {
private val layout = context.layout
private val packageName = layout.typeBuilderPackageName()
private val simpleName = "${obj.name.capitalizeFirstLetter()}Builder"
private val mapClassName = ClassName.get(packageName, "${obj.name.capitalizeFirstLetter()}Map")
private val simpleName = "${layout.schemaTypeName(obj.name)}Builder"
private val mapClassName = ClassName.get(packageName, "${layout.schemaTypeName(obj.name)}Map")

override fun prepare() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class ObjectMapBuilder(
) : JavaClassBuilder {
private val layout = context.layout
private val packageName = layout.typeBuilderPackageName()
private val simpleName = "${obj.name.capitalizeFirstLetter()}Map"
private val simpleName = "${layout.schemaTypeName(obj.name)}Map"

override fun prepare() {
context.resolver.registerMapType(obj.name, ClassName.get(packageName, simpleName))
Expand All @@ -39,7 +39,7 @@ internal class ObjectMapBuilder(
.superclass(JavaClassNames.ObjectMap)
.addSuperinterfaces(
superTypes.map {
ClassName.get(packageName, "${it.capitalizeFirstLetter()}Map")
ClassName.get(packageName, "${layout.schemaTypeName(it)}Map")
}
)
.addMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ internal class UnionBuilderBuilder(
) : JavaClassBuilder {
private val layout = context.layout
private val packageName = layout.typeBuilderPackageName()
private val simpleName = "Other${union.name.capitalizeFirstLetter()}Builder"
private val mapClassName = ClassName.get(packageName, "Other${union.name.capitalizeFirstLetter()}Map")
private val simpleName = "Other${layout.schemaTypeName(union.name)}Builder"
private val mapClassName = ClassName.get(packageName, "Other${layout.schemaTypeName(union.name)}Map")

override fun prepare() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class UnionMapBuilder(
) : JavaClassBuilder {
private val layout = context.layout
private val packageName = layout.typeBuilderPackageName()
private val simpleName = "${union.name.capitalizeFirstLetter()}Map"
private val simpleName = "${layout.schemaTypeName(union.name)}Map"

override fun prepare() {
context.resolver.registerMapType(union.name, ClassName.get(packageName, simpleName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class UnionUnknownMapBuilder(
) : JavaClassBuilder {
private val layout = context.layout
private val packageName = layout.typeBuilderPackageName()
private val simpleName = "Other${union.name.capitalizeFirstLetter()}Map"
private val simpleName = "Other${layout.schemaTypeName(union.name)}Map"

override fun prepare() {
context.resolver.registerMapType(union.name, ClassName.get(packageName, simpleName))
Expand All @@ -37,7 +37,7 @@ internal class UnionUnknownMapBuilder(
.classBuilder(simpleName)
.addModifiers(Modifier.PUBLIC)
.superclass(JavaClassNames.ObjectMap)
.addSuperinterface(ClassName.get(packageName, "${union.name.capitalizeFirstLetter()}Map"))
.addSuperinterface(ClassName.get(packageName, "${layout.schemaTypeName(union.name)}Map"))
.addMethod(
MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ internal class InterfaceBuilder(
private val layout = context.layout
private val packageName = layout.typePackageName()
private val simpleName = layout.schemaTypeName(iface.name)
private val builderName = "${iface.name.capitalizeFirstLetter()}Builder"
private val otherBuilderName = "Other${iface.name.capitalizeFirstLetter()}Builder"
private val mapName = "${iface.name.capitalizeFirstLetter()}Map"
private val otherMapName = "Other${iface.name.capitalizeFirstLetter()}Map"
private val builderName = "${layout.schemaTypeName(iface.name)}Builder"
private val otherBuilderName = "Other${layout.schemaTypeName(iface.name)}Builder"
private val mapName = "${layout.schemaTypeName(iface.name)}Map"
private val otherMapName = "Other${layout.schemaTypeName(iface.name)}Map"

override fun prepare() {
context.resolver.registerSchemaType(iface.name, ClassName(packageName, simpleName))
Expand Down Expand Up @@ -74,7 +74,7 @@ internal class InterfaceBuilder(
if (generateDataBuilders) {
add(
topLevelBuildFunSpec(
"buildOther${iface.name.capitalizeFirstLetter()}",
"buildOther${layout.schemaTypeName(iface.name)}",
ClassName(packageName, otherBuilderName),
ClassName(packageName, otherMapName),
requiresTypename = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ internal class ObjectBuilder(
private val layout = context.layout
private val packageName = layout.typePackageName()
private val simpleName = layout.schemaTypeName(obj.name)
private val builderName = "${obj.name.capitalizeFirstLetter()}Builder"
private val mapName = "${obj.name.capitalizeFirstLetter()}Map"
private val builderName = "${layout.schemaTypeName(obj.name)}Builder"
private val mapName = "${layout.schemaTypeName(obj.name)}Map"

override fun prepare() {
context.resolver.registerSchemaType(obj.name, ClassName(packageName, simpleName))
context.resolver.registerMapType(obj.name, ClassName(packageName, mapName))
context.resolver.registerBuilderType(obj.name, ClassName(packageName, builderName))
context.resolver.registerBuilderFun(obj.name, MemberName(packageName, "build${obj.name.capitalizeFirstLetter()}"))
context.resolver.registerBuilderFun(obj.name, MemberName(packageName, "build${layout.schemaTypeName(obj.name)}"))
for (fieldDefinition in obj.fieldDefinitions) {
fieldDefinition.argumentDefinitions.forEach { argumentDefinition ->
context.resolver.registerArgumentDefinition(argumentDefinition.id, ClassName(packageName, simpleName))
Expand Down Expand Up @@ -73,7 +73,7 @@ internal class ObjectBuilder(

private fun IrObject.builderFunSpec(): FunSpec {
return topLevelBuildFunSpec(
"build${name.capitalizeFirstLetter()}",
"build${layout.schemaTypeName(name)}",
ClassName(packageName, builderName),
ClassName(packageName, mapName),
requiresTypename = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ internal class UnionBuilder(
private val layout = context.layout
private val packageName = layout.typePackageName()
private val simpleName = layout.schemaTypeName(union.name)
private val builderName = "${union.name.capitalizeFirstLetter()}Builder"
private val otherBuilderName = "Other${union.name.capitalizeFirstLetter()}Builder"
private val mapName = "${union.name.capitalizeFirstLetter()}Map"
private val otherMapName = "Other${union.name.capitalizeFirstLetter()}Map"
private val builderName = "${layout.schemaTypeName(union.name)}Builder"
private val otherBuilderName = "Other${layout.schemaTypeName(union.name)}Builder"
private val mapName = "${layout.schemaTypeName(union.name)}Map"
private val otherMapName = "Other${layout.schemaTypeName(union.name)}Map"

override fun prepare() {
context.resolver.registerSchemaType(union.name, ClassName(packageName, simpleName))
Expand Down Expand Up @@ -69,7 +69,7 @@ internal class UnionBuilder(
if (generateDataBuilders) {
add(
topLevelBuildFunSpec(
"buildOther${union.name.capitalizeFirstLetter()}",
"buildOther${layout.schemaTypeName(union.name)}",
ClassName(packageName, otherBuilderName),
ClassName(packageName, otherMapName),
requiresTypename = true
Expand Down
1 change: 1 addition & 0 deletions tests/data-builders-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ apollo {
mapScalar("Long1", "com.example.MyLong", "com.example.MyLongAdapter")
mapScalar("Long2", "com.example.MyLong")
languageVersion.set("1.5")
alwaysGenerateTypesMatching.set(listOf("info", "Info"))
}
}
11 changes: 10 additions & 1 deletion tests/data-builders-kotlin/src/main/graphql/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,13 @@ union Feline2 = Cat | Lion

scalar Long1
scalar Long2
scalar Long3
scalar Long3

# See https://github.com/apollographql/apollo-kotlin/issues/5537#issuecomment-2403695852
type Info {
foo: String
}

type info {
foo: String
}

0 comments on commit 4a2a50d

Please sign in to comment.