【问题标题】:Sangria GraphQL: How to mix deferred fields, deriveObjectType, and case classesSangria GraphQL:如何混合延迟字段、派生对象类型和案例类
【发布时间】:2018-06-25 17:55:29
【问题描述】:

我很好奇是否可以将案例类的字段定义为延迟,同时仍使用deriveObjectType 宏定义其他所有内容。

这是一个例子。仪表板包含一系列选项卡:

case class Tab(id: Long, dashboardId: Long, name: String, order: Long)

case class Dashboard(id: Long, name: String, tabs: Seq[Tab])

我正在推迟使用Fetcher 解析Dashboard.tabs 字段,并且我想继续使用deriveObjectType 宏(如果可能)。所以这就是我如何定义我的ObjectTypes:

val TabType = deriveObjectType[Unit, Dashboard]()

val DashboardType = deriveObjectType[Unit, Dashboard](
  AddFields(
    fields =
      Field(
        name = "tabs",
        fieldType = ListType(TabType),
        resolve = ctx => {
          TabsFetcher.fetcher.defer(ctx.value.id)
        }
      )
  )
)

但是,当我运行代码时,我收到以下错误:

sangria.schema.NonUniqueFieldsError: All fields within 'Dashboard' type should have unique names! Non-unique fields: 'tabs'.

如果我从 Dashboard 案例类中删除 tabs 字段,错误就会消失,但我会失去使用案例类的一些好处(尤其是在单元测试中)。如果我避免使用deriveObjectType 宏(并手动定义DashboardObjectType),那么我将失去宏的好处(更少样板)。

所以,我很好奇是否有更好的方法或其他方法来解决这个问题,而不是在不使用宏的情况下定义 DashboardType 或从 Dashboard 案例类中删除 tags 字段。

(我希望可能有某种@GraphQLDeferred 注释可以应用于Dashboard.tabs 字段或类似的东西???)

【问题讨论】:

    标签: scala graphql sangria


    【解决方案1】:

    你几乎猜对了。您需要使用ReplaceField 而不是AddFields。或者,您可以ExcludeFields("tabs") 并继续使用AddFields

    【讨论】:

      猜你喜欢
      • 2019-01-03
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 2017-04-28
      相关资源
      最近更新 更多