Skip to content

Commit

Permalink
feat: Add @GQLExcluded (ghostdogpr#1141)
Browse files Browse the repository at this point in the history
* feat: Add @GQLExcluded

* PR Comments

* more _ :D
  • Loading branch information
frekw authored Nov 15, 2021
1 parent 9a2ba84 commit 3e8b5c4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/scala-3/caliban/schema/SchemaDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/caliban/schema/Annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 20 additions & 1 deletion core/src/test/scala/caliban/schema/SchemaSpec.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
)

Expand Down

0 comments on commit 3e8b5c4

Please sign in to comment.