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

Default to None in client for optional arguments #254

Merged
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
14 changes: 10 additions & 4 deletions codegen/src/main/scala/caliban/codegen/ClientWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,8 @@ object ClientWriter {
)
}
val args = field.args match {
case Nil => ""
case list =>
s"(${list.map(arg => s"${safeName(arg.name)}: ${writeType(arg.ofType)}").mkString(", ")})"
case Nil => ""
case list => s"(${writeArgumentFields(list)})"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the quotes now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to add parens in this case, I believe. Is that what you mean?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry i was not seeing properly. All good!

}
val argBuilder = field.args match {
case Nil => ""
Expand All @@ -263,7 +262,14 @@ object ClientWriter {
}

def writeArgumentFields(args: List[InputValueDefinition]): String =
s"${args.map(arg => s"${safeName(arg.name)}: ${writeType(arg.ofType)}").mkString(", ")}"
s"${args.map(arg => s"${safeName(arg.name)}: ${writeType(arg.ofType)}${writeDefaultArgument(arg)}").mkString(", ")}"

def writeDefaultArgument(arg: InputValueDefinition): String =
arg.ofType match {
case t if t.nullable => " = None"
case ListType(_, _) => " = Nil"
case _ => ""
}

def writeArguments(field: FieldDefinition): String =
if (field.args.nonEmpty) {
Expand Down
42 changes: 41 additions & 1 deletion codegen/src/test/scala/caliban/codegen/ClientWriterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ import caliban.client.Value._

object Client {

case class CharacterInput(name: String, nicknames: List[String])
case class CharacterInput(name: String, nicknames: List[String] = Nil)
object CharacterInput {
implicit val encoder: ArgEncoder[CharacterInput] = new ArgEncoder[CharacterInput] {
override def encode(value: CharacterInput): Value =
Expand Down Expand Up @@ -356,6 +356,46 @@ object Client {
def nicknames: SelectionBuilder[Character, List[String]] = Field("nicknames", ListOf(Scalar()))
}

}
"""
)
)
},
testM("default arguments for optional and list arguments") {
val schema =
"""
type Query {
characters(
first: Int!
last: Int
origins: [String]!
): String
}""".stripMargin

assertM(
gen(schema),
equalTo(
"""import caliban.client.FieldBuilder._
import caliban.client.SelectionBuilder._
import caliban.client._
import caliban.client.Operations._

object Client {

type Query = RootQuery
object Query {
def characters(
first: Int,
last: Option[Int] = None,
origins: List[Option[String]] = Nil
): SelectionBuilder[RootQuery, Option[String]] =
Field(
"characters",
OptionOf(Scalar()),
arguments = List(Argument("first", first), Argument("last", last), Argument("origins", origins))
)
}

}
"""
)
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/scala/caliban/parsing/adt/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package caliban.parsing.adt

import scala.annotation.tailrec

sealed trait Type
sealed trait Type {
val nonNull: Boolean
var nullable: Boolean = !nonNull
}

object Type {

Expand Down
52 changes: 26 additions & 26 deletions examples/src/main/scala/caliban/client/Client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import caliban.client.FieldBuilder._
import caliban.client.SelectionBuilder._
import caliban.client.Operations._
import caliban.client.Value._
import Client.Role._

/**
* This code was auto-generated by the caliban-codegen sbt plugin
*/
object Client {

sealed trait Origin extends Product with Serializable
sealed trait Origin extends scala.Product with scala.Serializable
object Origin {
case object BELT extends Origin
case object EARTH extends Origin
Expand All @@ -26,31 +22,17 @@ object Client {
}
implicit val encoder: ArgEncoder[Origin] = new ArgEncoder[Origin] {
override def encode(value: Origin): Value = value match {
case BELT => EnumValue("BELT")
case EARTH => EnumValue("EARTH")
case MARS => EnumValue("MARS")
case Origin.BELT => EnumValue("BELT")
case Origin.EARTH => EnumValue("EARTH")
case Origin.MARS => EnumValue("MARS")
}
override def typeName: String = "Origin"
}
}

object Role {
type Captain
object Captain {
def shipName: SelectionBuilder[Captain, String] = Field("shipName", Scalar())
}
type Engineer
object Engineer {
def shipName: SelectionBuilder[Engineer, String] = Field("shipName", Scalar())
}
type Mechanic
object Mechanic {
def shipName: SelectionBuilder[Mechanic, String] = Field("shipName", Scalar())
}
type Pilot
object Pilot {
def shipName: SelectionBuilder[Pilot, String] = Field("shipName", Scalar())
}
type Engineer
object Engineer {
def shipName: SelectionBuilder[Engineer, String] = Field("shipName", Scalar())
}

type Character
Expand Down Expand Up @@ -79,9 +61,25 @@ object Client {
)
}

type Pilot
object Pilot {
def shipName: SelectionBuilder[Pilot, String] = Field("shipName", Scalar())
}

type Mechanic
object Mechanic {
def shipName: SelectionBuilder[Mechanic, String] = Field("shipName", Scalar())
}

type Captain
object Captain {
def shipName: SelectionBuilder[Captain, String] = Field("shipName", Scalar())
}

type Queries = RootQuery
object Queries {
def characters[A](
origin: Option[Origin]
origin: Option[Origin] = None
)(innerSelection: SelectionBuilder[Character, A]): SelectionBuilder[RootQuery, List[A]] =
Field("characters", ListOf(Obj(innerSelection)), arguments = List(Argument("origin", origin)))
def character[A](
Expand All @@ -90,9 +88,11 @@ object Client {
Field("character", OptionOf(Obj(innerSelection)), arguments = List(Argument("name", name)))
}

type Mutations = RootMutation
object Mutations {
def deleteCharacter(name: String): SelectionBuilder[RootMutation, Boolean] =
Field("deleteCharacter", Scalar(), arguments = List(Argument("name", name)))
}

}

1 change: 0 additions & 1 deletion examples/src/main/scala/caliban/client/ExampleApp.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package caliban.client

import caliban.client.Client.Role._
import caliban.client.Client._
import sttp.client._
import sttp.client.asynchttpclient.zio.AsyncHttpClientZioBackend
Expand Down