除了分页数据之外,是否可以扩展 graphql 响应?

Is it possible to extend graphql response other than just data for pagination?

在 GraphQL 中,响应通常如下所示。

{
  "data": [{
    "id": 1,
    "username": "Jon Snow",
    "email": "crow@northofthew.all",
    "age": 20
  }, {
    "id": 2,
    "username": "Tyrion Lannister",
    "email": "drunk@i.mp",
    "age": 34
  }, {
    "id": 3,
    "username": "Sansa Stark",
    "email": "redhead@why.me",
    "age": 17
  }]
}

是否可以将元数据添加到您的响应中,例如像这样的分页。

{
  "pagination": {
    "total": 14,
    "count": 230,
  },
  "data": [{
    "id": 1,
    "username": "Jon Snow",
    "email": "crow@northofthew.all",
    "age": 20
  }, {
    "id": 2,
    "username": "Tyrion Lannister",
    "email": "drunk@i.mp",
    "age": 34
  }]
}

我正在使用 express-graphql,目前将这些分页设置为自定义响应 header,这很好,但还可以做得更好。由于 GraphQL 响应已经用 "data" 包裹,因此在其响应中添加更多 "data" 并不是很奇怪。

根据规范,加强@CommonsWare 已经声明的内容,这将是一个无效的 GraphQL 响应。关于分页,Relay 有自己的分页方法,称为 connections,但实际上,其他几种方法也是可行的,甚至在某些情况下更合适(连接不是灵丹妙药)。

我想通过补充说 GraphQL 的分层性质促使相关数据处于同一级别来补充已经说过的话。一个例子胜过千言万语,所以这里是:

query Q {
  pagination_info { # what is this info related to? completely unclear
    total
    count
  }
  user {
    friends {
      id
    }
  }
}

而是...

query Q {
  user {
    friends {
      pagination_info { # fairly obvious that this is related to friends
        total
        count
      }
      friend {
        id
      }
    }
  }
}