diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/DgsClient.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/DgsClient.kt index fe406809..8fc603c7 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/DgsClient.kt +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/DgsClient.kt @@ -1,3 +1,14 @@ package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected -public object DgsClient +import com.netflix.graphql.dgs.client.codegen.InputValueSerializerInterface +import com.netflix.graphql.dgs.codegen.GraphQLProjection +import com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.client.QueryProjection +import graphql.language.OperationDefinition +import kotlin.String + +public object DgsClient { + public fun buildQuery(inputValueSerializer: InputValueSerializerInterface? = null, + _projection: QueryProjection.() -> QueryProjection): String = + GraphQLProjection.asQuery(OperationDefinition.Operation.QUERY, + QueryProjection(inputValueSerializer), _projection) +} diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/DgsConstants.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/DgsConstants.kt index da09e098..728f53f2 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/DgsConstants.kt +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/DgsConstants.kt @@ -3,9 +3,29 @@ package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected import kotlin.String public object DgsConstants { + public const val QUERY_TYPE: String = "Query" + public object SAMPLETYPE { public const val TYPE_NAME: String = "SampleType" public const val Return: String = "return" } + + public object QUERY { + public const val TYPE_NAME: String = "Query" + + public const val People: String = "people" + } + + public object PERSON { + public const val TYPE_NAME: String = "Person" + + public const val Info: String = "info" + + public const val Interface: String = "interface" + + public object INFO_INPUT_ARGUMENT { + public const val Package: String = "package" + } + } } diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/client/PersonProjection.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/client/PersonProjection.kt new file mode 100644 index 00000000..698d5005 --- /dev/null +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/client/PersonProjection.kt @@ -0,0 +1,21 @@ +package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.client + +import com.netflix.graphql.dgs.client.codegen.InputValueSerializerInterface +import com.netflix.graphql.dgs.codegen.GraphQLProjection +import kotlin.String + +public class PersonProjection( + inputValueSerializer: InputValueSerializerInterface? = null, +) : GraphQLProjection(inputValueSerializer) { + public val `interface`: PersonProjection + get() { + field("interface") + return this + } + + public fun info(`package`: String? = default("package")): + PersonProjection { + field("info", "package" to `package`) + return this + } +} diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/client/QueryProjection.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/client/QueryProjection.kt new file mode 100644 index 00000000..c19ffa3f --- /dev/null +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/client/QueryProjection.kt @@ -0,0 +1,15 @@ +package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.client + +import com.netflix.graphql.dgs.client.codegen.InputValueSerializerInterface +import com.netflix.graphql.dgs.codegen.GraphQLProjection +import kotlin.String + +public class QueryProjection( + inputValueSerializer: InputValueSerializerInterface? = null, +) : GraphQLProjection(inputValueSerializer) { + public fun people(_alias: String? = null, _projection: PersonProjection.() -> PersonProjection): + QueryProjection { + field(_alias, "people", PersonProjection(inputValueSerializer), _projection) + return this + } +} diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/types/Person.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/types/Person.kt new file mode 100644 index 00000000..9c922ada --- /dev/null +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/types/Person.kt @@ -0,0 +1,62 @@ +package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.types + +import com.fasterxml.jackson.`annotation`.JsonIgnoreProperties +import com.fasterxml.jackson.`annotation`.JsonProperty +import com.fasterxml.jackson.`annotation`.JsonTypeInfo +import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize +import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder +import java.lang.IllegalStateException +import kotlin.String +import kotlin.jvm.JvmName + +@JsonTypeInfo(use = JsonTypeInfo.Id.NONE) +@JsonDeserialize(builder = Person.Builder::class) +public class Person( + info: () -> String? = infoDefault, + `interface`: () -> String? = interfaceDefault, +) { + private val __info: () -> String? = info + + private val __interface: () -> String? = `interface` + + @get:JvmName("getInfo") + public val info: String? + get() = __info.invoke() + + @get:JvmName("getInterface") + public val `interface`: String? + get() = __interface.invoke() + + public companion object { + private val infoDefault: () -> String? = + { throw IllegalStateException("Field `info` was not requested") } + + + private val interfaceDefault: () -> String? = + { throw IllegalStateException("Field `interface` was not requested") } + + } + + @JsonPOJOBuilder + @JsonIgnoreProperties("__typename") + public class Builder { + private var info: () -> String? = infoDefault + + private var `interface`: () -> String? = interfaceDefault + + @JsonProperty("info") + public fun withInfo(info: String?): Builder = this.apply { + this.info = { info } + } + + @JsonProperty("interface") + public fun withInterface(`interface`: String?): Builder = this.apply { + this.`interface` = { `interface` } + } + + public fun build(): Person = Person( + info = info, + `interface` = `interface`, + ) + } +} diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/types/Query.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/types/Query.kt new file mode 100644 index 00000000..d94d7680 --- /dev/null +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/types/Query.kt @@ -0,0 +1,43 @@ +package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.types + +import com.fasterxml.jackson.`annotation`.JsonIgnoreProperties +import com.fasterxml.jackson.`annotation`.JsonProperty +import com.fasterxml.jackson.`annotation`.JsonTypeInfo +import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize +import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder +import java.lang.IllegalStateException +import kotlin.collections.List +import kotlin.jvm.JvmName + +@JsonTypeInfo(use = JsonTypeInfo.Id.NONE) +@JsonDeserialize(builder = Query.Builder::class) +public class Query( + people: () -> List? = peopleDefault, +) { + private val __people: () -> List? = people + + @get:JvmName("getPeople") + public val people: List? + get() = __people.invoke() + + public companion object { + private val peopleDefault: () -> List? = + { throw IllegalStateException("Field `people` was not requested") } + + } + + @JsonPOJOBuilder + @JsonIgnoreProperties("__typename") + public class Builder { + private var people: () -> List? = peopleDefault + + @JsonProperty("people") + public fun withPeople(people: List?): Builder = this.apply { + this.people = { people } + } + + public fun build(): Query = Query( + people = people, + ) + } +} diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/schema.graphql b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/schema.graphql index c7162a29..de4de5f3 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/schema.graphql +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/schema.graphql @@ -1,3 +1,12 @@ type SampleType { return: String! } + +type Query { + people: [Person] +} + +type Person { + info(package: String): String + interface: String +} diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithNestedInputs/expected/client/QueryProjection.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithNestedInputs/expected/client/QueryProjection.kt index 5eb527e2..afcaeae6 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithNestedInputs/expected/client/QueryProjection.kt +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithNestedInputs/expected/client/QueryProjection.kt @@ -11,13 +11,13 @@ public class QueryProjection( ) : GraphQLProjection(inputValueSerializer) { public fun q1(arg1: String? = default("arg1"), arg2: I2? = default("arg2")): QueryProjection { - field("q1", "arg1" to arg1 , "arg2" to arg2) + field("q1", "arg1" to arg1, "arg2" to arg2) return this } public fun q2(arg1: I1? = default("arg1"), arg2: String? = default("arg2")): QueryProjection { - field("q2", "arg1" to arg1 , "arg2" to arg2) + field("q2", "arg1" to arg1, "arg2" to arg2) return this } } diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithPrimitiveAndArgs/expected/client/QueryProjection.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithPrimitiveAndArgs/expected/client/QueryProjection.kt index d9e4a2ef..b39034f8 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithPrimitiveAndArgs/expected/client/QueryProjection.kt +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithPrimitiveAndArgs/expected/client/QueryProjection.kt @@ -13,7 +13,7 @@ public class QueryProjection( a2: String, a3: I? = default("a3"), ): QueryProjection { - field("string", "a1" to a1 , "a2" to a2 , "a3" to a3) + field("string", "a1" to a1, "a2" to a2, "a3" to a3) return this } } diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithTypeAndArgs/expected/client/QueryProjection.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithTypeAndArgs/expected/client/QueryProjection.kt index ba9ed9bf..b4952f8d 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithTypeAndArgs/expected/client/QueryProjection.kt +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithTypeAndArgs/expected/client/QueryProjection.kt @@ -15,8 +15,8 @@ public class QueryProjection( _alias: String? = null, _projection: PersonProjection.() -> PersonProjection, ): QueryProjection { - field(_alias, "person", PersonProjection(inputValueSerializer), _projection, "a1" to a1 , "a2" - to a2 , "a3" to a3) + field(_alias, "person", PersonProjection(inputValueSerializer), _projection, "a1" to a1, "a2" to + a2, "a3" to a3) return this } } diff --git a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin2/GenerateKotlin2ClientTypes.kt b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin2/GenerateKotlin2ClientTypes.kt index 0cb18770..efc12c3c 100644 --- a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin2/GenerateKotlin2ClientTypes.kt +++ b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin2/GenerateKotlin2ClientTypes.kt @@ -28,6 +28,7 @@ import com.netflix.graphql.dgs.codegen.generators.shared.SchemaExtensionsUtils import com.netflix.graphql.dgs.codegen.generators.shared.excludeSchemaTypeExtension import com.netflix.graphql.dgs.codegen.shouldSkip import com.squareup.kotlinpoet.ClassName +import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.FunSpec import com.squareup.kotlinpoet.LambdaTypeName @@ -105,12 +106,13 @@ fun generateKotlin2ClientTypes( FunSpec.builder(field.name) .addInputArgs(config, typeLookup, typeName, field.inputValueDefinitions) .returns(typeName) - .addStatement( - """field(%S%L)""", - field.name, - field.inputValueDefinitions.joinToString(" ") { """, "${it.name}" to ${it.name}""" } + .addCode( + field.inputValueDefinitions.let { iv -> + val builder = CodeBlock.builder().add("field(%S", field.name) + iv.forEach { d -> builder.add(", %S to %N", d.name, d.name) } + builder.add(")\nreturn this").build() + } ) - .addStatement("return this") .build() } @@ -125,13 +127,17 @@ fun generateKotlin2ClientTypes( .addParameter(ParameterSpec.builder("_alias", String::class.asTypeName().copy(nullable = true)).defaultValue("null").build()) .addParameter(projection) .returns(typeName) - .addStatement( - """field(_alias, %S, %T(inputValueSerializer), _projection%L)""", - field.name, - projectionType, - field.inputValueDefinitions.joinToString(" ") { """, "${it.name}" to ${it.name}""" } + .addCode( + field.inputValueDefinitions.let { iv -> + val builder = CodeBlock.builder().add( + "field(_alias, %S, %T(inputValueSerializer), _projection", + field.name, + projectionType + ) + iv.forEach { d -> builder.add(", %S to %N", d.name, d.name) } + builder.add(")\nreturn this").build() + } ) - .addStatement("return this") .build() } }