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

Make the schema comparison to consider the addition of a mandatory argument breaking #1147

Merged
merged 1 commit into from
Nov 17, 2021
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
4 changes: 3 additions & 1 deletion tools/src/main/scala/caliban/tools/SchemaComparison.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ object SchemaComparison {
): List[SchemaComparisonChange] = {
val leftKeys = left.keySet
val rightKeys = right.keySet
val added = (rightKeys -- leftKeys).map(ArgumentAdded(_, target)).toList
val added = (right -- leftKeys).map { case (name, arg) =>
ArgumentAdded(name, target, arg.ofType.nullable || arg.defaultValue.isDefined)
}.toList
val deleted = (leftKeys -- rightKeys).map(ArgumentDeleted(_, target)).toList
val commonFields = leftKeys intersect rightKeys
val changes = commonFields.toList.flatMap(key => compareArgumentDefinition(left(key), right(key), target))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ object SchemaComparisonChange {
override def breaking: Boolean = true
}

case class ArgumentAdded(argName: String, target: Target) extends SchemaComparisonChange {
override def toString: String = s"Argument '$argName' was added on $target."
case class ArgumentAdded(argName: String, target: Target, optional: Boolean) extends SchemaComparisonChange {
override def toString: String = s"Argument '$argName' was added on $target."
override def breaking: Boolean = !optional
}
case class ArgumentDeleted(argName: String, target: Target) extends SchemaComparisonChange {
case class ArgumentDeleted(argName: String, target: Target) extends SchemaComparisonChange {
override def toString: String = s"Argument '$argName' was deleted from $target."
override def breaking: Boolean = true
}
Expand Down
44 changes: 44 additions & 0 deletions tools/src/test/scala/caliban/tools/SchemaComparisonSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,50 @@ object SchemaComparisonSpec extends DefaultRunnableSpec {

compare(schema1, schema2, expected)
},
testM("optional argument added") {
val schema1: String =
"""
input HeroInput {
test: String
}
|""".stripMargin

val schema2: String =
"""
input HeroInput {
test: String
test2: String
}
|""".stripMargin

assertM(for {
s1 <- Parser.parseQuery(schema1)
s2 <- Parser.parseQuery(schema2)
diff = compareDocuments(s1, s2)
} yield diff)(hasFirst(hasField("breaking", _.breaking, equalTo(false))))
},
testM("non-optional argument added") {
val schema1: String =
"""
input HeroInput {
test: String
}
|""".stripMargin

val schema2: String =
"""
input HeroInput {
test: String
test2: String!
}
|""".stripMargin

assertM(for {
s1 <- Parser.parseQuery(schema1)
s2 <- Parser.parseQuery(schema2)
diff = compareDocuments(s1, s2)
} yield diff)(hasFirst(hasField("breaking", _.breaking, equalTo(true))))
},
testM("deprecated") {
val schema1: String =
"""
Expand Down