Mapping on a deferred resolver

198 views
Skip to first unread message

Derek W

unread,
Jul 7, 2017, 5:57:40 PM7/7/17
to sangria-graphql
I've been using sangria-graphql for awhile now and have been loving how easy and flexible it is. My team has been really interested in using "deferred" fields for awhile now so we decided to start looking into it. So far I've liked playing with Fetchers; they seem pretty straightforward:
val campaignsFetcher = Fetcher { (ctx: LabelsContext, ids: Seq[Long]) =>
    ctx.fetchCampaignsByIds(ids.toSet)
  }(HasId(_.id))

  implicit val labelType = deriveObjectType[Unit, GraphQLLabel](
    ReplaceField(fieldName = "campaigns", field = Field(
      name = "campaigns",
      fieldType = listCampaignInfoType,
      description = Some("The campaign info for campaigns this label can be used within."),
      arguments = Argument("pageNumber", IntType) :: Argument("pageSize", IntType) :: Nil,
      resolve = (ctx) => {
        val pageSize = ctx.arg[Int]("pageSize")
        val pageNumber = ctx.arg[Int]("pageNumber")
        val campaignIds: List[Long] = ctx.value.campaigns.getOrElse(List.empty)
        val start = (pageNumber - 1) * pageSize
        campaignsFetcher.deferSeq(campaigns.slice(start, start + pageSize))
      }
    ))
  )

This all seems great, but I'd like to be able to return a full page object with the deferred results. I haven't been able (yet) to find a way to `map` or somehow manipulate that list returned by the fetcher. I'd love to do something like this:
campaignsFetcher.deferSeq(campaigns.slice(start, start + pageSize)).map { deferredCampaigns =>
  Page(data = deferredCampaigns,
    pageNumber = pageNumber,
    pageSize = pageSize,
    totalRecords = campaigns.size.toLong
  )
}

Does there exist a way to do this? Or perhaps I need to go create a custom DeferredResolver?

Omer Zach

unread,
Jul 7, 2017, 6:04:04 PM7/7/17
to Derek W, sangria-graphql
cWe do this:

  DeferredValue(meritFetcher.deferOpt(c.value.meritId)).map({
    case Some(merit) => merit.title
    case None => throw new Exception(s"Couldn't fetch merit corresponding to edge ${c.value.id}")
  })

Still pretty new to Sangria but I think you should be able to do the exact same thing with a Seq instead of an Opt.

--
You received this message because you are subscribed to the Google Groups "sangria-graphql" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sangria-graphql+unsubscribe@googlegroups.com.
To post to this group, send email to sangria-graphql@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sangria-graphql/797be069-1c9f-4c3f-a92a-ef03cda08847%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages