Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to parent type annotations for @GQLOneOfInput #2322

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions core/src/main/scala-2/caliban/schema/SchemaDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,14 @@ trait CommonSchemaDerivation[R] {
.getOrElse(customizeInputTypeName(getName(ctx)))),
getDescription(ctx),
ctx.subtypes.toList.flatMap { p =>
p.typeclass.toType_(isInput = true).allInputFields.map(_.nullable)
},
val pTpe = p.typeclass.toType_(isInput = true)
pTpe.allInputFields.map { t =>
t.nullable.copy(
description = t.description.orElse(pTpe.description),
directives = t.directives.orElse(pTpe.directives)
)
}
}.sortBy(_.name),
Some(ctx.typeName.full),
Some(List(Directive(Directives.OneOf)))
)
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/scala-3/caliban/schema/DerivationUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,15 @@ private object DerivationUtils {
makeInputObject(
Some(getInputName(annotations).getOrElse(customizeInputTypeName(getName(annotations, info)))),
getDescription(annotations),
schemas.flatMap(_.toType_(isInput = true).allInputFields.map(_.nullable)),
schemas.flatMap { s =>
val pTpe = s.toType_(isInput = true)
pTpe.allInputFields.map { t =>
t.nullable.copy(
description = t.description.orElse(pTpe.description),
directives = t.directives.orElse(pTpe.directives)
)
}
}.sortBy(_.name),
Some(info.full),
Some(List(Directive(Directives.OneOf)))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import caliban.CalibanError.ExecutionError
import caliban.InputValue.{ ListValue, ObjectValue }
import caliban.Macros.gqldoc
import caliban.Value.{ IntValue, NullValue, StringValue }
import caliban.schema.Annotations.GQLOneOfInput
import caliban.parsing.adt.Directive
import caliban.schema.Annotations.{ GQLDescription, GQLDirective, GQLOneOfInput }
import caliban.schema.ArgBuilder.*
import zio.ZIO
import zio.test.*
Expand Down Expand Up @@ -70,8 +71,8 @@ object ArgBuilderDerivesAutoSpec extends ZIOSpecDefault {
suite("enums as oneOf inputs") {
@GQLOneOfInput
enum Foo derives Schema.SemiAuto, ArgBuilder {
case FooString(stringValue: String) extends Foo
case FooInt(intValue: Int) extends Foo
@GQLDescription("fooString") case FooString(stringValue: String) extends Foo
@GQLDirective(Directive("intDirective")) case FooInt(intValue: Int) extends Foo
}
case class Wrapper(fooInput: Foo) derives Schema.SemiAuto, ArgBuilder
case class Queries(foo: Wrapper => String, fooUnwrapped: Foo => String) derives Schema.SemiAuto
Expand All @@ -84,8 +85,9 @@ object ArgBuilderDerivesAutoSpec extends ZIOSpecDefault {
|}
|
|input FooInput @oneOf {
| intValue: Int @intDirective
| "fooString"
| stringValue: String
| intValue: Int
|}
|
|type Queries {
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/scala/caliban/RenderingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ object RenderingSpec extends ZIOSpecDefault {
|}
|
|input FooInput @oneOf {
| stringValue: String
| otherStringField: String
| intValue: FooIntInput
| otherIntField: FooInt2Input
| otherStringField: String
| stringValue: String
|}
|
|input FooInt2Input {
Expand Down
31 changes: 31 additions & 0 deletions core/src/test/scala/caliban/schema/SchemaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,27 @@ object SchemaSpec extends ZIOSpecDefault {
| mid2: Mid2!
|}""".stripMargin
)
},
test("annotations on leaf classes of OneOfInput are added to the input object fields") {
case class Query(value: MyOneOfInput => String)

val schema = graphQL(RootResolver(Query(_.toString))).render
println(schema)
assertTrue(
schema == """schema {
| query: Query
|}
|
|input MyOneOfInput @oneOf {
| "foo input"
| a: Int
| b: String @barDirective
|}
|
|type Query {
| value(value: MyOneOfInput!): String!
|}""".stripMargin
)
}
)

Expand Down Expand Up @@ -460,4 +481,14 @@ object SchemaSpec extends ZIOSpecDefault {
case class FooB(b: String, c: String, d: String) extends Mid1 with Mid2
case class FooC(b: String, d: String, e: String) extends Mid2
}

@GQLOneOfInput
sealed trait MyOneOfInput

object OneOfInput {
@GQLDescription("foo input")
case class Foo(a: Int) extends MyOneOfInput
@GQLDirective(Directive("barDirective"))
case class Bar(b: String) extends MyOneOfInput
}
}