fun toSelectionSet()

in graphql-dgs-codegen-shared-core/src/main/kotlin/com/netflix/graphql/dgs/client/codegen/ProjectionSerializer.kt [32:90]


    fun toSelectionSet(projection: BaseProjectionNode): SelectionSet {
        val selectionSet = SelectionSet.newSelectionSet()

        for ((fieldName, value) in projection.fields) {
            val fieldSelection =
                Field
                    .newField()
                    .name(fieldName)
                    .arguments(
                        projection.inputArguments[fieldName].orEmpty().map { (argName, values, isReference, type) ->
                            if (isReference) {
                                query.variableDefinitions.add(VariableDefinition(values as String, type))
                                Argument(argName, VariableReference(values))
                            } else {
                                Argument(argName, inputValueSerializer.toValue(values))
                            }
                        },
                    )
            if (value is BaseProjectionNode) {
                val fieldSelectionSet = toSelectionSet(value)
                if (fieldSelectionSet.selections.isNotEmpty()) {
                    fieldSelection.selectionSet(fieldSelectionSet)
                }
            } else if (value != null) {
                fieldSelection.selectionSet(
                    SelectionSet
                        .newSelectionSet()
                        .selection(Field.newField(value.toString()).build())
                        .build(),
                )
            }
            selectionSet.selection(fieldSelection.build())
        }

        for (fragment in projection.fragments) {
            val typeCondition =
                fragment.schemaType
                    .map { TypeName(it) }
                    .orElseGet {
                        val className =
                            fragment::class.simpleName
                                ?: throw AssertionError("Unable to determine class name for projection: $fragment")
                        TypeName(
                            className
                                .substringAfterLast("_")
                                .substringBefore("Projection"),
                        )
                    }

            selectionSet.selection(
                InlineFragment
                    .newInlineFragment()
                    .typeCondition(typeCondition)
                    .selectionSet(toSelectionSet(fragment))
                    .build(),
            )
        }
        return selectionSet.build()
    }