From 82ff5a28b786252c7a21affdbad54bcc5ee8532c Mon Sep 17 00:00:00 2001 From: XiNiHa Date: Mon, 13 May 2024 03:17:35 +0900 Subject: [PATCH 1/2] Add docs for SemanticNonNull support --- vuepress/docs/docs/schema.md | 80 ++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/vuepress/docs/docs/schema.md b/vuepress/docs/docs/schema.md index 8c1dbc365..a4e98ba45 100644 --- a/vuepress/docs/docs/schema.md +++ b/vuepress/docs/docs/schema.md @@ -461,6 +461,86 @@ explicit constructor available under the `ArgBuilder` companion object. For inst to handle instants which are encoded using a `Long` from the standard java epoch time (January 1st 1970 00:00:00). For some time formats you can also specify a specific `DateTimeFormatter` to handle your particular date time needs. +## Using features that are disabled by default + +Some features of Caliban's schema derivation are disabled by default. +To enable them, you need to declare a custom schema derivation object like this: + + + + +```scala +import caliban.schema.SchemaDerivation + +object MySchemaDerivation extends SchemaDerivation[Any] { + override def config = DerivationConfig( + // add your config overrides here + enableSemanticNonNull = true + ) +} + +case class MyClass(field: String) + +// use the custom schema derivation defined above +implicit val schemaForMyClass: Schema[Any, MyClass] = MySchemaDerivation.gen +``` + + + +```scala +import caliban.schema.SchemaDerivation + +object MySchemaDerivation extends SchemaDerivation[Any] { + override def config = DerivationConfig( + // add your config overrides here + enableSemanticNonNull = true + ) +} + +case class MyClass(field: String) + +// use the custom schema derivation defined above +given Schema[Any, MyClass] = MySchemaDerivation.gen +``` + + + +```scala +import caliban.schema.{ CommonSchemaDerivation, Schema } + +trait MySchemaDerivation[R] extends CommonSchemaDerivation { + override def config = DerivationConfig( + // add your config overrides here + enableSemanticNonNull = true + ) + + final class SemiAuto[A](impl: Schema[R, A]) extends Schema[R, A] { + export impl.* + } + + object SemiAuto { + inline def derived[A]: SemiAuto[A] = new SemiAuto[A](MySchemaDerivation.derived[R, A]) + } +} + +object MySchemaDerivation extends MySchemaDerivation[Any] + +case class MyClass(field: String) derives MySchemaDerivation.SemiAuto +``` + + + +### SemanticNonNull support + +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. +While Caliban resolves all fallible effectful types (`ZIO[R, Throwable, A]`, ...) as nullable by default, +with the feature enabled, fields that doesn't get resolved to nullable types (for example, `ZIO[R, Throwable, A]` where `A` is not `Option[A]`, ...) +will be marked with `@semanticNonNull` to express that the field never returns `null` unless the effect fails. +`@GQLNullable` annotation can be used to override this behavior per field. + +If you have custom types that override the `Schema` trait, make sure to override `nullable` and `canFail` methods to return the correct values. +All types that return `false` for `nullable` and `true` for `canFail` will be treated as semantically non-nullable. + ## Code generation Caliban can automatically generate Scala code from a GraphQL schema. From f1b13de86eeb1d2a9086beb48cce00bc95082dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cosmo=20Shin=20=28=EC=8B=A0=EC=9D=98=ED=95=98=29?= Date: Mon, 13 May 2024 23:46:23 +0900 Subject: [PATCH 2/2] Update vuepress/docs/docs/schema.md Co-authored-by: Pierre Ricadat --- vuepress/docs/docs/schema.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vuepress/docs/docs/schema.md b/vuepress/docs/docs/schema.md index a4e98ba45..570d7b59d 100644 --- a/vuepress/docs/docs/schema.md +++ b/vuepress/docs/docs/schema.md @@ -534,7 +534,7 @@ case class MyClass(field: String) derives MySchemaDerivation.SemiAuto 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. While Caliban resolves all fallible effectful types (`ZIO[R, Throwable, A]`, ...) as nullable by default, -with the feature enabled, fields that doesn't get resolved to nullable types (for example, `ZIO[R, Throwable, A]` where `A` is not `Option[A]`, ...) +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]`, ...) will be marked with `@semanticNonNull` to express that the field never returns `null` unless the effect fails. `@GQLNullable` annotation can be used to override this behavior per field.