Skip to content

Commit

Permalink
Add introspection and codegen support for deprecated arguments (#2347)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou authored Jul 27, 2024
1 parent c5770da commit 8a5ba12
Show file tree
Hide file tree
Showing 24 changed files with 449 additions and 85 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ lazy val tools = project
"dev.zio" %% "zio-json" % zioJsonVersion % Test
)
)
.dependsOn(core, clientJVM)
.dependsOn(core, clientJVM, quickAdapter % Test)

lazy val tracing = project
.in(file("tracing"))
Expand Down
26 changes: 23 additions & 3 deletions client/src/main/scala/caliban/client/IntrospectionClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,17 @@ object IntrospectionClient {
arguments = List(Argument("includeDeprecated", includeDeprecated, "Boolean"))
)
def inputFields[A](innerSelection: SelectionBuilder[__InputValue, A]): SelectionBuilder[__Type, Option[List[A]]] =
Field("inputFields", OptionOf(ListOf(Obj(innerSelection))))
def ofType[A](innerSelection: SelectionBuilder[__Type, A]): SelectionBuilder[__Type, Option[A]] =
inputFields(None)(innerSelection)
def inputFields[A](includeDeprecated: Option[Boolean])(
innerSelection: SelectionBuilder[__InputValue, A]
): SelectionBuilder[__Type, Option[List[A]]] =
Field(
"inputFields",
OptionOf(ListOf(Obj(innerSelection))),
arguments = List(Argument("includeDeprecated", includeDeprecated, "Boolean"))
)

def ofType[A](innerSelection: SelectionBuilder[__Type, A]): SelectionBuilder[__Type, Option[A]] =
Field("ofType", OptionOf(Obj(innerSelection)))
}

Expand All @@ -160,7 +169,15 @@ object IntrospectionClient {
def name: SelectionBuilder[__Field, String] = Field("name", Scalar())
def description: SelectionBuilder[__Field, Option[String]] = Field("description", OptionOf(Scalar()))
def args[A](innerSelection: SelectionBuilder[__InputValue, A]): SelectionBuilder[__Field, List[A]] =
Field("args", ListOf(Obj(innerSelection)))
args(None)(innerSelection)
def args[A](
includeDeprecated: Option[Boolean]
)(innerSelection: SelectionBuilder[__InputValue, A]): SelectionBuilder[__Field, List[A]] =
Field(
"args",
ListOf(Obj(innerSelection)),
arguments = List(Argument("includeDeprecated", includeDeprecated, "Boolean"))
)
def `type`[A](innerSelection: SelectionBuilder[__Type, A]): SelectionBuilder[__Field, A] =
Field("type", Obj(innerSelection))
def isDeprecated: SelectionBuilder[__Field, Boolean] = Field("isDeprecated", Scalar())
Expand All @@ -173,6 +190,9 @@ object IntrospectionClient {
def description: SelectionBuilder[__InputValue, Option[String]] = Field("description", OptionOf(Scalar()))
def `type`[A](innerSelection: SelectionBuilder[__Type, A]): SelectionBuilder[__InputValue, A] =
Field("type", Obj(innerSelection))
def isDeprecated: SelectionBuilder[__InputValue, Boolean] = Field("isDeprecated", Scalar())
def deprecationReason: SelectionBuilder[__InputValue, Option[String]] =
Field("deprecationReason", OptionOf(Scalar()))
def defaultValue: SelectionBuilder[__InputValue, Option[String]] = Field("defaultValue", OptionOf(Scalar()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sealed trait CalibanSettings {
final def effect(effect: String): Self = withSettings(_.effect(effect))
final def abstractEffectType(abstractEffectType: Boolean): Self =
withSettings(_.abstractEffectType(abstractEffectType))
final def supportDeprecatedArgs(value: Boolean): Self = withSettings(_.supportDeprecatedArgs(value))
final def supportIsRepeatable(value: Boolean): Self = withSettings(_.supportIsRepeatable(value))
final def preserveInputNames(value: Boolean): Self = withSettings(_.preserveInputNames(value))
final def addDerives(value: Boolean): Self = withSettings(_.addDerives(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ object OptionsParser {
supportIsRepeatable: Option[Boolean],
addDerives: Option[Boolean],
envForDerives: Option[String],
excludeDeprecated: Option[Boolean]
excludeDeprecated: Option[Boolean],
supportDeprecatedArgs: Option[Boolean]
)

private object DescriptorUtils {
Expand Down Expand Up @@ -76,7 +77,8 @@ object OptionsParser {
rawOpts.supportIsRepeatable,
rawOpts.addDerives,
rawOpts.envForDerives,
rawOpts.excludeDeprecated
rawOpts.excludeDeprecated,
rawOpts.supportDeprecatedArgs
)
}.option
case _ => ZIO.none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -61,6 +62,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -92,6 +94,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -141,6 +144,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -172,6 +176,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -203,6 +208,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -234,6 +240,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -265,6 +272,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -296,6 +304,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -327,6 +336,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -358,6 +368,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -389,6 +400,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -420,6 +432,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -451,7 +464,40 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
Some(true)
Some(true),
None
)
)
)
}
},
test("provide supportDeprecatedArgs & supportIsRepeatable") {
val input = List("schema", "output", "--supportDeprecatedArgs", "false", "--supportIsRepeatable", "false")
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
Options(
"schema",
"output",
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
Some(false),
None,
None,
None,
Some(false)
)
)
)
Expand Down
23 changes: 20 additions & 3 deletions core/src/main/scala/caliban/parsing/adt/Definition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ object Definition {
def name: String
def description: Option[String]
def directives: List[Directive]

final def isDeprecated: Boolean = Directives.isDeprecated(directives)
final def deprecationReason: Option[String] = Directives.deprecationReason(directives)
}

object TypeDefinition {

final case class ObjectTypeDefinition(
Expand Down Expand Up @@ -142,17 +146,30 @@ object Definition {
ofType: Type,
defaultValue: Option[InputValue],
directives: List[Directive]
)
) {
def isDeprecated: Boolean = Directives.isDeprecated(directives)
def deprecationReason: Option[String] = Directives.deprecationReason(directives)
}

final case class FieldDefinition(
description: Option[String],
name: String,
args: List[InputValueDefinition],
ofType: Type,
directives: List[Directive]
)
) {
def isDeprecated: Boolean = Directives.isDeprecated(directives)
def deprecationReason: Option[String] = Directives.deprecationReason(directives)
}

final case class EnumValueDefinition(description: Option[String], enumValue: String, directives: List[Directive])
final case class EnumValueDefinition(
description: Option[String],
enumValue: String,
directives: List[Directive]
) {
def isDeprecated: Boolean = Directives.isDeprecated(directives)
def deprecationReason: Option[String] = Directives.deprecationReason(directives)
}

}

