首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sangria graphql查询返回1个元素列表

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

Stack Overflow用户
提问于 2016-08-12 06:30:47
回答 1查看 463关注 0票数 0

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

代码语言:javascript
复制
  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))
      )
    )

延迟码是

代码语言:javascript
复制
  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]())))
  }

所有操作都适用于单个对象,但是当我尝试请求一个对象及其子对象时,我只得到一个子对象返回

查询:

代码语言:javascript
复制
{
    citizen(id: "12345") {
    name
    accounts {
      id
      amountDue
    }
  }
}

响应:

代码语言:javascript
复制
{
  "data": {
    "citizen": {
      "name": "12345",
      "accounts": [
        {
          "id": "12345",
          "amountDue": 12.34
        }
      ]
    }
  }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-12 20:03:52

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

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

代码语言:javascript
复制
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]())))
}

重要的是要确保对于deferred向量中的每个Deferred值,在结果列表中只有一个Future元素(并且它应该在列表中的相同位置)。

它是一个针对性能进行了优化的低级应用编程接口,因此在resolve方法的签名中没有太多的类型安全性。在这种情况下,我只是使用created an issue来改进错误报告。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38907092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档