fun generate()

in graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin/KotlinEntitiesRepresentationTypeGenerator.kt [45:135]


    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 (representationName in generatedRepresentations) {
            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", STRING, false, 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 fieldType = typeUtils.findReturnType(it.type)
                        val fieldTypeRepresentationName = toRepresentationName(type)
                        val fieldRepresentationType =
                            fieldType
                                .toString()
                                .replace(type.name, fieldTypeRepresentationName)
                                .removeSuffix("?")

                        if (generatedRepresentations.containsKey(fieldTypeRepresentationName)) {
                            logger.trace("Representation for {} was already generated.", fieldTypeRepresentationName)
                        } else {
                            logger.debug("Generating entity representation {} ...", fieldTypeRepresentationName)
                            @Suppress("UNCHECKED_CAST")
                            val fieldTypeRepresentation =
                                generateRepresentations(
                                    type.name,
                                    fieldTypeRepresentationName,
                                    type.fieldDefinitions(),
                                    generatedRepresentations,
                                    keyFields[it.name] as Map<String, Any>,
                                )
                            fieldsCodeGenAccumulator = fieldsCodeGenAccumulator.merge(fieldTypeRepresentation)
                            generatedRepresentations[fieldTypeRepresentationName] = fieldRepresentationType
                        }
                        if (fieldType is ParameterizedTypeName && fieldType.rawType.simpleName == "List") {
                            Field(
                                it.name,
                                LIST.parameterizedBy(ClassName(getPackageName(), fieldTypeRepresentationName)),
                                typeUtils.isNullable(it.type),
                            )
                        } else {
                            Field(
                                it.name,
                                ClassName(getPackageName(), fieldTypeRepresentationName),
                                typeUtils.isNullable(it.type),
                            )
                        }
                    } else {
                        Field(it.name, typeUtils.findReturnType(it.type), typeUtils.isNullable(it.type))
                    }
                }
        // Generate base type representation...
        val parentRepresentationCodeGen =
            super.generate(
                name = representationName,
                interfaces = emptyList(),
                fields = fieldDefinitions.plus(typeName),
                description = null,
                document = document,
                directives = emptyList(),
            )
        generatedRepresentations[representationName] = typeUtils.qualifyName(representationName)
        // Merge all results.
        return parentRepresentationCodeGen.merge(fieldsCodeGenAccumulator)
    }