Skip to content

Commit

Permalink
Clear nullability determination logic
Browse files Browse the repository at this point in the history
  • Loading branch information
XiNiHa committed May 11, 2024
1 parent 6fd85ce commit 82662cb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
13 changes: 8 additions & 5 deletions core/src/main/scala-2/caliban/schema/SchemaDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 9 additions & 5 deletions core/src/main/scala-3/caliban/schema/DerivationUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,28 @@ private object DerivationUtils {
Some(getName(annotations, info)),
getDescription(annotations),
fields.map { (name, fieldAnnotations, schema) =>
val deprecatedReason = getDeprecatedReason(fieldAnnotations)
val (isNullable, isNullabilityForced) = {
val deprecatedReason = getDeprecatedReason(fieldAnnotations)
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)
Expand Down

0 comments on commit 82662cb

Please sign in to comment.