Skip to content

Commit

Permalink
Fix interface/union code generation (ghostdogpr#1217)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostdogpr authored and Fluxx committed Jan 13, 2022
1 parent 236f10a commit 1bf9a74
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
4 changes: 2 additions & 2 deletions tools/src/main/scala/caliban/tools/ClientWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ object ClientWriter {

val unionTypes = typesMap.collect { case (key, _: UnionTypeDefinition) => key }
val optionalUnionTypeFields = typedef.fields.flatMap { field =>
val isOptionalUnionType = unionTypes.exists(_.compareToIgnoreCase(field.ofType.toString) == 0)
val isOptionalUnionType = unionTypes.exists(_.compareToIgnoreCase(Type.innerType(field.ofType)) == 0)
if (isOptionalUnionType)
Some(
collectFieldInfo(
Expand All @@ -394,7 +394,7 @@ object ClientWriter {

val interfaceTypes = typesMap.collect { case (key, _: InterfaceTypeDefinition) => key }
val optionalInterfaceTypeFields = typedef.fields.flatMap { field =>
val isOptionalInterfaceType = interfaceTypes.exists(_.compareToIgnoreCase(field.ofType.toString) == 0)
val isOptionalInterfaceType = interfaceTypes.exists(_.compareToIgnoreCase(Type.innerType(field.ofType)) == 0)
if (isOptionalInterfaceType)
Vector(
collectFieldInfo(
Expand Down
77 changes: 76 additions & 1 deletion tools/src/test/scala/caliban/tools/ClientWriterSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package caliban.tools

import caliban.parsing.Parser
import zio.RIO
import zio.{ RIO, UIO }
import zio.blocking.Blocking
import zio.test.Assertion._
import zio.test._
Expand Down Expand Up @@ -895,6 +895,81 @@ object Client {
"""
)
}
},
testM("interface with list") {
val schema =
"""
interface Order {
name: String!
}
type Ascending implements Order {
name: String!
}
type Descending implements Order {
name: String!
}
type Sort {
orders: [Order]
}
""".stripMargin

for {
code <- gen(schema, Map.empty, List.empty)
} yield assertTrue(
code == """import caliban.client.FieldBuilder._
import caliban.client._
object Client {
type Order
object Order {
def name: SelectionBuilder[Order, String] = _root_.caliban.client.SelectionBuilder.Field("name", Scalar())
}
type Ascending
object Ascending {
def name: SelectionBuilder[Ascending, String] = _root_.caliban.client.SelectionBuilder.Field("name", Scalar())
}
type Descending
object Descending {
def name: SelectionBuilder[Descending, String] = _root_.caliban.client.SelectionBuilder.Field("name", Scalar())
}
type Sort
object Sort {
def orders[A](
onAscending: SelectionBuilder[Ascending, A],
onDescending: SelectionBuilder[Descending, A]
): SelectionBuilder[Sort, Option[List[Option[A]]]] = _root_.caliban.client.SelectionBuilder.Field(
"orders",
OptionOf(ListOf(OptionOf(ChoiceOf(Map("Ascending" -> Obj(onAscending), "Descending" -> Obj(onDescending))))))
)
def ordersOption[A](
onAscending: Option[SelectionBuilder[Ascending, A]] = None,
onDescending: Option[SelectionBuilder[Descending, A]] = None
): SelectionBuilder[Sort, Option[Option[List[Option[A]]]]] = _root_.caliban.client.SelectionBuilder.Field(
"orders",
OptionOf(
ListOf(
OptionOf(
ChoiceOf(
Map(
"Ascending" -> onAscending.fold[FieldBuilder[Option[A]]](NullField)(a => OptionOf(Obj(a))),
"Descending" -> onDescending.fold[FieldBuilder[Option[A]]](NullField)(a => OptionOf(Obj(a)))
)
)
)
)
)
)
def ordersInterface[A](orders: SelectionBuilder[Order, A]): SelectionBuilder[Sort, Option[List[Option[A]]]] =
_root_.caliban.client.SelectionBuilder.Field("orders", OptionOf(ListOf(OptionOf(Obj(orders)))))
}
}
"""
)
}
) @@ TestAspect.sequential
}

0 comments on commit 1bf9a74

Please sign in to comment.