Skip to content

_alias in mutation argument breaks compilation #930

@rostislavosvald-ataccama

Description

I use client generation and we have _alias argument for one mutation in graphql schema I am using. This breaks compilation as generated MutationProjection method contains 2 arguments with the same name:

  public fun nodeSetCreate2(
    _alias: String? = default<MutationProjection, String?>("_alias"),
    summary: String? = default<MutationProjection, String?>("summary"),
    _alias: String? = null,
    _projection: MdNodeSetNodeMutationResultProjection.() -> MdNodeSetNodeMutationResultProjection,
  ): MutationProjection {
    field(_alias, "nodeSetCreate2", MdNodeSetNodeMutationResultProjection(inputValueSerializer),
        _projection, "_alias" to _alias, "summary" to summary)
    return this
  }

It generates this error:

e: file:///<path>/build/generated/sources/dgs-codegen/com/ataccama/metadata/client/dgs/client/MutationProjection.kt:3090:5 Conflicting declarations:
_alias: String? = ...
_alias: String? = ...

Would it be possible to override the parameter name somehow?

My current solution is to replace the name in the kotlin file right after it is generated. This is my current config of GenerateJavaTask:

tasks.withType<com.netflix.graphql.dgs.codegen.gradle.GenerateJavaTask> {
    schemaPaths = mutableListOf("$projectDir/src/main/resources/graphql-client/metadata/schema.graphqls")
    packageName = "com.ataccama.metadata.client.dgs"
    language = "kotlin"
    generateClient = true
    generateKotlinNullableClasses = true
    generateKotlinClosureProjections = true
    shortProjectionNames = true
    typeMapping = mutableMapOf(
        "AnyScalar" to "kotlin.Any",
        "BigDecimal" to "java.math.BigDecimal",
        "BigInteger" to "java.math.BigInteger",
        "Byte" to "kotlin.Byte",
        "Cursor" to "kotlin.String",
        "Duration" to "kotlin.String",
        "Filter" to "kotlin.String",
        "GID" to "kotlin.String",
        "Hcn" to "kotlin.Long",
        "Json" to "kotlin.Any",
        "JsonNode" to "kotlin.Any",
        "Long" to "kotlin.Long",
        "New" to "kotlin.Any",
        "NodePath" to "kotlin.String",
        "ObjectLink" to "kotlin.String",
        "Patch" to "kotlin.Any",
        "RichText" to "kotlin.String",
        "SessionId" to "kotlin.String",
        "Short" to "kotlin.Short",
        "Timestamp" to "kotlin.String",
    )
    doLast {
        // Fix DGS codegen conflict: schema field `_alias` on nodeSetCreate2 clashes with codegen's own `_alias` parameter
        val mutationFile = file("build/generated/sources/dgs-codegen/com/ataccama/metadata/client/dgs/client/MutationProjection.kt")
        if (mutationFile.exists()) {
            var content = mutationFile.readText()
            content = content.replace(
                "    _alias: String? = default<MutationProjection, String?>(\"_alias\"),\n" +
                    "    summary: String? = default<MutationProjection, String?>(\"summary\"),\n" +
                    "    _alias: String? = null,",
                "    nodeSetAlias: String? = default<MutationProjection, String?>(\"_alias\"),\n" +
                    "    summary: String? = default<MutationProjection, String?>(\"summary\"),\n" +
                    "    _alias: String? = null,",
            )
            content = content.replace(
                "\"_alias\" to _alias, \"summary\" to summary)",
                "\"_alias\" to nodeSetAlias, \"summary\" to summary)",
            )
            mutationFile.writeText(content)
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions