Skip to content

Commit

Permalink
Parse annotations for type unions
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed May 22, 2024
1 parent 29b37f1 commit 3ad29b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
23 changes: 13 additions & 10 deletions core/src/main/scala-3/caliban/schema/TypeUnionDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ object TypeUnionDerivation {
def typeUnionSchema[R: Type, T: Type](using quotes: Quotes): Expr[Schema[R, T]] = {
import quotes.reflect.*

val typeName = TypeRepr.of[T].show

if (typeName.contains("|")) {
report.error(
s"You must explicitly add type parameter to derive Schema for a union type in order to capture the name of the type alias"
)
}

class TypeAndSchema[A](val typeRef: String, val schema: Expr[Schema[R, A]], val tpe: Type[A])

def rec[A](using tpe: Type[A]): List[TypeAndSchema[?]] =
Expand Down Expand Up @@ -37,14 +45,8 @@ object TypeUnionDerivation {
'{ (${ Expr(tas.typeRef) }, ${ tas.schema }.asInstanceOf[Schema[R, Any]]) }
}
)
val name = TypeRepr.of[T].show

if (name.contains("|")) {
report.error(
s"You must explicitly add type parameter to derive Schema for a union type in order to capture the name of the type alias"
)
}

val annotations: Expr[List[Any]] = magnolia1.Macro.anns[T](summon, quotes)
'{
val schemaByName: Map[String, Schema[R, Any]] = ${ schemaByTypeNameList }.toMap
new Schema[R, T] {
Expand All @@ -66,9 +68,10 @@ object TypeUnionDerivation {

def toType(isInput: Boolean, isSubscription: Boolean): __Type =
Types.makeUnion(
Some(${ Expr(name) }),
None,
schemaByName.values.map(_.toType_(isInput, isSubscription)).toList
Some(DerivationUtils.getName(${ annotations }, ${ Expr(typeName) })),
DerivationUtils.getDescription(${ annotations }),
schemaByName.values.map(_.toType_(isInput, isSubscription)).toList,
directives = Option(DerivationUtils.getDirectives(${ annotations })).filter(_.nonEmpty)
)
}
}
Expand Down
12 changes: 8 additions & 4 deletions core/src/test/scala-3/caliban/schema/Scala3DerivesSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package caliban.schema

import caliban.*
import caliban.schema.Annotations.{ GQLDescription, GQLField, GQLInterface, GQLName }
import caliban.parsing.adt.Directive
import caliban.schema.Annotations.{ GQLDescription, GQLDirective, GQLField, GQLInterface, GQLName }
import zio.test.{ assertTrue, ZIOSpecDefault }
import zio.{ RIO, Task, ZIO }

Expand Down Expand Up @@ -278,8 +279,10 @@ object Scala3DerivesSpec extends ZIOSpecDefault {
final case class Foo(value: String) derives Schema.SemiAuto
final case class Bar(foo: Int) derives Schema.SemiAuto
final case class Baz(bar: Int) derives Schema.SemiAuto
@GQLName("Payload2")
@GQLDescription("Union type Payload")
@GQLDirective(Directive.apply("mydirective", Map("arg" -> Value.StringValue("value"))))
type Payload = Foo | Bar | Baz

given Schema[Any, Payload] = Schema.unionType[Payload]

final case class QueryInput(isFoo: Boolean) derives ArgBuilder, Schema.SemiAuto
Expand All @@ -292,7 +295,8 @@ object Scala3DerivesSpec extends ZIOSpecDefault {
query: Query
}
union Payload = Foo | Bar | Baz
"Union type Payload"
union Payload2 @mydirective(arg: "value") = Foo | Bar | Baz
type Bar {
foo: Int!
Expand All @@ -307,7 +311,7 @@ type Foo {
}
type Query {
testQuery(isFoo: Boolean!): Payload!
testQuery(isFoo: Boolean!): Payload2!
}""".stripMargin
val interpreter = gql.interpreterUnsafe

Expand Down

0 comments on commit 3ad29b8

Please sign in to comment.