Skip to content

Commit

Permalink
Reuse semanticNonNull directive definition across files
Browse files Browse the repository at this point in the history
  • Loading branch information
XiNiHa committed Apr 23, 2024
1 parent e0fd730 commit a84bbfe
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ trait CommonSchemaDerivation[R] {
Option(
p.annotations.collect { case GQLDirective(dir) => dir }.toList ++ {
if (enableSemanticNonNull && !isNullable && p.typeclass.canFail)
Some(Directive("semanticNonNull"))
Some(SchemaUtils.SemanticNonNull)
else None
}
).filter(_.nonEmpty)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala-3/caliban/schema/DerivationUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private object DerivationUtils {
deprecatedReason,
Option(
getDirectives(fieldAnnotations) ++ {
if (enableSemanticNonNull && !isNullable && schema.canFail) Some(Directive("semanticNonNull"))
if (enableSemanticNonNull && !isNullable && schema.canFail) Some(SchemaUtils.SemanticNonNull)
else None
}
).filter(_.nonEmpty)
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/caliban/schema/SchemaUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import caliban.ResponseValue.ObjectValue
import caliban.Value.{ EnumValue, NullValue, StringValue }
import caliban.introspection.adt.{ __DeprecatedArgs, __Field, __Type }
import caliban.schema.Step.MetadataFunctionStep
import caliban.parsing.adt.Directive

private[schema] object SchemaUtils {
/**
* Directive used to mark a field as semantically non-nullable.
*/
val SemanticNonNull = Directive("semanticNonNull")

private val fakeField =
Some(
List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object SemanticNonNullSchemaSpec extends ZIOSpecDefault {
hasField[__Field, Option[List[Directive]]](
"directives",
_.directives,
isSome(contains((Directive("semanticNonNull"))))
isSome(contains(SchemaUtils.SemanticNonNull))
)
)
)
Expand All @@ -31,7 +31,7 @@ object SemanticNonNullSchemaSpec extends ZIOSpecDefault {
isSome(
hasField[__Field, Option[List[Directive]]](
"directives",
_.directives.map(_.filter(_.name == "semanticNonNull")).filter(_.nonEmpty),
_.directives.map(_.filter(_ == SchemaUtils.SemanticNonNull)).filter(_.nonEmpty),
isNone
)
)
Expand Down

0 comments on commit a84bbfe

Please sign in to comment.