Skip to content

Commit 928acc7

Browse files
committed
feat(codegen): move method to common GraphQlTypeHelper, add comments
1 parent 22d15df commit 928acc7

File tree

2 files changed

+44
-38
lines changed
  • graphql-kotlin-toolkit-codegen/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/codeblock
  • graphql-kotlin-toolkit-common/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/common/helper

2 files changed

+44
-38
lines changed

graphql-kotlin-toolkit-codegen/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/codeblock/ArgumentCodeBlockGenerator.kt

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.auritylab.graphql.kotlin.toolkit.codegen.helper.NamingHelper
44
import com.auritylab.graphql.kotlin.toolkit.codegen.mapper.GeneratedMapper
55
import com.auritylab.graphql.kotlin.toolkit.codegen.mapper.KotlinTypeMapper
66
import com.auritylab.graphql.kotlin.toolkit.common.directive.DirectiveFacade
7+
import com.auritylab.graphql.kotlin.toolkit.common.helper.GraphQLTypeHelper
78
import com.squareup.kotlinpoet.ANY
89
import com.squareup.kotlinpoet.CodeBlock
910
import com.squareup.kotlinpoet.FunSpec
@@ -17,7 +18,6 @@ import graphql.schema.GraphQLDirectiveContainer
1718
import graphql.schema.GraphQLEnumType
1819
import graphql.schema.GraphQLInputObjectType
1920
import graphql.schema.GraphQLList
20-
import graphql.schema.GraphQLModifiedType
2121
import graphql.schema.GraphQLNonNull
2222
import graphql.schema.GraphQLType
2323

@@ -42,17 +42,17 @@ internal class ArgumentCodeBlockGenerator(
4242
argumentName: String,
4343
type: GraphQLType,
4444
fieldDirectiveContainer: GraphQLDirectiveContainer?
45-
): FunSpec {
46-
val kotlinType = typeMapper.getKotlinType(type, fieldDirectiveContainer)
47-
48-
return FunSpec.builder("resolve${NamingHelper.uppercaseFirstLetter(argumentName)}")
49-
.addModifiers(KModifier.PRIVATE)
50-
.addParameter("map", MAP.parameterizedBy(STRING, ANY))
51-
.returns(kotlinType)
52-
.addCode(buildArgumentResolverCodeBlock(argumentName, type, fieldDirectiveContainer))
53-
.build()
54-
}
45+
) = FunSpec.builder("resolve${NamingHelper.uppercaseFirstLetter(argumentName)}")
46+
.addModifiers(KModifier.PRIVATE)
47+
.addParameter("map", MAP.parameterizedBy(STRING, ANY))
48+
.returns(typeMapper.getKotlinType(type, fieldDirectiveContainer))
49+
.addCode(buildArgumentResolverCodeBlock(argumentName, type, fieldDirectiveContainer))
50+
.build()
5551

52+
/**
53+
* Will build the [CodeBlock], which contains all the functions to resolve each layer and build the final result
54+
* for the given [type].
55+
*/
5656
private fun buildArgumentResolverCodeBlock(
5757
name: String,
5858
type: GraphQLType,
@@ -61,7 +61,7 @@ internal class ArgumentCodeBlockGenerator(
6161
val code = CodeBlock.builder()
6262

6363
// Get all layers of the type.
64-
val layers = unwrapTypeLayers(type).asReversed()
64+
val layers = GraphQLTypeHelper.unwrapTypeLayers(type).asReversed()
6565

6666
var currentIndex = 0
6767
var lastType: TypeName = ANY
@@ -99,23 +99,8 @@ internal class ArgumentCodeBlockGenerator(
9999
}
100100

101101
/**
102-
* A [GraphQLType] can be wrapped with unmodifiable types (e.g. List, NonNull).
103-
* This method will unwrap until an actual type.
102+
* Will build the [CodeBlock] which will resolve the given [type].
104103
*/
105-
private fun unwrapTypeLayers(type: GraphQLType): List<GraphQLType> {
106-
val wraps = mutableListOf<GraphQLType>()
107-
108-
var c = type
109-
while (c is GraphQLModifiedType) {
110-
wraps.add(c)
111-
c = c.wrappedType
112-
}
113-
114-
wraps.add(c)
115-
116-
return wraps
117-
}
118-
119104
private fun buildLayerParser(
120105
type: GraphQLType,
121106
index: Int,

graphql-kotlin-toolkit-common/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/common/helper/GraphQLTypeHelper.kt

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,40 @@ import graphql.schema.GraphQLType
88
object GraphQLTypeHelper {
99
/**
1010
* Will unwrap the given [type] until the type is no longer instance of [GraphQLModifiedType].
11-
* A [GraphQLType] can wrapped multiple times with a [GraphQLModifiedType].
11+
* This will just return the non [GraphQLModifiedType] aka the most inner type.
12+
*
13+
* @param type The type to unwrap.
14+
* @return The most inner type of the given type.
1215
*/
13-
fun unwrapType(type: GraphQLType): GraphQLType {
14-
// If it's no modified type unwrapping is not necessary.
15-
if (type !is GraphQLModifiedType)
16-
return type
16+
fun unwrapType(type: GraphQLType): GraphQLType =
17+
unwrapTypeLayers(type).last()
18+
19+
/**
20+
* Will unwrap the given [type] until the type is no longer instance of [GraphQLModifiedType]. Each layer of the
21+
* unwrapping process will be returned in the [List]. The list is sorted accordingly to the unwrapping. The final
22+
* non [GraphQLModifiedType] will also be added to the last on the last index.
23+
*
24+
* @param type The type to unwrap.
25+
* @return List of all [GraphQLType].
26+
*/
27+
fun unwrapTypeLayers(type: GraphQLType): List<GraphQLType> {
28+
val wraps = mutableListOf<GraphQLType>()
29+
30+
// Start with the given type.
31+
var c = type
32+
33+
// Iterate until there is no longer a modified type.
34+
while (c is GraphQLModifiedType) {
35+
// Add the current type to the layers.
36+
wraps.add(c)
37+
// Set the current type to the wrapped type.
38+
c = c.wrappedType
39+
}
1740

18-
var t = type
19-
// Will unwrap until the Type is no modified type.
20-
while (t is GraphQLModifiedType)
21-
t = t.wrappedType
41+
// Add the non modified type to the list.
42+
wraps.add(c)
2243

23-
return t
44+
return wraps
2445
}
2546

2647
/**

0 commit comments

Comments
 (0)