Skip to content

Commit

Permalink
Merge pull request #700 from Netflix/feature/escape-projection-args
Browse files Browse the repository at this point in the history
Escape projection argument names in kotlin2
  • Loading branch information
mbossenbroek authored Jun 24, 2024
2 parents 08eb4ad + d252f8a commit 48e2b3b
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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<PersonProjection, String?>("package")):
PersonProjection {
field("info", "package" to `package`)
return this
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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`,
)
}
}
Original file line number Diff line number Diff line change
@@ -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<Person?>? = peopleDefault,
) {
private val __people: () -> List<Person?>? = people

@get:JvmName("getPeople")
public val people: List<Person?>?
get() = __people.invoke()

public companion object {
private val peopleDefault: () -> List<Person?>? =
{ throw IllegalStateException("Field `people` was not requested") }

}

@JsonPOJOBuilder
@JsonIgnoreProperties("__typename")
public class Builder {
private var people: () -> List<Person?>? = peopleDefault

@JsonProperty("people")
public fun withPeople(people: List<Person?>?): Builder = this.apply {
this.people = { people }
}

public fun build(): Query = Query(
people = people,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
type SampleType {
return: String!
}

type Query {
people: [Person]
}

type Person {
info(package: String): String
interface: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class QueryProjection(
) : GraphQLProjection(inputValueSerializer) {
public fun q1(arg1: String? = default<QueryProjection, String?>("arg1"), arg2: I2? =
default<QueryProjection, I2?>("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<QueryProjection, I1?>("arg1"), arg2: String? =
default<QueryProjection, String?>("arg2")): QueryProjection {
field("q2", "arg1" to arg1 , "arg2" to arg2)
field("q2", "arg1" to arg1, "arg2" to arg2)
return this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class QueryProjection(
a2: String,
a3: I? = default<QueryProjection, I?>("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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

Expand All @@ -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()
}
}
Expand Down

0 comments on commit 48e2b3b

Please sign in to comment.