Expand Down
14 changes: 10 additions & 4 deletions tools/src/main/scala/caliban/tools/CalibanCommonSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ final case class CalibanCommonSettings(
supportIsRepeatable: Option[Boolean],
addDerives: Option[Boolean],
envForDerives: Option[String],
excludeDeprecated: Option[Boolean]
excludeDeprecated: Option[Boolean],
supportDeprecatedArgs: Option[Boolean]
) {

private[caliban] def toOptions(schemaPath: String, toPath: String): Options =
Expand All @@ -43,7 +44,8 @@ final case class CalibanCommonSettings(
supportIsRepeatable = supportIsRepeatable,
addDerives = addDerives,
envForDerives = envForDerives,
excludeDeprecated = excludeDeprecated
excludeDeprecated = excludeDeprecated,
supportDeprecatedArgs = supportDeprecatedArgs
)

private[caliban] def combine(r: => CalibanCommonSettings): CalibanCommonSettings =
Expand All @@ -65,7 +67,8 @@ final case class CalibanCommonSettings(
supportIsRepeatable = r.supportIsRepeatable.orElse(this.supportIsRepeatable),
addDerives = r.addDerives.orElse(this.addDerives),
envForDerives = r.envForDerives.orElse(this.envForDerives),
excludeDeprecated = r.excludeDeprecated.orElse(this.excludeDeprecated)
excludeDeprecated = r.excludeDeprecated.orElse(this.excludeDeprecated),
supportDeprecatedArgs = r.supportDeprecatedArgs.orElse(this.supportDeprecatedArgs)
)

def clientName(value: String): CalibanCommonSettings = this.copy(clientName = Some(value))
Expand All @@ -90,6 +93,8 @@ final case class CalibanCommonSettings(
def addDerives(addDerives: Boolean): CalibanCommonSettings = this.copy(addDerives = Some(addDerives))
def envForDerives(envForDerives: String): CalibanCommonSettings = this.copy(envForDerives = Some(envForDerives))
def excludeDeprecated(value: Boolean): CalibanCommonSettings = this.copy(excludeDeprecated = Some(value))
def supportDeprecatedArgs(value: Boolean): CalibanCommonSettings =
this.copy(supportDeprecatedArgs = Some(value))
}

object CalibanCommonSettings {
Expand All @@ -112,6 +117,7 @@ object CalibanCommonSettings {
supportIsRepeatable = None,
addDerives = None,
envForDerives = None,
excludeDeprecated = None
excludeDeprecated = None,
supportDeprecatedArgs = None
)
}
Loading

0 comments on commit 8a5ba12

Please sign in to comment.