From 3e8b5c44d8f95eebb88bbab85d68f057235c878a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20W=C3=A4rnsberg?= Date: Mon, 15 Nov 2021 02:10:15 +0100 Subject: [PATCH] feat: Add @GQLExcluded (#1141) * feat: Add @GQLExcluded * PR Comments * more _ :D --- .../caliban/schema/SchemaDerivation.scala | 1 + .../caliban/schema/SchemaDerivation.scala | 4 +++- .../scala/caliban/schema/Annotations.scala | 5 +++++ .../scala/caliban/schema/SchemaSpec.scala | 21 ++++++++++++++++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala b/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala index e865e16de7..da4ef31b99 100644 --- a/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala +++ b/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala @@ -64,6 +64,7 @@ trait SchemaDerivation[R] extends LowPriorityDerivedSchema { Some(getName(ctx)), getDescription(ctx), ctx.parameters + .filterNot(_.annotations.exists(_ == GQLExcluded())) .map(p => __Field( getName(p), diff --git a/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala b/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala index 51b1dfdc10..dfc44cbaf1 100644 --- a/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala +++ b/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala @@ -143,7 +143,9 @@ trait SchemaDerivation[R] { makeObject( Some(getName(annotations, info)), getDescription(annotations), - fields.map { case (label, _, schema, _) => + fields.filterNot { case (label, _, _, _) => + paramAnnotations.getOrElse(label, Nil).exists(_ == GQLExcluded()) + }.map { case (label, _, schema, _) => val fieldAnnotations = paramAnnotations.getOrElse(label, Nil) __Field( getName(fieldAnnotations, label), diff --git a/core/src/main/scala/caliban/schema/Annotations.scala b/core/src/main/scala/caliban/schema/Annotations.scala index ced4505e82..fd4c05aadc 100644 --- a/core/src/main/scala/caliban/schema/Annotations.scala +++ b/core/src/main/scala/caliban/schema/Annotations.scala @@ -16,6 +16,11 @@ object Annotations { */ case class GQLDescription(value: String) extends StaticAnnotation + /** + * Annotation used to exclude a field from a type. + */ + case class GQLExcluded() extends StaticAnnotation + /** * Annotation used to customize the name of an input type. * This is usually needed to avoid a name clash when a type is used both as an input and an output. diff --git a/core/src/test/scala/caliban/schema/SchemaSpec.scala b/core/src/test/scala/caliban/schema/SchemaSpec.scala index a6739e00d1..88245898e4 100644 --- a/core/src/test/scala/caliban/schema/SchemaSpec.scala +++ b/core/src/test/scala/caliban/schema/SchemaSpec.scala @@ -1,8 +1,10 @@ package caliban.schema import java.util.UUID +import caliban.GraphQL.graphQL +import caliban.RootResolver import caliban.introspection.adt.{ __DeprecatedArgs, __Type, __TypeKind } -import caliban.schema.Annotations.{ GQLInterface, GQLUnion, GQLValueType } +import caliban.schema.Annotations.{ GQLExcluded, GQLInterface, GQLUnion, GQLValueType } import zio.blocking.Blocking import zio.console.Console import zio.query.ZQuery @@ -162,6 +164,23 @@ object SchemaSpec extends DefaultRunnableSpec { assert(introspect[Queries].fields(__DeprecatedArgs()).toList.flatten.headOption.map(_.`type`()))( isSome(hasField[__Type, Option[String]]("name", _.name, equalTo(Some("Wrapper")))) ) + }, + test("GQLExcluded") { + case class QueryType(a: String, @GQLExcluded b: String) + case class Query(query: QueryType) + val gql = graphQL(RootResolver(Query(QueryType("a", "b")))) + val expected = """schema { + | query: Query + |} + + |type Query { + | query: QueryType! + |} + + |type QueryType { + | a: String! + |}""".stripMargin + assertTrue(gql.render == expected) } )