我有以下案例类:
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))
然后是 a 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
你能帮我找出我错过了什么吗?