-
-
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
Changes from 3 commits
7456d0d
e3a91d3
adc7ec9
4c5d713
f6eb15f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
Comment on lines
+7
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fairly annoying we have to extend |
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 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package caliban.schema | ||
|
||
import caliban.CalibanError.ExecutionError | ||
import caliban.Value.NullValue | ||
import caliban.ResponseValue.ObjectValue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused imports ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed. By the way shall we add a linter to check for unused imports and run it during CI only? It's pretty easy to miss them unfortunately There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that'd be nice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I'll add it in a separate PR 👍 |
||
import caliban.Value.{ EnumValue, NullValue, StringValue } | ||
import caliban.execution.{ Field, FieldInfo } | ||
import caliban.{ InputValue, PathValue, ResponseValue } | ||
import zio.query.ZQuery | ||
|
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.
Are imports right?