How to addFields for later defining relation with Sangria for graphQL?

242 Views Asked by At

While deriving an object type i want to add an field with the AddFields-methode of Sangria. I don't know how to fill the parameter "resolve = " Can anyone help me?

Given are the entities Delivery and System. I derived the object Type SystemD4S. Now I want to add a field "systemObject" with type SystemD4S in the derived object type for delivery. Don't know how to fill the parameter "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()))
  )
1

There are 1 best solutions below

0
Gagandeep Kalra On

resolve is a function of type Context[Ctx, Val] ⇒ Action[Ctx, Res].

For deriveObjectType[D4sEntityRepo, Delivery], your Ctx is D4sEntityRepo, Val is Delivery and Res is SystemD4S

Now to get the enitiesD4S instance in your resolve function, you can use c.ctx.

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

Hope this helps.