@@ -18,22 +18,24 @@ internal class GeneratedMapper(
1818 private val options : CodegenOptions
1919) {
2020 /* *
21- * Will return the [ClassName] for the given [graphQLType].
21+ * Will build the generated [ClassName] for the given [GraphQLType]. This will only build [ClassName]s which
22+ * are generated through this code generator.
2223 *
2324 * @throws IllegalArgumentException If no name can be generated for the given [graphQLType]
2425 */
25- fun getGeneratedTypeClassName (graphQLType : GraphQLType , appendCompanion : Boolean = false ): ClassName {
26+ fun getGeneratedTypeClassName (graphQLType : GraphQLType ): ClassName {
2627 val name = NamingHelper .uppercaseFirstLetter(graphQLType.name)
2728 return when (graphQLType) {
28- is GraphQLEnumType -> buildClassName(name, appendCompanion, " enum" )
29- is GraphQLInputObjectType -> buildClassName(name, appendCompanion, " inputObject" )
30- is GraphQLObjectType -> buildClassName(name, appendCompanion, " object" )
29+ is GraphQLEnumType -> buildClassName(name, " enum" )
30+ is GraphQLInputObjectType -> buildClassName(name, " inputObject" )
31+ is GraphQLObjectType -> buildClassName(name, " object" )
3132 else -> throw IllegalArgumentException (" Unable to build name for ${graphQLType.name} " )
3233 }
3334 }
3435
3536 /* *
36- * Will return the [ClassName] for the given [field] in the [container].
37+ * Will return the [ClassName] for a resolver which is defined through the given [field], which is within the
38+ * given [container]. The [container] is necessary to build a unique identifier for the resolver.
3739 */
3840 fun getGeneratedFieldResolverClassName (
3941 container : GraphQLFieldsContainer ,
@@ -43,92 +45,68 @@ internal class GeneratedMapper(
4345 val containerName = NamingHelper .uppercaseFirstLetter(container.name)
4446 val fieldName = NamingHelper .uppercaseFirstLetter(field.name)
4547
46- return buildClassName(containerName + fieldName, false , " resolver" )
48+ return buildClassName(" $ containerName$ fieldName" , " resolver" )
4749 }
4850
4951 /* *
5052 * Will return the [ClassName] for the value wrapper.
5153 */
5254 fun getValueWrapperName (): ClassName =
53- buildClassName(" V" , false , " util" )
55+ buildClassName(" V" , " util" )
5456
5557 /* *
5658 * Will return the [ClassName] for the environment wrapper.
5759 */
5860 fun getEnvironmentWrapperClassName (): ClassName =
59- buildClassName(" Env" , false , " util" )
61+ buildClassName(" Env" , " util" )
6062
6163 /* *
6264 * Will return the [MemberName] of the builder method for the given [inputObject].
6365 */
6466 fun getInputObjectBuilderMemberName (inputObject : GraphQLInputObjectType ): MemberName =
65- MemberName (getGeneratedTypeClassName(inputObject, true ), " buildByMap" )
67+ MemberName (getGeneratedTypeClassName(inputObject).addSimpleNames( " Companion " ), " buildByMap" )
6668
6769 /* *
6870 * Will return the [MemberName] which points to a string which contains the name of the container for the given field resolver.
6971 */
7072 fun getFieldResolverContainerMemberName (
7173 container : GraphQLFieldsContainer ,
7274 field : GraphQLFieldDefinition
73- ): MemberName {
74- val resolver = getGeneratedFieldResolverClassName(container, field)
75-
76- return MemberName (
77- ClassName (resolver.packageName, * resolver.simpleNames.toTypedArray(), " Meta" ),
78- " CONTAINER"
79- )
80- }
75+ ): MemberName =
76+ MemberName (getGeneratedFieldResolverClassName(container, field).addSimpleNames(" Meta" ), " CONTAINER" )
8177
8278 /* *
8379 * Will return the [MemberName] which points to a string which contains the name of the field for the given field resolver.
8480 */
8581 fun getFieldResolverFieldMemberName (
8682 container : GraphQLFieldsContainer ,
8783 field : GraphQLFieldDefinition
88- ): MemberName {
89- val resolver = getGeneratedFieldResolverClassName(container, field)
90-
91- return MemberName (
92- ClassName (resolver.packageName, * resolver.simpleNames.toTypedArray(), " Meta" ),
93- " FIELD"
94- )
95- }
84+ ): MemberName =
85+ MemberName (getGeneratedFieldResolverClassName(container, field).addSimpleNames(" Meta" ), " FIELD" )
9686
9787 /* *
9888 * Will return the [ClassName] which points to the Environment class for the given resolver.
9989 */
100- fun getFieldResolverEnvironment (container : GraphQLFieldsContainer , field : GraphQLFieldDefinition ): ClassName {
101- val resolver = getGeneratedFieldResolverClassName(container, field)
102-
103- return ClassName (resolver.packageName, * resolver.simpleNames.toTypedArray(), " Env" )
104- }
90+ fun getFieldResolverEnvironment (container : GraphQLFieldsContainer , field : GraphQLFieldDefinition ): ClassName =
91+ getGeneratedFieldResolverClassName(container, field).addSimpleNames(" Env" )
10592
10693 fun getPaginationInfoClassName (): ClassName {
107- return buildClassName(" PaginationInfo" , false , " pagination" )
94+ return buildClassName(" PaginationInfo" , " pagination" )
10895 }
10996
110- fun getPaginationInfoBuilderMemberName (): MemberName {
111- val paginationInfo = getPaginationInfoClassName()
112-
113- return MemberName (
114- ClassName (
115- paginationInfo.packageName,
116- * paginationInfo.simpleNames.toTypedArray(),
117- " Companion"
118- ), " buildByMap"
119- )
120- }
97+ fun getPaginationInfoBuilderMemberName (): MemberName =
98+ MemberName (getPaginationInfoClassName().addSimpleNames(" Companion" ), " buildByMap" )
12199
122100 fun getPaginationConnectionClassName (): ClassName {
123- return buildClassName(" PaginationConnection" , false , " pagination" )
101+ return buildClassName(" PaginationConnection" , " pagination" )
124102 }
125103
126104 fun getPaginationEdgeClassName (): ClassName {
127- return buildClassName(" PaginationEdge" , false , " pagination" )
105+ return buildClassName(" PaginationEdge" , " pagination" )
128106 }
129107
130108 fun getPaginationPageInfoClassName (): ClassName {
131- return buildClassName(" PaginationPageInfo" , false , " pagination" )
109+ return buildClassName(" PaginationPageInfo" , " pagination" )
132110 }
133111
134112 /* *
@@ -137,18 +115,27 @@ internal class GeneratedMapper(
137115 */
138116 private fun buildClassName (
139117 className : String ,
140- appendCompanion : Boolean = false,
141118 additionalPackage : String = ""
142119 ): ClassName {
120+ // Create the package for the Classname.
143121 val fullPackage = options.generatedBasePackage + " ." + additionalPackage
144122
123+ // Augment the name of the class with the global prefix.
145124 val augmented =
146125 if (options.generatedGlobalPrefix == null ) className
147126 else " ${options.generatedGlobalPrefix}$className "
148127
149- return if (appendCompanion)
150- ClassName (fullPackage, augmented, " Companion" )
151- else
152- ClassName (fullPackage, augmented)
128+ // Build the ClassName.
129+ return ClassName (fullPackage, augmented)
130+ }
131+
132+ /* *
133+ * Will create a copy of the [ClassName] and add the given [name]s as [ClassName.simpleNames].
134+ *
135+ * @param name The names to add as simple names to the ClassName.
136+ * @return The new [ClassName] with the added simple names.
137+ */
138+ private fun ClassName.addSimpleNames (vararg name : String ): ClassName {
139+ return ClassName (this .packageName, * this .simpleNames.toTypedArray(), * name)
153140 }
154141}
0 commit comments