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

[Issue 645] Added Option to exclude deprecation flags. Schema -> Clie… #1082

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ final case class CalibanFileSettings(file: File, settings: CalibanCommonSettings
def splitFiles(value: Boolean): CalibanFileSettings = this.copy(settings = this.settings.splitFiles(value))
def enableFmt(value: Boolean): CalibanFileSettings = this.copy(settings = this.settings.enableFmt(value))
def extensibleEnums(value: Boolean): CalibanFileSettings = this.copy(settings = this.settings.extensibleEnums(value))
def excludeClientDeprecation(value: Boolean): CalibanFileSettings =
this.copy(settings = this.settings.excludeClientDeprecation(value))
}

final case class CalibanUrlSettings(url: URL, settings: CalibanCommonSettings) extends CalibanSettings {
Expand All @@ -34,4 +36,6 @@ final case class CalibanUrlSettings(url: URL, settings: CalibanCommonSettings) e
def splitFiles(value: Boolean): CalibanUrlSettings = this.copy(settings = this.settings.splitFiles(value))
def enableFmt(value: Boolean): CalibanUrlSettings = this.copy(settings = this.settings.enableFmt(value))
def extensibleEnums(value: Boolean): CalibanUrlSettings = this.copy(settings = this.settings.extensibleEnums(value))
def excludeClientDepercation(value: Boolean): CalibanUrlSettings =
this.copy(settings = this.settings.excludeClientDeprecation(value))
}
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 @@ -10,7 +10,8 @@ final case class CalibanCommonSettings(
imports: Seq[String],
splitFiles: Option[Boolean],
enableFmt: Option[Boolean],
extensibleEnums: Option[Boolean]
extensibleEnums: Option[Boolean],
excludeClientDeprecation: Option[Boolean]
) {

private[caliban] def toOptions(schemaPath: String, toPath: String): Options =
Expand All @@ -28,7 +29,8 @@ final case class CalibanCommonSettings(
abstractEffectType = Option.empty,
splitFiles = splitFiles,
enableFmt = enableFmt,
extensibleEnums = extensibleEnums
extensibleEnums = extensibleEnums,
excludeClientDeprecation = excludeClientDeprecation
)

private[caliban] def combine(r: => CalibanCommonSettings): CalibanCommonSettings =
Expand All @@ -42,7 +44,8 @@ final case class CalibanCommonSettings(
imports = this.imports ++ r.imports,
splitFiles = r.splitFiles.orElse(this.splitFiles),
enableFmt = r.enableFmt.orElse(this.enableFmt),
extensibleEnums = r.extensibleEnums.orElse(this.extensibleEnums)
extensibleEnums = r.extensibleEnums.orElse(this.extensibleEnums),
excludeClientDeprecation = r.excludeClientDeprecation.orElse(this.excludeClientDeprecation)
)

def clientName(value: String): CalibanCommonSettings = this.copy(clientName = Some(value))
Expand All @@ -56,6 +59,8 @@ final case class CalibanCommonSettings(
def splitFiles(value: Boolean): CalibanCommonSettings = this.copy(splitFiles = Some(value))
def enableFmt(value: Boolean): CalibanCommonSettings = this.copy(enableFmt = Some(value))
def extensibleEnums(value: Boolean): CalibanCommonSettings = this.copy(extensibleEnums = Some(value))
def excludeClientDeprecation(value: Boolean): CalibanCommonSettings =
this.copy(excludeClientDeprecation = Some(value))
}

object CalibanCommonSettings {
Expand All @@ -70,6 +75,7 @@ object CalibanCommonSettings {
imports = Seq.empty,
splitFiles = None,
enableFmt = None,
extensibleEnums = None
extensibleEnums = None,
excludeClientDeprecation = None
)
}
59 changes: 37 additions & 22 deletions tools/src/main/scala/caliban/tools/ClientWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ object ClientWriter {
genView: Boolean = false,
additionalImports: Option[List[String]] = None,
splitFiles: Boolean = false,
extensibleEnums: Boolean = false
extensibleEnums: Boolean = false,
excludeClientDeprecation: Boolean = false
)(implicit scalarMappings: ScalarMappings): List[(String, String)] = {
require(packageName.isDefined || !splitFiles, "splitFiles option requires a package name")

val schemaDef = schema.schemaDefinition

implicit val excludeDeprecation: Boolean = excludeClientDeprecation
Copy link
Contributor

@guizmaii guizmaii Oct 12, 2021

Choose a reason for hiding this comment

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

This is weird. Why do you need to do that?

Copy link
Author

Choose a reason for hiding this comment

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

That was one approach. Another is to just pass a parameter, I had to modify quite a few signatures instead, thought better was to do via implicit instead.


implicit val mappingClashedTypeNames: MappingClashedTypeNames = MappingClashedTypeNames(
getMappingsClashedNames(
schema.definitions.collect {
Expand Down Expand Up @@ -295,7 +298,8 @@ object ClientWriter {
)(implicit
typesMap: TypesMap,
mappingClashedTypeNames: MappingClashedTypeNames,
scalarMappings: ScalarMappings
scalarMappings: ScalarMappings,
excludeDeprecation: Boolean
): String =
s"""object ${typedef.name} {
| ${typedef.fields.map(writeField(_, "_root_.caliban.client.Operations.RootQuery")).mkString("\n ")}
Expand All @@ -312,7 +316,8 @@ object ClientWriter {
)(implicit
typesMap: TypesMap,
mappingClashedTypeNames: MappingClashedTypeNames,
scalarMappings: ScalarMappings
scalarMappings: ScalarMappings,
excludeDeprecation: Boolean
): String =
s"""object ${typedef.name} {
| ${typedef.fields.map(writeField(_, "_root_.caliban.client.Operations.RootMutation")).mkString("\n ")}
Expand All @@ -329,7 +334,8 @@ object ClientWriter {
)(implicit
typesMap: TypesMap,
mappingClashedTypeNames: MappingClashedTypeNames,
scalarMappings: ScalarMappings
scalarMappings: ScalarMappings,
excludeDeprecation: Boolean
): String =
s"""object ${typedef.name} {
| ${typedef.fields.map(writeField(_, "_root_.caliban.client.Operations.RootSubscription")).mkString("\n ")}
Expand All @@ -354,7 +360,8 @@ object ClientWriter {
)(implicit
typesMap: TypesMap,
mappingClashedTypeNames: MappingClashedTypeNames,
scalarMappings: ScalarMappings
scalarMappings: ScalarMappings,
excludeDeprecation: Boolean
): String = {

val objectName: String = safeTypeName(typedef.name)
Expand Down Expand Up @@ -617,7 +624,8 @@ object ClientWriter {
)(implicit
typesMap: TypesMap,
mappingClashedTypeNames: MappingClashedTypeNames,
scalarMappings: ScalarMappings
scalarMappings: ScalarMappings,
excludeDeprecation: Boolean
): String =
writeFieldInfo(collectFieldInfo(field, typeName))

Expand Down Expand Up @@ -648,27 +656,34 @@ object ClientWriter {
)(implicit
typesMap: TypesMap,
mappingClashedTypeNames: MappingClashedTypeNames,
scalarMappings: ScalarMappings
scalarMappings: ScalarMappings,
excludeDeprecation: Boolean
): FieldInfo = {
val name = safeName(field.name)
val description = field.description match {
val name = safeName(field.name)
val description = field.description match {
case Some(d) if d.trim.nonEmpty => s"/**\n * ${d.trim}\n */\n"
case _ => ""
}
val deprecated = field.directives.find(_.name == "deprecated") match {
case None => ""
case Some(directive) =>
val body =
directive.arguments.collectFirst { case ("reason", StringValue(reason)) =>
reason
}.getOrElse("")

val quotes =
if (body.contains("\n")) tripleQuotes
else doubleQuotes

"@deprecated(" + quotes + body + quotes + """, "")""" + "\n"

val deprecated = excludeDeprecation match {
case true => ""
case false =>
field.directives.find(_.name == "deprecated") match {
case None => ""
case Some(directive) =>
val body =
directive.arguments.collectFirst { case ("reason", StringValue(reason)) =>
reason
}.getOrElse("")

val quotes =
if (body.contains("\n")) tripleQuotes
else doubleQuotes

"@deprecated(" + quotes + body + quotes + """, "")""" + "\n"
}
}

val fieldType = safeTypeName(getTypeName(field.ofType))
val isScalar = typesMap
.get(fieldType)
Expand Down
9 changes: 6 additions & 3 deletions tools/src/main/scala/caliban/tools/Options.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ final case class Options(
abstractEffectType: Option[Boolean],
splitFiles: Option[Boolean],
enableFmt: Option[Boolean],
extensibleEnums: Option[Boolean]
extensibleEnums: Option[Boolean],
excludeClientDeprecation: Option[Boolean]
)

object Options {
Expand All @@ -34,7 +35,8 @@ object Options {
abstractEffectType: Option[Boolean],
splitFiles: Option[Boolean],
enableFmt: Option[Boolean],
extensibleEnums: Option[Boolean]
extensibleEnums: Option[Boolean],
excludeClientDeprecation: Option[Boolean]
)

def fromArgs(args: List[String]): Option[Options] =
Expand Down Expand Up @@ -77,7 +79,8 @@ object Options {
rawOpts.abstractEffectType,
rawOpts.splitFiles,
rawOpts.enableFmt,
rawOpts.extensibleEnums
rawOpts.extensibleEnums,
rawOpts.excludeClientDeprecation
)
}
case _ => None
Expand Down
9 changes: 6 additions & 3 deletions tools/src/main/scala/caliban/tools/compiletime/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ trait Config {
imports: List[String] = List.empty,
splitFiles: Boolean = false,
enableFmt: Boolean = true,
extensibleEnums: Boolean = false
extensibleEnums: Boolean = false,
excludeClientDeprecation: Boolean = false
) {
private[caliban] def toCalibanCommonSettings: CalibanCommonSettings =
CalibanCommonSettings(
Expand All @@ -25,7 +26,8 @@ trait Config {
imports = imports,
splitFiles = Some(splitFiles),
enableFmt = Some(enableFmt),
extensibleEnums = Some(extensibleEnums)
extensibleEnums = Some(extensibleEnums),
excludeClientDeprecation = Some(excludeClientDeprecation)
)

private[caliban] def asScalaCode: String = {
Expand All @@ -40,7 +42,8 @@ trait Config {
| imports = ${toScalaCode(imports)(v => s""""$v"""")},
| splitFiles = $splitFiles,
| enableFmt = $enableFmt,
| extensibleEnums = $extensibleEnums
| extensibleEnums = $extensibleEnums,
| excludeClientDeprecation = $excludeClientDeprecation
|)
""".stripMargin.trim
}
Expand Down
58 changes: 58 additions & 0 deletions tools/src/test/scala/caliban/tools/ClientWriterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ object ClientWriterSpec extends DefaultRunnableSpec {
)
)

def genWithoutDeprecation(
schema: String,
scalarMappings: Map[String, String] = Map.empty,
additionalImports: List[String] = List.empty,
extensibleEnums: Boolean = false,
excludeClientDeprecation: Boolean = true
): RIO[Blocking, String] = Parser
.parseQuery(schema)
.flatMap(doc =>
Formatter.format(
ClientWriter
.write(
doc,
additionalImports = Some(additionalImports),
extensibleEnums = extensibleEnums,
excludeClientDeprecation = excludeClientDeprecation
)(
ScalarMappings(Some(scalarMappings))
)
.head
._2,
None
)
)

override def spec: ZSpec[TestEnvironment, Any] =
suite("ClientWriterSpec")(
testM("simple object type") {
Expand Down Expand Up @@ -558,6 +583,39 @@ bar$tripleQuotes,
def name: SelectionBuilder[Character, String] = _root_.caliban.client.SelectionBuilder.Field("name", Scalar())
}

}
"""
)
)
},
testM("deprecated field excluded when cli option is set to true") {
val schema =
"""
type Character {
"name"
name: String! @deprecated(reason: "blah")
nicknames: [String!]! @deprecated
}
""".stripMargin

assertM(genWithoutDeprecation(schema))(
equalTo(
"""import caliban.client.FieldBuilder._
import caliban.client._

object Client {

type Character
object Character {

/**
* name
*/
def name: SelectionBuilder[Character, String] = _root_.caliban.client.SelectionBuilder.Field("name", Scalar())
def nicknames: SelectionBuilder[Character, List[String]] =
_root_.caliban.client.SelectionBuilder.Field("nicknames", ListOf(Scalar()))
}

}
"""
)
Expand Down
Loading