Skip to content

Commit

Permalink
improved errors on stiching
Browse files Browse the repository at this point in the history
ValdemarGr committed Jan 19, 2024
1 parent c6cbd35 commit bb430c1
Showing 2 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -132,15 +132,19 @@ object StreamInterpreter {
} <* debug("done interpreting")

// Patch the previously emitted json data
val o: F[(JsonObject, Chain[EvalFailure])] = evalled.map { case (jsons, errs) =>
val o: F[(JsonObject, Chain[EvalFailure])] = evalled.flatMap { case (jsons, errs) =>
val allJsons = jsons ++ paddedErrors
val allErrs = errs ++ Chain.fromSeq(preparedRoots.toList).flatMap { case (es, _, _) => es }

val stitched = allJsons.foldLeft(prevJo) { case (accum, (pos, patch)) =>
stitchInto(accum.asJson, patch, pos).asObject.get
val stitched = allJsons.foldLeftM(prevJo) { case (accum, (pos, patch)) =>
Temporal[F]
.fromEither {
stitchInto(accum.asJson, patch, pos, Cursor.empty).value.value
}
.map(_.asObject.get)
}

(stitched, allErrs)
stitched.tupleRight(allErrs)
}

o.map { case (o, errs) => (o, Result(errs, o).some) }
@@ -152,19 +156,39 @@ object StreamInterpreter {
}
}

def stitchInto(oldTree: Json, subTree: Json, path: Cursor): Json =
path.uncons match {
case None => subTree
final case class StitchFailure(
currentPath: Cursor,
remainingPath: Cursor,
msg: String
) extends Exception(msg) {
override def toString = s"StitchFailure($currentPath, $remainingPath, $msg)"

override def getMessage(): String = toString
}
def stitchInto(oldTree: Json, subTree: Json, remainingPath: Cursor, currentPath: Cursor): EitherT[Eval, StitchFailure, Json] = {
remainingPath.uncons match {
case None => EitherT.pure(subTree)
case Some((p, tl)) =>
val newCurrent = currentPath.add(p)
def err(msg: String) = StitchFailure(newCurrent, tl, msg)
p match {
case GraphArc.Field(name) =>
val oldObj = oldTree.asObject.get
val oldValue = oldObj(name).get
val newSubTree = stitchInto(oldValue, subTree, tl)
oldObj.add(name, newSubTree).asJson
for {
oldObj <-
EitherT.fromOption[Eval](oldTree.asObject, err(s"Expected object at $name, but got ${oldTree.name}"))
oldValue <-
EitherT.fromOption[Eval](oldObj(name), err(s"Expected field $name in object, but found nothing"))
newSubTree <- stitchInto(oldValue, subTree, tl, newCurrent)
} yield oldObj.add(name, newSubTree).asJson
case GraphArc.Index(index) =>
val oldArr = oldTree.asArray.get
oldArr.updated(index, stitchInto(oldArr(index), subTree, tl)).asJson
for {
oldArr <-
EitherT.fromOption[Eval](oldTree.asArray, err(s"expected array at, but found ${oldTree.name}"))
atIndex <-
EitherT.fromOption[Eval](oldArr.get(index.toLong), err(s"expected array at $index, but found nothing"))
newSubTree <- stitchInto(atIndex, subTree, tl, newCurrent)
} yield oldArr.updated(index, newSubTree).asJson
}
}
}
}
1 change: 0 additions & 1 deletion modules/server/src/test/scala/gql/BatchTest.scala
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ import cats.implicits._
import gql.resolver.Resolver
import gql.resolver.ShowMissingKeys
import io.circe.JsonObject
import cats.effect

class BatchTest extends CatsEffectSuite {
case class Batchers(

0 comments on commit bb430c1

Please sign in to comment.