Scala sangria graphql查询返回1个元素列表

Scala sangria graphql查询返回1个元素列表,scala,graphql,sangria,Scala,Graphql,Sangria,我使用sangria作为GraphQL服务器。模式的相关部分是: val Account = ObjectType( "Account", "An account with a municipal unit", fields[Unit, Account]( Field("id", StringType, Some("The account id"), resolve = _.value.id), Field("mu"

我使用sangria作为GraphQL服务器。模式的相关部分是:

  val Account =
    ObjectType(
      "Account",
      "An account with a municipal unit",
      fields[Unit, Account](
        Field("id", StringType, Some("The account id"), resolve = _.value.id),
        Field("mu", OptionType(MunicipalUnit), Some("The municipal unit this account is with"), resolve = ctx => ctx.ctx.asInstanceOf[ObjectResolver].resolve[MunicipalUnit](ctx.value.mu)),
        Field("eu", OptionType(EconomicUnit), Some("The economic unit this account belongs to"), resolve = ctx => ctx.ctx.asInstanceOf[ObjectResolver].resolve[EconomicUnit](ctx.value.eu)),
        Field("location", OptionType(Location), Some("The physical location associated with this account"), resolve = ctx => ctx.ctx.asInstanceOf[ObjectResolver].resolve[Location](ctx.value.location)),
        Field("amountDue", BigDecimalType, Some("The amount currently due"), resolve = _.value.amountDue)
      ))

  val Citizen =
    ObjectType(
      "Citizen",
      "A Citizen",
      interfaces[Unit, Citizen](EconomicUnit),
      fields[Unit, Citizen](
        Field("id", StringType, Some("The ID of the citizen"), resolve = _.value.id),
        Field("name", StringType, Some("The name of the citizen"), resolve = _.value.id),
        Field("delegates", OptionType(ListType(OptionType(EconomicUnit))), Some("The delegates of the citizen"), resolve = ctx => DeferDelegates(ctx.value.delegates)),
        Field("locations", OptionType(ListType(OptionType(Location))), Some("The locations of the citizen"), resolve = ctx => DeferLocations(ctx.value.locations)),
        Field("accounts", OptionType(ListType(OptionType(Account))), Some("The accounts of the citizen"), resolve = ctx => DeferAccounts(ctx.value.accounts))
      )
    )
延迟代码为

  def resolveByType[T](ids: List[Any])(implicit m: Manifest[T]) = ids map (id => resolver.resolve[T](id))

  override def resolve(deferred: Vector[Deferred[Any]], ctx: Any) = deferred flatMap {
    case DeferAccounts(ids) => resolveByType[Account](ids)
    case DeferLocations(ids) => resolveByType[Location](ids)
    case DeferDelegates(ids) => resolveByType[EconomicUnit](ids)
    case DeferMUs(ids) => resolveByType[MunicipalUnit](ids)

    case _ =>
      List(Future.fromTry(Try(List[Any]())))
  }
每件事都适用于单个对象,但是当我试图请求一个对象及其子对象时,我只得到一个子对象返回

查询:

{
    citizen(id: "12345") {
    name
    accounts {
      id
      amountDue
    }
  }
}
答复:

{
  "data": {
    "citizen": {
      "name": "12345",
      "accounts": [
        {
          "id": "12345",
          "amountDue": 12.34
        }
      ]
    }
  }
}

所以-这是正确的,我可以在后端看到列表的所有元素都在加载,但它们似乎没有返回。

问题是,您正在使用
flatMap
并将不相关列表的所有元素合并到一个结果列表中

我认为这些小小的改变会达到理想的效果:

def resolveByType[T](ids: List[Any])(implicit m: Manifest[T]): Future[Seq[T]] = 
  Future.sequence(ids map (id => resolver.resolve[T](id)))

override def resolve(deferred: Vector[Deferred[Any]], ctx: Any) = deferred map {
  case DeferAccounts(ids) => resolveByType[Account](ids)
  case DeferLocations(ids) => resolveByType[Location](ids)
  case DeferDelegates(ids) => resolveByType[EconomicUnit](ids)
  case DeferMUs(ids) => resolveByType[MunicipalUnit](ids)

  case _ =>
    List(Future.fromTry(Try(List[Any]())))
}
它的引入是为了确保对于
递延
向量中的每个
递延
值,在结果列表中只有一个
未来
元素(它应该在列表中的相同位置)


它是针对性能优化的低级API,因此
resolve
方法的签名中没有太多类型安全性。我只是想改进这种情况下的错误报告

问题在于您正在使用
flatMap
并将不相关列表的所有元素合并到一个结果列表中

我认为这些小小的改变会达到理想的效果:

def resolveByType[T](ids: List[Any])(implicit m: Manifest[T]): Future[Seq[T]] = 
  Future.sequence(ids map (id => resolver.resolve[T](id)))

override def resolve(deferred: Vector[Deferred[Any]], ctx: Any) = deferred map {
  case DeferAccounts(ids) => resolveByType[Account](ids)
  case DeferLocations(ids) => resolveByType[Location](ids)
  case DeferDelegates(ids) => resolveByType[EconomicUnit](ids)
  case DeferMUs(ids) => resolveByType[MunicipalUnit](ids)

  case _ =>
    List(Future.fromTry(Try(List[Any]())))
}
它的引入是为了确保对于
递延
向量中的每个
递延
值,在结果列表中只有一个
未来
元素(它应该在列表中的相同位置)

它是针对性能优化的低级API,因此
resolve
方法的签名中没有太多类型安全性。我只是想改进这种情况下的错误报告