-
-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Iterate through queried fields instead of object fields (#2021)
- Loading branch information
1 parent
47c5189
commit a36f832
Showing
5 changed files
with
62 additions
and
41 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
36 changes: 36 additions & 0 deletions
36
core/src/main/scala/caliban/schema/ObjectFieldResolver.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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package caliban.schema | ||
|
||
import caliban.execution.Field | ||
import caliban.schema.Step.{ MetadataFunctionStep, ObjectStep } | ||
|
||
import scala.collection.immutable.HashMap | ||
|
||
final private class ObjectFieldResolver[R, A]( | ||
objectName: String, | ||
fields: Iterable[(String, A => Step[R])] | ||
) { | ||
|
||
private val fieldsMap: java.util.HashMap[String, A => Step[R]] = { | ||
val map = new java.util.HashMap[String, A => Step[R]]() | ||
fields.foreach { case (name, resolve) => map.put(name, resolve) } | ||
map | ||
} | ||
|
||
def resolve(value: A): Step[R] = MetadataFunctionStep(resolveForField(value, _)) | ||
|
||
private def resolveForField( | ||
value: A, | ||
field: Field | ||
): Step[R] = { | ||
val fieldsBuilder = HashMap.newBuilder[String, Step[R]] | ||
var remaining = field.distinctFieldNames | ||
while (!remaining.isEmpty) { | ||
val name = remaining.head | ||
val resolve = fieldsMap.get(name) | ||
if (resolve eq null) () | ||
else fieldsBuilder += name -> resolve(value) | ||
remaining = remaining.tail | ||
} | ||
ObjectStep(objectName, fieldsBuilder.result()) | ||
} | ||
} |
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