我有以下案例类:
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))
}
它无法编译,我收到此错误:奥德 :
[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
你能帮我找出我错过了什么吗?
我认为eventsFetcher
应该这样定义(因此Res
和RelRes
类型参数都属于同一类型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))