【问题标题】:How to addFields for later defining relation with Sangria for graphQL?如何添加字段以便稍后为 graphQL 定义与 Sangria 的关系?
【发布时间】:2019-07-24 08:14:51
【问题描述】:

在派生对象类型时,我想使用 Sangria 的 AddFields 方法添加一个字段。我不知道如何填写参数“resolve =” 谁能帮帮我?

给出实体交付和系统。我派生了对象类型 SystemD4S。 现在我想在派生对象类型中添加一个类型为 SystemD4S 的字段“systemObject”以进行传递。不知道如何填写参数“resolve =”

case class Delivery (
                     docid: String,
                     override val docType: String = Doctype.DELIVERY,
                     system: String,
                     status: String,
                     items: List[DeliveryItem],
                     deleted: Boolean
                     ) extends EntityItemCollection {
  def getBusinessKey: String = s"$docid::$docType::$system"
}

case class System(id: String, company: String)

val SystemD4S = deriveObjectType[D4sEntityRepo, System](
    ObjectTypeDescription("system"))

val DeliveryD4S = deriveObjectType[D4sEntityRepo, Delivery](
    ObjectTypeDescription("delivery"),
    AddFields(Field("systemObject", SystemD4S, resolve = c => enitiesD4S.deferRel()))
  )

【问题讨论】:

    标签: scala graphql sangria


    【解决方案1】:

    resolve 是Context[Ctx, Val] ⇒ Action[Ctx, Res] 类型的函数。

    对于deriveObjectType[D4sEntityRepo, Delivery],您的CtxD4sEntityRepoValDeliveryResSystemD4S

    现在要在解析函数中获取enitiesD4S 实例,您可以使用c.ctx

    val DeliveryD4S = deriveObjectType[D4sEntityRepo, Delivery](
        ObjectTypeDescription("delivery"),
        AddFields(Field("systemObject", SystemD4S, resolve = c => c.ctx.deferRel()))
      )
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 2017-06-24
      • 2017-07-18
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多