Skip to content

Commit f6bf6f5

Browse files
XiNiHaghostdogpr
andauthored
Add docs for SemanticNonNull support (#2229)
* Add docs for SemanticNonNull support * Update vuepress/docs/docs/schema.md Co-authored-by: Pierre Ricadat <[email protected]> --------- Co-authored-by: Pierre Ricadat <[email protected]>
1 parent b81453b commit f6bf6f5

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

vuepress/docs/docs/schema.md

+80
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,86 @@ explicit constructor available under the `ArgBuilder` companion object. For inst
452452
to handle instants which are encoded using a `Long` from the standard java epoch time (January 1st 1970 00:00:00).
453453
For some time formats you can also specify a specific `DateTimeFormatter` to handle your particular date time needs.
454454

455+
## Using features that are disabled by default
456+
457+
Some features of Caliban's schema derivation are disabled by default.
458+
To enable them, you need to declare a custom schema derivation object like this:
459+
460+
<code-group>
461+
<code-block title="Scala 2" active>
462+
463+
```scala
464+
import caliban.schema.SchemaDerivation
465+
466+
object MySchemaDerivation extends SchemaDerivation[Any] {
467+
override def config = DerivationConfig(
468+
// add your config overrides here
469+
enableSemanticNonNull = true
470+
)
471+
}
472+
473+
case class MyClass(field: String)
474+
475+
// use the custom schema derivation defined above
476+
implicit val schemaForMyClass: Schema[Any, MyClass] = MySchemaDerivation.gen
477+
```
478+
</code-block>
479+
<code-block title="Scala 3 (with given)">
480+
481+
```scala
482+
import caliban.schema.SchemaDerivation
483+
484+
object MySchemaDerivation extends SchemaDerivation[Any] {
485+
override def config = DerivationConfig(
486+
// add your config overrides here
487+
enableSemanticNonNull = true
488+
)
489+
}
490+
491+
case class MyClass(field: String)
492+
493+
// use the custom schema derivation defined above
494+
given Schema[Any, MyClass] = MySchemaDerivation.gen
495+
```
496+
</code-block>
497+
<code-block title="Scala 3 (with derives)">
498+
499+
```scala
500+
import caliban.schema.{ CommonSchemaDerivation, Schema }
501+
502+
trait MySchemaDerivation[R] extends CommonSchemaDerivation {
503+
override def config = DerivationConfig(
504+
// add your config overrides here
505+
enableSemanticNonNull = true
506+
)
507+
508+
final class SemiAuto[A](impl: Schema[R, A]) extends Schema[R, A] {
509+
export impl.*
510+
}
511+
512+
object SemiAuto {
513+
inline def derived[A]: SemiAuto[A] = new SemiAuto[A](MySchemaDerivation.derived[R, A])
514+
}
515+
}
516+
517+
object MySchemaDerivation extends MySchemaDerivation[Any]
518+
519+
case class MyClass(field: String) derives MySchemaDerivation.SemiAuto
520+
```
521+
</code-block>
522+
</code-group>
523+
524+
### SemanticNonNull support
525+
526+
Caliban supports deriving schemas to the form that supports [the SemanticNonNull type RFC](https://github.com/graphql/graphql-spec/pull/1065), by introducing the `@semanticNonNull` directive.
527+
While Caliban resolves all fallible effectful types (`ZIO[R, Throwable, A]`, ...) as nullable by default,
528+
with the feature enabled, fields that don't get resolved to nullable types (for example, `ZIO[R, Throwable, A]` where `A` is not `Option[A]`, ...)
529+
will be marked with `@semanticNonNull` to express that the field never returns `null` unless the effect fails.
530+
`@GQLNullable` annotation can be used to override this behavior per field.
531+
532+
If you have custom types that override the `Schema` trait, make sure to override `nullable` and `canFail` methods to return the correct values.
533+
All types that return `false` for `nullable` and `true` for `canFail` will be treated as semantically non-nullable.
534+
455535
## Building Schemas by hand
456536

457537
Sometimes for whatever reason schema generation fails. This can happen if your schema has co-recursive types and derivation is unable

0 commit comments

Comments
 (0)