Skip to content

Commit

Permalink
Kotlin2: Update projection name to be derived from GQL name instead o…
Browse files Browse the repository at this point in the history
…f kotlin name. This enables support for generic classes to be used in the type mapping.
  • Loading branch information
mbossenbroek committed Jan 8, 2024
1 parent 99722d7 commit aefe5fd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class Kotlin2CodeGenTest {
"dataClassWithMappedTypes" -> mapOf(
"Long" to "kotlin.Long",
"DateTime" to "java.time.OffsetDateTime",
"PageInfo" to "graphql.relay.PageInfo"
"PageInfo" to "graphql.relay.PageInfo",
"EntityConnection" to "graphql.relay.SimpleListConnection<com.netflix.graphql.dgs.codegen.cases.dataClassWithMappedTypes.expected.types.EntityEdge>"
)
"dataClassWithMappedInterfaces" -> mapOf(
"Node" to "com.netflix.graphql.dgs.codegen.fixtures.Node"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 graphql.relay.SimpleListConnection
import java.lang.IllegalStateException
import kotlin.collections.List
import kotlin.jvm.JvmName
Expand All @@ -13,26 +14,26 @@ import kotlin.jvm.JvmName
@JsonDeserialize(builder = Query.Builder::class)
public class Query(
entity: () -> List<Entity?>? = entityDefault,
entityConnection: () -> EntityConnection? = entityConnectionDefault,
entityConnection: () -> SimpleListConnection<EntityEdge>? = entityConnectionDefault,
) {
private val _entity: () -> List<Entity?>? = entity

private val _entityConnection: () -> EntityConnection? = entityConnection
private val _entityConnection: () -> SimpleListConnection<EntityEdge>? = entityConnection

@get:JvmName("getEntity")
public val entity: List<Entity?>?
get() = _entity.invoke()

@get:JvmName("getEntityConnection")
public val entityConnection: EntityConnection?
public val entityConnection: SimpleListConnection<EntityEdge>?
get() = _entityConnection.invoke()

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


private val entityConnectionDefault: () -> EntityConnection? =
private val entityConnectionDefault: () -> SimpleListConnection<EntityEdge>? =
{ throw IllegalStateException("Field `entityConnection` was not requested") }

}
Expand All @@ -42,15 +43,16 @@ public class Query(
public class Builder {
private var entity: () -> List<Entity?>? = entityDefault

private var entityConnection: () -> EntityConnection? = entityConnectionDefault
private var entityConnection: () -> SimpleListConnection<EntityEdge>? = entityConnectionDefault

@JsonProperty("entity")
public fun withEntity(entity: List<Entity?>?): Builder = this.apply {
this.entity = { entity }
}

@JsonProperty("entityConnection")
public fun withEntityConnection(entityConnection: EntityConnection?): Builder = this.apply {
public fun withEntityConnection(entityConnection: SimpleListConnection<EntityEdge>?): Builder =
this.apply {
this.entityConnection = { entityConnection }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ import com.squareup.kotlinpoet.asTypeName
import graphql.language.Document
import graphql.language.InputValueDefinition
import graphql.language.InterfaceTypeDefinition
import graphql.language.ListType
import graphql.language.NamedNode
import graphql.language.NonNullType
import graphql.language.ObjectTypeDefinition
import graphql.language.Type
import graphql.language.UnionTypeDefinition

fun generateKotlin2ClientTypes(
Expand Down Expand Up @@ -110,9 +113,8 @@ fun generateKotlin2ClientTypes(
// otherwise it's a projection with optional args
// !isScalar && hasArgs
else -> {
val projectTypeName =
projectionTypeName(typeLookup.findReturnType(config.packageNameClient, field.type))
val (projectionType, projection) = projectionType(config.packageNameClient, projectTypeName)
val projectionTypeName = projectionTypeName(field.type)
val (projectionType, projection) = projectionType(config.packageNameClient, projectionTypeName)

FunSpec.builder(field.name)
.addInputArgs(config, typeLookup, typeName, field.inputValueDefinitions)
Expand Down Expand Up @@ -213,11 +215,12 @@ fun generateKotlin2ClientTypes(
}

// unpack the type to get the underlying type of the projection
private fun projectionTypeName(type: TypeName): String {
private fun projectionTypeName(type: Type<*>): String {
return when (type) {
is ClassName -> type.simpleName
is ParameterizedTypeName -> projectionTypeName(type.typeArguments.first())
else -> throw UnsupportedOperationException(type::class.simpleName)
is graphql.language.TypeName -> type.name
is ListType -> projectionTypeName(type.type)
is NonNullType -> projectionTypeName(type.type)
else -> throw UnsupportedOperationException(type::class.qualifiedName)
}
}

Expand Down

0 comments on commit aefe5fd

Please sign in to comment.