Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class ClientApiGenerator internal constructor(
) {
constructor(config: CodeGenConfig, document: Document) : this(config, SchemaIndex(document))

private val document = schemaIndex.document
private val generatedClasses = mutableSetOf<String>()
private val typeUtils = TypeUtils(getDatatypesPackageName(), config, schemaIndex)
private val javaReservedKeywordSanitizer = JavaReservedKeywordSanitizer()
Expand Down Expand Up @@ -412,37 +411,37 @@ class ClientApiGenerator internal constructor(
originalMethodName.plus("GraphQLQuery")
}

private fun createProjectionClass(clazzName: String): TypeSpec.Builder {
val baseProjectionClass = ClassName.get(BaseSubProjectionNode::class.java)
val baseProjectionType =
ParameterizedTypeName.get(baseProjectionClass, TypeVariableName.get("?"), TypeVariableName.get("?"))
val parentType = TypeVariableName.get("PARENT").withBounds(baseProjectionType)
val rootType = TypeVariableName.get("ROOT").withBounds(baseProjectionType)

return TypeSpec
.classBuilder(clazzName)
.addOptionalGeneratedAnnotation(config)
.addTypeVariable(parentType)
.addTypeVariable(rootType)
.addModifiers(Modifier.PUBLIC)
.superclass(ParameterizedTypeName.get(baseProjectionClass, TypeVariableName.get("PARENT"), TypeVariableName.get("ROOT")))
}

private fun createRootProjectionConstructor(typeName: String): MethodSpec =
MethodSpec
.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addCode("""super(null, null, java.util.Optional.of("$typeName"));""")
.build()

private fun createRootProjection(
type: TypeDefinition<*>,
prefix: String,
): CodeGenResult {
val clazzName = "${prefix}ProjectionRoot"
val className = ClassName.get(BaseSubProjectionNode::class.java)
val parentJavaType =
TypeVariableName
.get(
"PARENT",
).withBounds(ParameterizedTypeName.get(className, TypeVariableName.get("?"), TypeVariableName.get("?")))
val rootJavaType =
TypeVariableName
.get(
"ROOT",
).withBounds(ParameterizedTypeName.get(className, TypeVariableName.get("?"), TypeVariableName.get("?")))
val javaType =
TypeSpec
.classBuilder(clazzName)
.addOptionalGeneratedAnnotation(config)
.addTypeVariable(parentJavaType)
.addTypeVariable(rootJavaType)
.addModifiers(Modifier.PUBLIC)
.superclass(ParameterizedTypeName.get(className, TypeVariableName.get("PARENT"), TypeVariableName.get("ROOT")))
.addMethod(
MethodSpec
.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addCode("""super(null, null, java.util.Optional.of("${type.name}"));""")
.build(),
)
createProjectionClass(clazzName)
.addMethod(createRootProjectionConstructor(type.name))

val typeVariable = TypeVariableName.get("$clazzName<PARENT, ROOT>")
javaType.addMethod(
Expand Down Expand Up @@ -617,32 +616,9 @@ class ClientApiGenerator internal constructor(

private fun createEntitiesRootProjection(federatedTypes: List<ObjectTypeDefinition>): CodeGenResult {
val clazzName = "EntitiesProjectionRoot"
val className = ClassName.get(BaseSubProjectionNode::class.java)
val parentType =
TypeVariableName
.get(
"PARENT",
).withBounds(ParameterizedTypeName.get(className, TypeVariableName.get("?"), TypeVariableName.get("?")))
val rootType =
TypeVariableName
.get(
"ROOT",
).withBounds(ParameterizedTypeName.get(className, TypeVariableName.get("?"), TypeVariableName.get("?")))
val javaType =
TypeSpec
.classBuilder(clazzName)
.addOptionalGeneratedAnnotation(config)
.addTypeVariable(parentType)
.addTypeVariable(rootType)
.addModifiers(Modifier.PUBLIC)
.superclass(ParameterizedTypeName.get(className, TypeVariableName.get("PARENT"), TypeVariableName.get("ROOT")))
.addMethod(
MethodSpec
.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addCode("""super(null, null, java.util.Optional.of("${"_entities"}"));""")
.build(),
)
createProjectionClass(clazzName)
.addMethod(createRootProjectionConstructor("_entities"))

if (generatedClasses.contains(clazzName)) return CodeGenResult.EMPTY else generatedClasses.add(clazzName)

Expand Down Expand Up @@ -802,28 +778,11 @@ class ClientApiGenerator internal constructor(
root: TypeSpec,
prefix: String,
): Pair<TypeSpec.Builder, CodeGenResult>? {
val className = ClassName.get(BaseSubProjectionNode::class.java)
val clazzName = "${prefix}Projection"
if (generatedClasses.contains(clazzName)) return null else generatedClasses.add(clazzName)

val parentJavaType =
TypeVariableName
.get(
"PARENT",
).withBounds(ParameterizedTypeName.get(className, TypeVariableName.get("?"), TypeVariableName.get("?")))
val rootJavaType =
TypeVariableName
.get(
"ROOT",
).withBounds(ParameterizedTypeName.get(className, TypeVariableName.get("?"), TypeVariableName.get("?")))
val javaType =
TypeSpec
.classBuilder(clazzName)
.addOptionalGeneratedAnnotation(config)
.addTypeVariable(parentJavaType)
.addTypeVariable(rootJavaType)
.addModifiers(Modifier.PUBLIC)
.superclass(ParameterizedTypeName.get(className, TypeVariableName.get("PARENT"), TypeVariableName.get("ROOT")))
createProjectionClass(clazzName)
.addMethod(
MethodSpec
.constructorBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ open class InputValueSerializer(
val objectFields =
propertyValues
.asSequence()
.filter { (_, value) -> value != null }
.filter { (_, value) -> shouldSerializeProperty(value) }
.map { (name, value) -> ObjectField(InputReservedKeywordSanitizer().desanitize(name), toValue(value)) }
.toList()
return ObjectValue
Expand All @@ -88,6 +88,8 @@ open class InputValueSerializer(
.build()
}

protected open fun shouldSerializeProperty(value: Any?): Boolean = value != null

protected fun getOptionalValue(input: Any): Optional<Value<*>> {
if (input is Value<*>) {
return Optional.of(input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,10 @@

package com.netflix.graphql.dgs.client.codegen

import graphql.language.NullValue
import graphql.language.ObjectField
import graphql.language.ObjectValue
import graphql.language.Value
import graphql.schema.Coercing
import kotlin.reflect.full.allSuperclasses

class NullableInputValueSerializer(
scalars: Map<Class<*>, Coercing<*, *>> = emptyMap(),
) : InputValueSerializer(scalars) {
override fun toValue(input: Any?): Value<*> {
Comment thread
iuliiasobolevska marked this conversation as resolved.
if (input == null) {
return NullValue.newNullValue().build()
}

val optionalValue = getOptionalValue(input)

if (optionalValue.isPresent) {
return optionalValue.get()
}

val classes = (sequenceOf(input::class) + input::class.allSuperclasses.asSequence()) - Any::class
val propertyValues = getPropertyValues(classes, input)

val objectFields =
propertyValues
.asSequence()
.map { (name, value) -> ObjectField(InputReservedKeywordSanitizer().desanitize(name), toValue(value)) }
.toList()
return ObjectValue
.newObjectValue()
.objectFields(objectFields)
.build()
}
override fun shouldSerializeProperty(value: Any?): Boolean = true
}
Loading