Skip to content

Commit 48e2b3b

Browse files
authored
Merge pull request #700 from Netflix/feature/escape-projection-args
Escape projection argument names in kotlin2
2 parents 08eb4ad + d252f8a commit 48e2b3b

File tree

11 files changed

+204
-17
lines changed

11 files changed

+204
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected
22

3-
public object DgsClient
3+
import com.netflix.graphql.dgs.client.codegen.InputValueSerializerInterface
4+
import com.netflix.graphql.dgs.codegen.GraphQLProjection
5+
import com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.client.QueryProjection
6+
import graphql.language.OperationDefinition
7+
import kotlin.String
8+
9+
public object DgsClient {
10+
public fun buildQuery(inputValueSerializer: InputValueSerializerInterface? = null,
11+
_projection: QueryProjection.() -> QueryProjection): String =
12+
GraphQLProjection.asQuery(OperationDefinition.Operation.QUERY,
13+
QueryProjection(inputValueSerializer), _projection)
14+
}

graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/dataClassWithReservedWord/expected/DgsConstants.kt

+20
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,29 @@ package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected
33
import kotlin.String
44

55
public object DgsConstants {
6+
public const val QUERY_TYPE: String = "Query"
7+
68
public object SAMPLETYPE {
79
public const val TYPE_NAME: String = "SampleType"
810

911
public const val Return: String = "return"
1012
}
13+
14+
public object QUERY {
15+
public const val TYPE_NAME: String = "Query"
16+
17+
public const val People: String = "people"
18+
}
19+
20+
public object PERSON {
21+
public const val TYPE_NAME: String = "Person"
22+
23+
public const val Info: String = "info"
24+
25+
public const val Interface: String = "interface"
26+
27+
public object INFO_INPUT_ARGUMENT {
28+
public const val Package: String = "package"
29+
}
30+
}
1131
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.client
2+
3+
import com.netflix.graphql.dgs.client.codegen.InputValueSerializerInterface
4+
import com.netflix.graphql.dgs.codegen.GraphQLProjection
5+
import kotlin.String
6+
7+
public class PersonProjection(
8+
inputValueSerializer: InputValueSerializerInterface? = null,
9+
) : GraphQLProjection(inputValueSerializer) {
10+
public val `interface`: PersonProjection
11+
get() {
12+
field("interface")
13+
return this
14+
}
15+
16+
public fun info(`package`: String? = default<PersonProjection, String?>("package")):
17+
PersonProjection {
18+
field("info", "package" to `package`)
19+
return this
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.client
2+
3+
import com.netflix.graphql.dgs.client.codegen.InputValueSerializerInterface
4+
import com.netflix.graphql.dgs.codegen.GraphQLProjection
5+
import kotlin.String
6+
7+
public class QueryProjection(
8+
inputValueSerializer: InputValueSerializerInterface? = null,
9+
) : GraphQLProjection(inputValueSerializer) {
10+
public fun people(_alias: String? = null, _projection: PersonProjection.() -> PersonProjection):
11+
QueryProjection {
12+
field(_alias, "people", PersonProjection(inputValueSerializer), _projection)
13+
return this
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.types
2+
3+
import com.fasterxml.jackson.`annotation`.JsonIgnoreProperties
4+
import com.fasterxml.jackson.`annotation`.JsonProperty
5+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo
6+
import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
7+
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
8+
import java.lang.IllegalStateException
9+
import kotlin.String
10+
import kotlin.jvm.JvmName
11+
12+
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
13+
@JsonDeserialize(builder = Person.Builder::class)
14+
public class Person(
15+
info: () -> String? = infoDefault,
16+
`interface`: () -> String? = interfaceDefault,
17+
) {
18+
private val __info: () -> String? = info
19+
20+
private val __interface: () -> String? = `interface`
21+
22+
@get:JvmName("getInfo")
23+
public val info: String?
24+
get() = __info.invoke()
25+
26+
@get:JvmName("getInterface")
27+
public val `interface`: String?
28+
get() = __interface.invoke()
29+
30+
public companion object {
31+
private val infoDefault: () -> String? =
32+
{ throw IllegalStateException("Field `info` was not requested") }
33+
34+
35+
private val interfaceDefault: () -> String? =
36+
{ throw IllegalStateException("Field `interface` was not requested") }
37+
38+
}
39+
40+
@JsonPOJOBuilder
41+
@JsonIgnoreProperties("__typename")
42+
public class Builder {
43+
private var info: () -> String? = infoDefault
44+
45+
private var `interface`: () -> String? = interfaceDefault
46+
47+
@JsonProperty("info")
48+
public fun withInfo(info: String?): Builder = this.apply {
49+
this.info = { info }
50+
}
51+
52+
@JsonProperty("interface")
53+
public fun withInterface(`interface`: String?): Builder = this.apply {
54+
this.`interface` = { `interface` }
55+
}
56+
57+
public fun build(): Person = Person(
58+
info = info,
59+
`interface` = `interface`,
60+
)
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.types
2+
3+
import com.fasterxml.jackson.`annotation`.JsonIgnoreProperties
4+
import com.fasterxml.jackson.`annotation`.JsonProperty
5+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo
6+
import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
7+
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
8+
import java.lang.IllegalStateException
9+
import kotlin.collections.List
10+
import kotlin.jvm.JvmName
11+
12+
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
13+
@JsonDeserialize(builder = Query.Builder::class)
14+
public class Query(
15+
people: () -> List<Person?>? = peopleDefault,
16+
) {
17+
private val __people: () -> List<Person?>? = people
18+
19+
@get:JvmName("getPeople")
20+
public val people: List<Person?>?
21+
get() = __people.invoke()
22+
23+
public companion object {
24+
private val peopleDefault: () -> List<Person?>? =
25+
{ throw IllegalStateException("Field `people` was not requested") }
26+
27+
}
28+
29+
@JsonPOJOBuilder
30+
@JsonIgnoreProperties("__typename")
31+
public class Builder {
32+
private var people: () -> List<Person?>? = peopleDefault
33+
34+
@JsonProperty("people")
35+
public fun withPeople(people: List<Person?>?): Builder = this.apply {
36+
this.people = { people }
37+
}
38+
39+
public fun build(): Query = Query(
40+
people = people,
41+
)
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
type SampleType {
22
return: String!
33
}
4+
5+
type Query {
6+
people: [Person]
7+
}
8+
9+
type Person {
10+
info(package: String): String
11+
interface: String
12+
}

graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithNestedInputs/expected/client/QueryProjection.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ public class QueryProjection(
1111
) : GraphQLProjection(inputValueSerializer) {
1212
public fun q1(arg1: String? = default<QueryProjection, String?>("arg1"), arg2: I2? =
1313
default<QueryProjection, I2?>("arg2")): QueryProjection {
14-
field("q1", "arg1" to arg1 , "arg2" to arg2)
14+
field("q1", "arg1" to arg1, "arg2" to arg2)
1515
return this
1616
}
1717

1818
public fun q2(arg1: I1? = default<QueryProjection, I1?>("arg1"), arg2: String? =
1919
default<QueryProjection, String?>("arg2")): QueryProjection {
20-
field("q2", "arg1" to arg1 , "arg2" to arg2)
20+
field("q2", "arg1" to arg1, "arg2" to arg2)
2121
return this
2222
}
2323
}

graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithPrimitiveAndArgs/expected/client/QueryProjection.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class QueryProjection(
1313
a2: String,
1414
a3: I? = default<QueryProjection, I?>("a3"),
1515
): QueryProjection {
16-
field("string", "a1" to a1 , "a2" to a2 , "a3" to a3)
16+
field("string", "a1" to a1, "a2" to a2, "a3" to a3)
1717
return this
1818
}
1919
}

graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithTypeAndArgs/expected/client/QueryProjection.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class QueryProjection(
1515
_alias: String? = null,
1616
_projection: PersonProjection.() -> PersonProjection,
1717
): QueryProjection {
18-
field(_alias, "person", PersonProjection(inputValueSerializer), _projection, "a1" to a1 , "a2"
19-
to a2 , "a3" to a3)
18+
field(_alias, "person", PersonProjection(inputValueSerializer), _projection, "a1" to a1, "a2" to
19+
a2, "a3" to a3)
2020
return this
2121
}
2222
}

graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin2/GenerateKotlin2ClientTypes.kt

+17-11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.netflix.graphql.dgs.codegen.generators.shared.SchemaExtensionsUtils
2828
import com.netflix.graphql.dgs.codegen.generators.shared.excludeSchemaTypeExtension
2929
import com.netflix.graphql.dgs.codegen.shouldSkip
3030
import com.squareup.kotlinpoet.ClassName
31+
import com.squareup.kotlinpoet.CodeBlock
3132
import com.squareup.kotlinpoet.FileSpec
3233
import com.squareup.kotlinpoet.FunSpec
3334
import com.squareup.kotlinpoet.LambdaTypeName
@@ -105,12 +106,13 @@ fun generateKotlin2ClientTypes(
105106
FunSpec.builder(field.name)
106107
.addInputArgs(config, typeLookup, typeName, field.inputValueDefinitions)
107108
.returns(typeName)
108-
.addStatement(
109-
"""field(%S%L)""",
110-
field.name,
111-
field.inputValueDefinitions.joinToString(" ") { """, "${it.name}" to ${it.name}""" }
109+
.addCode(
110+
field.inputValueDefinitions.let { iv ->
111+
val builder = CodeBlock.builder().add("field(%S", field.name)
112+
iv.forEach { d -> builder.add(", %S to %N", d.name, d.name) }
113+
builder.add(")\nreturn this").build()
114+
}
112115
)
113-
.addStatement("return this")
114116
.build()
115117
}
116118

@@ -125,13 +127,17 @@ fun generateKotlin2ClientTypes(
125127
.addParameter(ParameterSpec.builder("_alias", String::class.asTypeName().copy(nullable = true)).defaultValue("null").build())
126128
.addParameter(projection)
127129
.returns(typeName)
128-
.addStatement(
129-
"""field(_alias, %S, %T(inputValueSerializer), _projection%L)""",
130-
field.name,
131-
projectionType,
132-
field.inputValueDefinitions.joinToString(" ") { """, "${it.name}" to ${it.name}""" }
130+
.addCode(
131+
field.inputValueDefinitions.let { iv ->
132+
val builder = CodeBlock.builder().add(
133+
"field(_alias, %S, %T(inputValueSerializer), _projection",
134+
field.name,
135+
projectionType
136+
)
137+
iv.forEach { d -> builder.add(", %S to %N", d.name, d.name) }
138+
builder.add(")\nreturn this").build()
139+
}
133140
)
134-
.addStatement("return this")
135141
.build()
136142
}
137143
}

0 commit comments

Comments
 (0)