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

Add update cache methods to Fetcher #448

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Add update cache methods to Fetcher
  • Loading branch information
maxperrimond committed Jun 20, 2019
commit 8d23ec6a3fc565ff7851b00ae42ff9a58daf9c5f
8 changes: 7 additions & 1 deletion src/main/scala/sangria/execution/deferred/Fetcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class Fetcher[Ctx, Res, RelRes, Id](
def clearCachedRelId[RelId](deferredResolverState: Any, rel: Relation[Res, _, RelId], relId: RelId) =
findCache(deferredResolverState)(_.clearRelId(rel, relId))

def updateCache(deferredResolverState: Any, id: Id, value: Res) =
findCache(deferredResolverState)(_.update(id, value))

def updateCacheRel[RelId](deferredResolverState: Any, rel: Relation[Res, _, RelId], relId: RelId, values: Seq[Res]) =
findCache(deferredResolverState)(_.updateRel(rel, relId, idFn, values))

private def findCache(deferredResolverState: Any)(op: FetcherCache ⇒ Unit): Unit =
deferredResolverState match {
case map: Map[AnyRef, FetcherCache] @unchecked ⇒ map.get(this) match {
Expand Down Expand Up @@ -243,4 +249,4 @@ case class FetcherContext[Ctx](

class RelationNotSupportedError extends Exception(s"Relations are not supported by Fetcher.")

class RelationOnlySupportedError extends Exception(s"Only relations are supported by Fetcher.")
class RelationOnlySupportedError extends Exception(s"Only relations are supported by Fetcher.")
32 changes: 30 additions & 2 deletions src/test/scala/sangria/execution/deferred/FetcherSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ class FetcherSpec extends WordSpec with Matchers with FutureResultSupport {
Field("categoryEager", OptionType(CategoryType),
arguments = Argument("id", StringType) :: Nil,
resolve = c ⇒ c.ctx.getCategory(c.arg[String]("id"))),
Field("categoryAndCacheEmptyProducts", CategoryType,
arguments = Argument("id", StringType) :: Nil,
resolve = c ⇒ DeferredValue(fetcherCat.defer(c.arg[String]("id"))) map {
cat => {
fetcherProd.updateCacheRel(c.deferredResolverState, prodCat, cat.id, Seq())
cat
}
}),
Field("categoryNonOpt", CategoryType,
arguments = Argument("id", StringType) :: Nil,
resolve = c ⇒ fetcherCat.defer(c.arg[String]("id"))),
Expand Down Expand Up @@ -414,7 +422,7 @@ class FetcherSpec extends WordSpec with Matchers with FutureResultSupport {
resRelsOnly should be (resRelsOnlyCached)
}

"hansle complex relations" in {
"handle complex relations" in {
val query =
graphql"""
{
Expand Down Expand Up @@ -939,6 +947,26 @@ class FetcherSpec extends WordSpec with Matchers with FutureResultSupport {

fetchedProdIds.map(_.sorted) should be (Vector(Vector(2)))
}

"support manual cache updates at field resolver" in {
check(schema(), (), """
{
categoryAndCacheEmptyProducts(id: "4") {
name
productRelSeq {
name
}
}
}
""", Map(
"data" → Map(
"categoryAndCacheEmptyProducts" → Map(
"name" → "Cat 4",
"productRelSeq" → Vector(),
))),
resolver = defaultResolver,
userContext = new Repo)
}
}

"Fetcher" when {
Expand All @@ -950,4 +978,4 @@ class FetcherSpec extends WordSpec with Matchers with FutureResultSupport {
behave like properFetcher (sync.executionContext)
}
}
}
}