From 1bf9a74f3791c29a526cf52155c75126d123eae5 Mon Sep 17 00:00:00 2001 From: Pierre Ricadat Date: Mon, 20 Dec 2021 11:21:53 +0100 Subject: [PATCH] Fix interface/union code generation (#1217) --- .../scala/caliban/tools/ClientWriter.scala | 4 +- .../caliban/tools/ClientWriterSpec.scala | 77 ++++++++++++++++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/tools/src/main/scala/caliban/tools/ClientWriter.scala b/tools/src/main/scala/caliban/tools/ClientWriter.scala index 35390997c..8e98c1c78 100644 --- a/tools/src/main/scala/caliban/tools/ClientWriter.scala +++ b/tools/src/main/scala/caliban/tools/ClientWriter.scala @@ -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( @@ -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( diff --git a/tools/src/test/scala/caliban/tools/ClientWriterSpec.scala b/tools/src/test/scala/caliban/tools/ClientWriterSpec.scala index 09c75f83f..13224f231 100644 --- a/tools/src/test/scala/caliban/tools/ClientWriterSpec.scala +++ b/tools/src/test/scala/caliban/tools/ClientWriterSpec.scala @@ -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._ @@ -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 }