@@ -21,9 +21,16 @@ package com.netflix.graphql.dgs.codegen.generators.java
2121import com.netflix.graphql.dgs.codegen.CodeGenConfig
2222import com.netflix.graphql.dgs.codegen.CodeGenResult
2323import com.netflix.graphql.dgs.codegen.fieldDefinitions
24+ import com.netflix.graphql.dgs.codegen.generators.EntitiesRepresentationTypeGeneratorUtils
25+ import com.netflix.graphql.dgs.codegen.generators.EntitiesRepresentationTypeGeneratorUtils.findType
26+ import com.netflix.graphql.dgs.codegen.generators.EntitiesRepresentationTypeGeneratorUtils.toRepresentationName
2427import com.squareup.javapoet.ClassName
2528import com.squareup.javapoet.CodeBlock
26- import graphql.language.*
29+ import graphql.language.Document
30+ import graphql.language.EnumTypeDefinition
31+ import graphql.language.FieldDefinition
32+ import graphql.language.InterfaceTypeDefinition
33+ import graphql.language.ObjectTypeDefinition
2734import org.slf4j.LoggerFactory
2835
2936@Suppress(" UNCHECKED_CAST" )
@@ -32,27 +39,15 @@ class EntitiesRepresentationTypeGenerator(
3239 document : Document
3340) : BaseDataTypeGenerator(config.packageNameClient, config, document) {
3441
35- fun generate (definition : ObjectTypeDefinition , generatedRepresentations : MutableMap <String , Any >): CodeGenResult {
36- if (config.skipEntityQueries) {
37- return CodeGenResult ()
38- }
39- val representationName = toRepresentationName(definition)
40- if (generatedRepresentations.containsKey(representationName)) {
41- return CodeGenResult ()
42- }
43- val directiveArg =
44- definition
45- .getDirectives(" key" )
46- .map { it.argumentsByName[" fields" ]?.value as StringValue }
47- .map { it.value }
48-
49- val keyFields = parseKeyDirectiveValue(directiveArg)
50- return generateRepresentations(
51- definition.name,
52- representationName,
53- definition.fieldDefinitions,
42+ fun generate (
43+ definition : ObjectTypeDefinition ,
44+ generatedRepresentations : MutableMap <String , Any >
45+ ): CodeGenResult {
46+ return EntitiesRepresentationTypeGeneratorUtils .generate(
47+ config,
48+ definition,
5449 generatedRepresentations,
55- keyFields
50+ this ::generateRepresentations
5651 )
5752 }
5853
@@ -74,7 +69,6 @@ class EntitiesRepresentationTypeGenerator(
7469 .filter { keyFields.containsKey(it.name) }
7570 .map {
7671 val type = findType(it.type, document)
77-
7872 if (type != null &&
7973 (
8074 type is ObjectTypeDefinition ||
@@ -105,8 +99,7 @@ class EntitiesRepresentationTypeGenerator(
10599 }
106100 Field (it.name, ClassName .get(" " , fieldRepresentationType))
107101 } else {
108- val returnType = typeUtils.findReturnType(it.type)
109- Field (it.name, returnType)
102+ Field (it.name, typeUtils.findReturnType(it.type))
110103 }
111104 }
112105 // Generate base type representation...
@@ -121,61 +114,8 @@ class EntitiesRepresentationTypeGenerator(
121114 return parentRepresentationCodeGen.merge(fieldsCodeGenAccumulator)
122115 }
123116
124- private fun findType (typeName : Type <* >, document : Document ): TypeDefinition <* >? {
125- return when (typeName) {
126- is NonNullType -> {
127- findType(typeName.type, document)
128- }
129- is ListType -> {
130- findType(typeName.type, document)
131- }
132- else -> document.definitions.filterIsInstance<TypeDefinition <* >>()
133- .find { it.name == (typeName as TypeName ).name }
134- }
135- }
136-
137- private fun parseKeyDirectiveValue (keyDirective : List <String >): Map <String , Any > {
138- data class Node (val key : String , val map : MutableMap <String , Any >, val parent : Node ? )
139-
140- val keys = keyDirective.map { ds ->
141- ds.map { if (it == ' {' || it == ' }' ) " $it " else " $it " }
142- .joinToString(" " , " " , " " )
143- .split(" " )
144- }.flatten()
145-
146- // handle simple keys and nested keys by constructing the path to each key
147- // e.g. type Movie @key(fields: "movieId") or type MovieCast @key(fields: movie { movieId } actors { name } }
148- val mappedKeyTypes = mutableMapOf<String , Any >()
149- var parent = Node (" " , mappedKeyTypes, null )
150- var current = Node (" " , mappedKeyTypes, null )
151- keys.filter { it != " " && it != " " }
152- .forEach {
153- when (it) {
154- " {" -> {
155- // push a new map for the next level
156- val previous = parent
157- parent = current
158- current = Node (" " , current.map[current.key] as MutableMap <String , Any >, previous)
159- }
160- " }" -> {
161- // pop back to parent level
162- current = parent
163- parent = parent.parent!!
164- }
165- else -> {
166- // make an entry at the current level
167- current.map.putIfAbsent(it, mutableMapOf<String , Any >())
168- current = Node (it, current.map, parent)
169- }
170- }
171- }
172- return mappedKeyTypes
173- }
174-
175117 companion object {
176118 private val logger: org.slf4j.Logger =
177119 LoggerFactory .getLogger(EntitiesRepresentationTypeGenerator ::class .java)
178-
179- private fun toRepresentationName (definition : TypeDefinition <* >) = " ${definition.name} Representation"
180120 }
181121}
0 commit comments