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

Improved execution field merging #2019

Merged
merged 4 commits into from
Nov 25, 2023
Merged
Changes from 2 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
44 changes: 22 additions & 22 deletions core/src/main/scala/caliban/execution/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -305,29 +305,29 @@ object Executor {
private[caliban] def fail(error: CalibanError): UIO[GraphQLResponse[CalibanError]] =
ZIO.succeed(GraphQLResponse(NullValue, List(error)))

private[caliban] def mergeFields(field: Field, typeName: String): List[Field] = {
val map = new java.util.LinkedHashMap[String, Field]()
var modified = false

field.fields.foreach { field =>
if (field._condition.forall(_.contains(typeName))) {
map.compute(
field.aliasedName,
(_, f) =>
if (f == null) field
else {
modified = true
f.copy(fields = f.fields ::: field.fields)
}
)
} else {
modified = true
}
}
private[caliban] def mergeFields(field: Field, typeName: String): List[Field] =
field.fields match {
// Shortcut if all the fields have the same condition, which means we don't need to dedup
// as that's been handled in Field.apply
case head :: tail if tail.forall(_._condition == head._condition) =>
if (head._condition.forall(_.contains(typeName))) field.fields
else Nil
case Nil => Nil
case fields =>
val map = new java.util.LinkedHashMap[String, Field]()
fields.foreach { field =>
kyri-petrou marked this conversation as resolved.
Show resolved Hide resolved
if (field._condition.forall(_.contains(typeName))) {
map.compute(
field.aliasedName,
(_, f) =>
if (f == null) field
kyri-petrou marked this conversation as resolved.
Show resolved Hide resolved
else f.copy(fields = f.fields ::: field.fields)
)
}
}

// Avoid conversions if no modification took place
if (modified) map.values().asScala.toList else field.fields
}
map.values().asScala.toList
}

private def fieldInfo(field: Field, path: List[Either[String, Int]], fieldDirectives: List[Directive]): FieldInfo =
FieldInfo(field.aliasedName, field, path, fieldDirectives, field.parentType)
Expand Down