无法理解Fetcher.rel

时间:2018-04-14 16:38:14

标签: scala sangria

我有以下案例类:

case class OrganizationId(value: Long) extends AnyVal
case class Organization(id: OrganizationId, name: String, iban: Option[String], bic: Option[String], updatedAt: LocalDateTime, insertedAt: LocalDateTime)
case class EventId(value: Long) extends AnyVal
case class Event(
  id: EventId, organizationId: OrganizationId, name: String, description: Option[String], updatedAt: LocalDateTime, insertedAt: LocalDateTime
)

它们代表一个简单的架构,Organization可以有多个Event 基本上我正在尝试定义两个graphql查询:一个列出所有组织及其嵌套事件,另一个查询是另一种方式:列出所有事件及其相应的组织。在这两种情况下,我都想避免N + 1个查询。我认为两个查询都可以在每个2个SQL查询中运行 为了解决这个问题,我查看了高级Fetch API,特别是Fetcher.rel部分。如果我理解正确,我需要定义这样的关系:val byOrganization = Relation[Event, OrganizationId]("byOrganization", e => Seq(e.organizationId))然后是Fetcher.rel,但我无法使其正常工作。
这是我的架构定义的完整代码:

class GraphqlSchema {
  val byOrganization = Relation[Event, OrganizationId]("byOrganization", e => Seq(e.organizationId))
  val organizationsFetcher = Fetcher(
    (context: GraphqlContext, ids: Seq[OrganizationId]) => {
      context.organizations.all(ids)
    }
  )(HasId(_.id))
  val eventsFetcher = Fetcher.rel[GraphqlContext, Event, Organization, EventId](
    (context: GraphqlContext, ids: Seq[EventId]) => {
      context.events.all(ids)
    },
    (context: GraphqlContext, ids: RelationIds[Event]) => {
      ???
    }
  )(HasId(_.id))

  val deferredResolver = DeferredResolver.fetchers(organizationsFetcher, eventsFetcher)

  implicit val OrganizationIdType = CustomScalarTypes.organiaztionIdType
  implicit val EventIdType = CustomScalarTypes.eventIdType
  implicit val UuidType = CustomScalarTypes.uuidType
  implicit val LocalDateTimeType = CustomScalarTypes.localDateTimeType
  implicit val UserType = deriveObjectType[GraphqlContext, User]()
  implicit val OrganizationType: ObjectType[GraphqlContext, Organization] = deriveObjectType[GraphqlContext, Organization](
    AddFields(
      Field("events", ListType(EventType), resolve = (ctx) => {
        eventsFetcher.deferRelSeq(byOrganization, ctx.value.id)
      })
    )
  )
  implicit val EventType: ObjectType[GraphqlContext, Event] = deriveObjectType[GraphqlContext, Event](
    AddFields(
      Field("organization", OptionType(OrganizationType), resolve = (ctx) => {
        organizationsFetcher.deferOpt(ctx.value.organizationId)
      })
    )
  )

  val QueryType = deriveContextObjectType[GraphqlContext, Query, Unit](_.query)
  val MutationType = deriveContextObjectType[GraphqlContext, Mutation, Unit](_.mutation)

  val schema = Schema(QueryType, Some(MutationType))
}

它没有编译,我收到此错误: ound:

[error] /Users/matthieu/Code/runevent/runevent/scala/api/src/main/scala/com/runevent/api/graphql/GraphqlSchema.scala:39:35: type mismatch;
[error]  found   : sangria.execution.deferred.Relation[com.runevent.database.models.Event,com.runevent.database.models.Event,com.runevent.database.models.OrganizationId]
[error]  required: sangria.execution.deferred.Relation[com.runevent.database.models.Event,com.runevent.database.models.Organization,?]
[error]         eventsFetcher.deferRelSeq(byOrganization, ctx.value.id)
[error]                                   ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

你能帮我找出我错过的东西吗?

1 个答案:

答案 0 :(得分:2)

我认为eventsFetcher应该像这样定义(因此ResRelRes类型参数属于同一类型Event):

val eventsFetcher = Fetcher.rel[GraphqlContext, Event, Event, EventId](
  (context: GraphqlContext, ids: Seq[EventId]) => {
    context.events.all(ids)
  },
  (context: GraphqlContext, ids: RelationIds[Event]) => {
    context.events.loadByOrganizationIds(ids(byOrganization))
  }
)(HasId(_.id))