-
-
Notifications
You must be signed in to change notification settings - Fork 249
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
Resolve empty union objects within Sum schema derivation & cleanups #2121
Merged
kyri-petrou
merged 5 commits into
series/2.x
from
optimize-step-reduction-pattern-matching
Feb 19, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7456d0d
Resolve empty union objects within Sum schema derivation
kyri-petrou e3a91d3
Cleanup
kyri-petrou adc7ec9
Make Scala 2.12 and MiMa happy
kyri-petrou 4c5d713
Remove unused imports
kyri-petrou f6eb15f
Use `.contains(true)` instead of `.exists(identity)`
kyri-petrou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fairly annoying we have to extend
AbstractFunction1
to make MiMA happy with Scala 2.13. We can remove this for 2.6.0