in graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/EntitiesRepresentationTypeGenerator.kt [42:117]
fun generate(
definition: ObjectTypeDefinition,
generatedRepresentations: MutableMap<String, Any>,
): CodeGenResult =
EntitiesRepresentationTypeGeneratorUtils.generate(
config,
definition,
generatedRepresentations,
this::generateRepresentations,
)
private fun generateRepresentations(
definitionName: String,
representationName: String,
fields: List<FieldDefinition>,
generatedRepresentations: MutableMap<String, Any>,
keyFields: Map<String, Any>,
): CodeGenResult {
if (generatedRepresentations.containsKey(representationName)) {
return CodeGenResult.EMPTY
}
var fieldsCodeGenAccumulator = CodeGenResult.EMPTY
// generate representations of entity types that have @key, including the __typename field, and the key fields
val typeName = Field("__typename", ClassName.get(String::class.java), CodeBlock.of("\$S", definitionName))
val fieldDefinitions =
fields
.filter { keyFields.containsKey(it.name) }
.map {
val type = findType(it.type, document)
if (type != null &&
(
type is ObjectTypeDefinition ||
type is InterfaceTypeDefinition ||
type is EnumTypeDefinition
)
) {
val fieldTypeRepresentationName = toRepresentationName(type)
val fieldRepresentationType =
typeUtils
.findReturnType(it.type)
.toString()
.replace(type.name, fieldTypeRepresentationName)
if (generatedRepresentations.containsKey(fieldTypeRepresentationName)) {
logger.trace("Representation for {} was already generated.", fieldTypeRepresentationName)
} else {
logger.debug("Generating entity representation {} ...", fieldTypeRepresentationName)
val fieldTypeRepresentation =
generateRepresentations(
type.name,
fieldTypeRepresentationName,
type.fieldDefinitions(),
generatedRepresentations,
keyFields[it.name] as Map<String, Any>,
)
fieldsCodeGenAccumulator = fieldsCodeGenAccumulator.merge(fieldTypeRepresentation)
generatedRepresentations[fieldTypeRepresentationName] = fieldRepresentationType
}
Field(it.name, ClassName.get("", fieldRepresentationType))
} else {
Field(it.name, typeUtils.findReturnType(it.type))
}
}
// Generate base type representation...
val parentRepresentationCodeGen =
super.generate(
name = representationName,
interfaces = emptyList(),
fields = fieldDefinitions + typeName,
description = null,
directives = emptyList(),
)
generatedRepresentations[representationName] = typeUtils.qualifyName(representationName)
// Merge all results.
return parentRepresentationCodeGen.merge(fieldsCodeGenAccumulator)
}