Skip to content

Commit 82662cb

Browse files
committed
Clear nullability determination logic
1 parent 6fd85ce commit 82662cb

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

core/src/main/scala-2/caliban/schema/SchemaDerivation.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,27 @@ trait CommonSchemaDerivation[R] {
9595
ctx.parameters
9696
.filterNot(_.annotations.exists(_ == GQLExcluded()))
9797
.map { p =>
98-
val (isNullable, isNullabilityForced) = {
98+
val (isNullable, isSemanticNonNull) = {
9999
val hasNullableAnn = p.annotations.contains(GQLNullable())
100100
val hasNonNullAnn = p.annotations.contains(GQLNonNullable())
101-
(!hasNonNullAnn && (hasNullableAnn || p.typeclass.nullable), hasNullableAnn || hasNonNullAnn)
101+
102+
if (hasNonNullAnn) (false, false)
103+
else if (hasNullableAnn || p.typeclass.nullable) (true, false)
104+
else if (p.typeclass.canFail) (true, true)
105+
else (false, false)
102106
}
103107
Types.makeField(
104108
getName(p),
105109
getDescription(p),
106110
p.typeclass.arguments,
107111
() =>
108-
if (isNullable || (!isNullabilityForced && p.typeclass.canFail))
109-
p.typeclass.toType_(isInput, isSubscription)
112+
if (isNullable) p.typeclass.toType_(isInput, isSubscription)
110113
else p.typeclass.toType_(isInput, isSubscription).nonNull,
111114
p.annotations.collectFirst { case GQLDeprecated(_) => () }.isDefined,
112115
p.annotations.collectFirst { case GQLDeprecated(reason) => reason },
113116
Option(
114117
p.annotations.collect { case GQLDirective(dir) => dir }.toList ++ {
115-
if (config.enableSemanticNonNull && !isNullable && p.typeclass.canFail)
118+
if (config.enableSemanticNonNull && isSemanticNonNull)
116119
Some(SchemaUtils.SemanticNonNull)
117120
else None
118121
}

core/src/main/scala-3/caliban/schema/DerivationUtils.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,28 @@ private object DerivationUtils {
126126
Some(getName(annotations, info)),
127127
getDescription(annotations),
128128
fields.map { (name, fieldAnnotations, schema) =>
129-
val deprecatedReason = getDeprecatedReason(fieldAnnotations)
130-
val (isNullable, isNullabilityForced) = {
129+
val deprecatedReason = getDeprecatedReason(fieldAnnotations)
130+
val (isNullable, isSemanticNonNull) = {
131131
val hasNullableAnn = fieldAnnotations.contains(GQLNullable())
132132
val hasNonNullAnn = fieldAnnotations.contains(GQLNonNullable())
133-
(!hasNonNullAnn && (hasNullableAnn || schema.nullable), hasNullableAnn || hasNonNullAnn)
133+
134+
if (hasNonNullAnn) (false, false)
135+
else if (hasNullableAnn || schema.nullable) (true, false)
136+
else if (schema.canFail) (true, true)
137+
else (false, false)
134138
}
135139
Types.makeField(
136140
name,
137141
getDescription(fieldAnnotations),
138142
schema.arguments,
139143
() =>
140-
if (isNullable || (!isNullabilityForced && schema.canFail)) schema.toType_(isInput, isSubscription)
144+
if (isNullable) schema.toType_(isInput, isSubscription)
141145
else schema.toType_(isInput, isSubscription).nonNull,
142146
deprecatedReason.isDefined,
143147
deprecatedReason,
144148
Option(
145149
getDirectives(fieldAnnotations) ++ {
146-
if (enableSemanticNonNull && !isNullable && schema.canFail) Some(SchemaUtils.SemanticNonNull)
150+
if (enableSemanticNonNull && isSemanticNonNull) Some(SchemaUtils.SemanticNonNull)
147151
else None
148152
}
149153
).filter(_.nonEmpty)

0 commit comments

Comments
 (0)