From e4878c46c0bbfc4959b060f290ac883e0f1ada24 Mon Sep 17 00:00:00 2001 From: XiNiHa Date: Sat, 11 May 2024 18:08:52 +0900 Subject: [PATCH] Clear nullability determination logic --- .../scala-2/caliban/schema/SchemaDerivation.scala | 13 ++++++++----- .../scala-3/caliban/schema/DerivationUtils.scala | 12 ++++++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala b/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala index a138a556b..ef87048eb 100644 --- a/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala +++ b/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala @@ -95,24 +95,27 @@ trait CommonSchemaDerivation[R] { ctx.parameters .filterNot(_.annotations.exists(_ == GQLExcluded())) .map { p => - val (isNullable, isNullabilityForced) = { + val (isNullable, isSemanticNonNull) = { val hasNullableAnn = p.annotations.contains(GQLNullable()) val hasNonNullAnn = p.annotations.contains(GQLNonNullable()) - (!hasNonNullAnn && (hasNullableAnn || p.typeclass.nullable), hasNullableAnn || hasNonNullAnn) + + if (hasNonNullAnn) (false, false) + else if (hasNullableAnn || p.typeclass.nullable) (true, false) + else if (p.typeclass.canFail) (true, true) + else (false, false) } Types.makeField( getName(p), getDescription(p), p.typeclass.arguments, () => - if (isNullable || (!isNullabilityForced && p.typeclass.canFail)) - p.typeclass.toType_(isInput, isSubscription) + if (isNullable) p.typeclass.toType_(isInput, isSubscription) else p.typeclass.toType_(isInput, isSubscription).nonNull, p.annotations.collectFirst { case GQLDeprecated(_) => () }.isDefined, p.annotations.collectFirst { case GQLDeprecated(reason) => reason }, Option( p.annotations.collect { case GQLDirective(dir) => dir }.toList ++ { - if (config.enableSemanticNonNull && !isNullable && p.typeclass.canFail) + if (config.enableSemanticNonNull && isSemanticNonNull) Some(SchemaUtils.SemanticNonNull) else None } diff --git a/core/src/main/scala-3/caliban/schema/DerivationUtils.scala b/core/src/main/scala-3/caliban/schema/DerivationUtils.scala index 853c26ca2..77f4e138f 100644 --- a/core/src/main/scala-3/caliban/schema/DerivationUtils.scala +++ b/core/src/main/scala-3/caliban/schema/DerivationUtils.scala @@ -127,23 +127,27 @@ private object DerivationUtils { getDescription(annotations), fields.map { (name, fieldAnnotations, schema) => val deprecatedReason = getDeprecatedReason(fieldAnnotations) - val (isNullable, isNullabilityForced) = { + val (isNullable, isSemanticNonNull) = { val hasNullableAnn = fieldAnnotations.contains(GQLNullable()) val hasNonNullAnn = fieldAnnotations.contains(GQLNonNullable()) - (!hasNonNullAnn && (hasNullableAnn || schema.nullable), hasNullableAnn || hasNonNullAnn) + + if (hasNonNullAnn) (false, false) + else if (hasNullableAnn || schema.nullable) (true, false) + else if (schema.canFail) (true, true) + else (false, false) } Types.makeField( name, getDescription(fieldAnnotations), schema.arguments, () => - if (isNullable || (!isNullabilityForced && schema.canFail)) schema.toType_(isInput, isSubscription) + if (isNullable) schema.toType_(isInput, isSubscription) else schema.toType_(isInput, isSubscription).nonNull, deprecatedReason.isDefined, deprecatedReason, Option( getDirectives(fieldAnnotations) ++ { - if (enableSemanticNonNull && !isNullable && schema.canFail) Some(SchemaUtils.SemanticNonNull) + if (enableSemanticNonNull && isSemanticNonNull) Some(SchemaUtils.SemanticNonNull) else None } ).filter(_.nonEmpty)