Skip to content

Commit d8d3da7

Browse files
committed
Wrap enableSemanticNonNull option with a config object
1 parent 85e67f1 commit d8d3da7

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ import scala.language.experimental.macros
1111

1212
trait CommonSchemaDerivation[R] {
1313

14+
case class DerivationConfig(
15+
/**
16+
* Whether to enable the `SemanticNonNull` feature on derivation.
17+
* It is currently disabled by default since it is not yet stable.
18+
*/
19+
enableSemanticNonNull: Boolean = false
20+
)
21+
1422
/**
15-
* Enables the `SemanticNonNull` feature on derivation.
16-
* It is currently disabled by default since it is not yet stable.
23+
* Returns a configuration object that can be used to customize the derivation behavior.
1724
*
18-
* Override this method and return `true` to enable the feature.
25+
* Override this method to customize the configuration.
1926
*/
20-
def enableSemanticNonNull: Boolean = false
27+
def config: DerivationConfig = DerivationConfig()
2128

2229
/**
2330
* Default naming logic for input types.
@@ -105,7 +112,7 @@ trait CommonSchemaDerivation[R] {
105112
p.annotations.collectFirst { case GQLDeprecated(reason) => reason },
106113
Option(
107114
p.annotations.collect { case GQLDirective(dir) => dir }.toList ++ {
108-
if (enableSemanticNonNull && !isNullable && p.typeclass.canFail)
115+
if (config.enableSemanticNonNull && !isNullable && p.typeclass.canFail)
109116
Some(SchemaUtils.SemanticNonNull)
110117
else None
111118
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ object PrintDerived {
2323
trait CommonSchemaDerivation {
2424
export DerivationUtils.customizeInputTypeName
2525

26+
case class DerivationConfig(
27+
/**
28+
* Whether to enable the `SemanticNonNull` feature on derivation.
29+
* It is currently disabled by default since it is not yet stable.
30+
*/
31+
enableSemanticNonNull: Boolean = false
32+
)
33+
2634
/**
27-
* Enables the `SemanticNonNull` feature on derivation.
28-
* It is currently disabled by default since it is not yet stable.
29-
*
30-
* Override this method and return `true` to enable the feature.
31-
*/
32-
def enableSemanticNonNull: Boolean = false
35+
* Returns a configuration object that can be used to customize the derivation behavior.
36+
*
37+
* Override this method to customize the configuration.
38+
*/
39+
def config: DerivationConfig = DerivationConfig()
3340

3441
inline def recurseSum[R, P, Label, A <: Tuple](
3542
inline types: List[(String, __Type, List[Any])] = Nil,
@@ -104,7 +111,7 @@ trait CommonSchemaDerivation {
104111
MagnoliaMacro.typeInfo[A],
105112
// Workaround until we figure out why the macro uses the parent's annotations when the leaf is a Scala 3 enum
106113
inline if (!MagnoliaMacro.isEnum[A]) MagnoliaMacro.anns[A] else Nil,
107-
enableSemanticNonNull
114+
config.enableSemanticNonNull
108115
)
109116
case _ if Macros.hasAnnotation[A, GQLValueType] =>
110117
new ValueTypeSchema[R, A](
@@ -119,7 +126,7 @@ trait CommonSchemaDerivation {
119126
MagnoliaMacro.typeInfo[A],
120127
MagnoliaMacro.anns[A],
121128
MagnoliaMacro.paramAnns[A].toMap,
122-
enableSemanticNonNull
129+
config.enableSemanticNonNull
123130
)(using summonInline[ClassTag[A]])
124131
}
125132

core/src/test/scala/caliban/schema/SemanticNonNullSchemaSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import zio.test.Assertion._
99
import zio.test._
1010

1111
object SemanticNonNullSchema extends SchemaDerivation[Any] {
12-
override def enableSemanticNonNull: Boolean = true
12+
override def config = DerivationConfig(enableSemanticNonNull = true)
1313
}
1414

1515
object SemanticNonNullSchemaSpec extends ZIOSpecDefault {

0 commit comments

Comments
 (0)