Skip to content

Commit

Permalink
Wrap enableSemanticNonNull option with a config object
Browse files Browse the repository at this point in the history
  • Loading branch information
XiNiHa committed Apr 24, 2024
1 parent 85e67f1 commit 7d8e546
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
17 changes: 12 additions & 5 deletions core/src/main/scala-2/caliban/schema/SchemaDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ import scala.language.experimental.macros

trait CommonSchemaDerivation[R] {

case class DerivationConfig(
/**
* Whether to enable the `SemanticNonNull` feature on derivation.
* It is currently disabled by default since it is not yet stable.
*/
enableSemanticNonNull: Boolean = false
)

/**
* Enables the `SemanticNonNull` feature on derivation.
* It is currently disabled by default since it is not yet stable.
* Returns a configuration object that can be used to customize the derivation behavior.
*
* Override this method and return `true` to enable the feature.
* Override this method to customize the configuration.
*/
def enableSemanticNonNull: Boolean = false
def config: DerivationConfig = DerivationConfig()

/**
* Default naming logic for input types.
Expand Down Expand Up @@ -105,7 +112,7 @@ trait CommonSchemaDerivation[R] {
p.annotations.collectFirst { case GQLDeprecated(reason) => reason },
Option(
p.annotations.collect { case GQLDirective(dir) => dir }.toList ++ {
if (enableSemanticNonNull && !isNullable && p.typeclass.canFail)
if (config.enableSemanticNonNull && !isNullable && p.typeclass.canFail)
Some(SchemaUtils.SemanticNonNull)
else None
}
Expand Down
19 changes: 13 additions & 6 deletions core/src/main/scala-3/caliban/schema/SchemaDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ object PrintDerived {
trait CommonSchemaDerivation {
export DerivationUtils.customizeInputTypeName

case class DerivationConfig(
/**
* Whether to enable the `SemanticNonNull` feature on derivation.
* It is currently disabled by default since it is not yet stable.
*/
enableSemanticNonNull: Boolean = false
)

/**
* Enables the `SemanticNonNull` feature on derivation.
* It is currently disabled by default since it is not yet stable.
* Returns a configuration object that can be used to customize the derivation behavior.
*
* Override this method and return `true` to enable the feature.
* Override this method to customize the configuration.
*/
def enableSemanticNonNull: Boolean = false
def config: DerivationConfig = DerivationConfig()

inline def recurseSum[R, P, Label, A <: Tuple](
inline types: List[(String, __Type, List[Any])] = Nil,
Expand Down Expand Up @@ -104,7 +111,7 @@ trait CommonSchemaDerivation {
MagnoliaMacro.typeInfo[A],
// Workaround until we figure out why the macro uses the parent's annotations when the leaf is a Scala 3 enum
inline if (!MagnoliaMacro.isEnum[A]) MagnoliaMacro.anns[A] else Nil,
enableSemanticNonNull
config.enableSemanticNonNull
)
case _ if Macros.hasAnnotation[A, GQLValueType] =>
new ValueTypeSchema[R, A](
Expand All @@ -119,7 +126,7 @@ trait CommonSchemaDerivation {
MagnoliaMacro.typeInfo[A],
MagnoliaMacro.anns[A],
MagnoliaMacro.paramAnns[A].toMap,
enableSemanticNonNull
config.enableSemanticNonNull
)(using summonInline[ClassTag[A]])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import zio.test.Assertion._
import zio.test._

object SemanticNonNullSchema extends SchemaDerivation[Any] {
override def enableSemanticNonNull: Boolean = true
override def config = DerivationConfig(enableSemanticNonNull = true)
}

object SemanticNonNullSchemaSpec extends ZIOSpecDefault {
Expand Down

0 comments on commit 7d8e546

Please sign in to comment.