-
-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve empty union objects within Sum schema derivation & cleanups (#…
…2121) * Resolve empty union objects within Sum schema derivation * Cleanup * Make Scala 2.12 and MiMa happy * Remove unused imports * Use `.contains(true)` instead of `.exists(identity)`
- Loading branch information
1 parent
1e67c31
commit 26373cf
Showing
12 changed files
with
138 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
core/src/main/scala/caliban/introspection/adt/__DeprecatedArgs.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
package caliban.introspection.adt | ||
|
||
import scala.runtime.AbstractFunction1 | ||
|
||
case class __DeprecatedArgs(includeDeprecated: Option[Boolean] = None) | ||
|
||
// NOTE: This object extends AbstractFunction1 to maintain binary compatibility for Scala 2.13. | ||
// TODO: Remove inheritance in the next major version | ||
object __DeprecatedArgs extends AbstractFunction1[Option[Boolean], __DeprecatedArgs] { | ||
val include: __DeprecatedArgs = __DeprecatedArgs(Some(true)) | ||
|
||
def apply(v: Option[Boolean] = None): __DeprecatedArgs = new __DeprecatedArgs(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package caliban.schema | ||
|
||
import caliban.ResponseValue.ObjectValue | ||
import caliban.Value.{ EnumValue, NullValue, StringValue } | ||
import caliban.introspection.adt.{ __DeprecatedArgs, __Field, __Type } | ||
import caliban.schema.Step.MetadataFunctionStep | ||
|
||
private[schema] object SchemaUtils { | ||
private val fakeField = | ||
Some( | ||
List( | ||
__Field( | ||
"_", | ||
Some( | ||
"Fake field because GraphQL does not support empty objects. Do not query, use __typename instead." | ||
), | ||
_ => Nil, | ||
() => Types.makeScalar("Boolean") | ||
) | ||
) | ||
) | ||
|
||
def isEmptyUnionObject(t: __Type): Boolean = | ||
t.fields(__DeprecatedArgs.include).contains(Nil) | ||
|
||
// see https://github.com/graphql/graphql-spec/issues/568 | ||
def fixEmptyUnionObject(t: __Type): __Type = | ||
if (isEmptyUnionObject(t)) t.copy(fields = (_: __DeprecatedArgs) => fakeField) | ||
else t | ||
|
||
def resolveEmptyUnionStep[R](step: Step[R]): Step[R] = step match { | ||
case s @ PureStep(EnumValue(v)) => | ||
MetadataFunctionStep[R] { field => | ||
field.fields.view | ||
.filter(_._condition.forall(_.contains(v))) | ||
.collectFirst { | ||
case f if f.name == "__typename" => ObjectValue(List(f.aliasedName -> StringValue(v))) | ||
case f if f.name == "_" => NullValue | ||
} | ||
.fold(s)(PureStep(_)) | ||
} | ||
case _ => step | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters