From 309b6fdccd09ff316818f323f3c9b01cbf81e1a6 Mon Sep 17 00:00:00 2001 From: Tobias Trumm Date: Fri, 28 Apr 2023 09:41:49 +0200 Subject: [PATCH] fix: Improve alias handling in client generation Take aliases into account when calculating the names of the selected fields. fixes #1760 --- .../generator/types/generateTypeName.kt | 5 +-- .../alias_nested/AliasNestedQuery.graphql | 8 +++++ .../alias_nested/AliasNestedQuery.kt | 33 +++++++++++++++++++ .../aliasnestedquery/ComplexObject.kt | 17 ++++++++++ .../aliasnestedquery/ComplexObject2.kt | 17 ++++++++++ 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/AliasNestedQuery.graphql create mode 100644 plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/AliasNestedQuery.kt create mode 100644 plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/aliasnestedquery/ComplexObject.kt create mode 100644 plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/aliasnestedquery/ComplexObject2.kt diff --git a/plugins/client/graphql-kotlin-client-generator/src/main/kotlin/com/expediagroup/graphql/plugin/client/generator/types/generateTypeName.kt b/plugins/client/graphql-kotlin-client-generator/src/main/kotlin/com/expediagroup/graphql/plugin/client/generator/types/generateTypeName.kt index bb0a1db2b7..b74b4fca92 100644 --- a/plugins/client/graphql-kotlin-client-generator/src/main/kotlin/com/expediagroup/graphql/plugin/client/generator/types/generateTypeName.kt +++ b/plugins/client/graphql-kotlin-client-generator/src/main/kotlin/com/expediagroup/graphql/plugin/client/generator/types/generateTypeName.kt @@ -229,9 +229,10 @@ private fun calculateSelectedFields( selectionSet.selections.forEach { selection -> when (selection) { is Field -> { - result.add(path + selection.name) + val fieldName = selection.alias ?: selection.name + result.add(path + fieldName) if (selection.selectionSet != null) { - result.addAll(calculateSelectedFields(context, targetType, selection.selectionSet, "$path${selection.name}.")) + result.addAll(calculateSelectedFields(context, targetType, selection.selectionSet, "$path$fieldName.")) } } is InlineFragment -> { diff --git a/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/AliasNestedQuery.graphql b/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/AliasNestedQuery.graphql new file mode 100644 index 0000000000..9b3609146f --- /dev/null +++ b/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/AliasNestedQuery.graphql @@ -0,0 +1,8 @@ +query AliasNestedQuery { + first: complexObjectQuery { + nameA: name + } + second: complexObjectQuery { + nameB: name + } +} diff --git a/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/AliasNestedQuery.kt b/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/AliasNestedQuery.kt new file mode 100644 index 0000000000..6b8b1325e5 --- /dev/null +++ b/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/AliasNestedQuery.kt @@ -0,0 +1,33 @@ +package com.expediagroup.graphql.generated + +import com.expediagroup.graphql.client.Generated +import com.expediagroup.graphql.client.types.GraphQLClientRequest +import com.expediagroup.graphql.generated.aliasnestedquery.ComplexObject +import com.expediagroup.graphql.generated.aliasnestedquery.ComplexObject2 +import kotlin.String +import kotlin.reflect.KClass + +public const val ALIAS_NESTED_QUERY: String = + "query AliasNestedQuery {\n first: complexObjectQuery {\n nameA: name\n }\n second: complexObjectQuery {\n nameB: name\n }\n}" + +@Generated +public class AliasNestedQuery : GraphQLClientRequest { + public override val query: String = ALIAS_NESTED_QUERY + + public override val operationName: String = "AliasNestedQuery" + + public override fun responseType(): KClass = + AliasNestedQuery.Result::class + + @Generated + public data class Result( + /** + * Query returning an object that references another object + */ + public val first: ComplexObject, + /** + * Query returning an object that references another object + */ + public val second: ComplexObject2, + ) +} diff --git a/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/aliasnestedquery/ComplexObject.kt b/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/aliasnestedquery/ComplexObject.kt new file mode 100644 index 0000000000..67384da863 --- /dev/null +++ b/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/aliasnestedquery/ComplexObject.kt @@ -0,0 +1,17 @@ +package com.expediagroup.graphql.generated.aliasnestedquery + +import com.expediagroup.graphql.client.Generated +import kotlin.String + +/** + * Multi line description of a complex type. + * This is a second line of the paragraph. + * This is final line of the description. + */ +@Generated +public data class ComplexObject( + /** + * Some object name + */ + public val nameA: String, +) diff --git a/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/aliasnestedquery/ComplexObject2.kt b/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/aliasnestedquery/ComplexObject2.kt new file mode 100644 index 0000000000..3dac9e25b2 --- /dev/null +++ b/plugins/client/graphql-kotlin-client-generator/src/test/data/generator/alias_nested/aliasnestedquery/ComplexObject2.kt @@ -0,0 +1,17 @@ +package com.expediagroup.graphql.generated.aliasnestedquery + +import com.expediagroup.graphql.client.Generated +import kotlin.String + +/** + * Multi line description of a complex type. + * This is a second line of the paragraph. + * This is final line of the description. + */ +@Generated +public data class ComplexObject2( + /** + * Some object name + */ + public val nameB: String, +)