GraphQL和中继的概念

罗宾斯·古普塔(Robins Gupta):

我试图建立一个GraphQL endpointsgolang

我一直在关注这些博客,了解学习Golang + GraphQL + Relay#1来实现graphQlin,golang并且已经成功构建了的简单端点GraphQL

我仍然不清楚一些概念,因为我该如何使用来构建pagination端点graphQl,还有诸如

//How to build endpoints for fetching data from this query
friends.first(1){
   cursor,
   node {
      name
   }
}

//or this query
friends.last(1){
   cursor,
   node {
      name
   }
}

中继和GraphQL简介

因为我来自Angular背景,这个Relay概念仍然让我感到困惑。中继如何工作以促进客户端和服务器之间的通信。

请提供一些使用Node或其他使这些概念更清晰的示例

Calebmer:

Pagination is never an easy question and not one explicitly solved by GraphQL. GraphQL provides a data fetching framework, how that framework is used to do things (like pagination) is up to the developer.

The Relay team has attempted to standardize pagination with the cursor connection specification for GraphQL. The cursor connection specification is not unique to Relay and can be used with Angular, the main goal of the specification is to provide a standardized way to work with large collections from a GraphQL server. The main thing to note about this specification is that with every object there is an associated cursor. This associated cursor allows the client to resume pagination from any point in the collection.

If you are interested in implementing the Relay specification I encourage you to read Sashko Stubailo's Understanding pagination: REST, GraphQL, and Relay where he explains the motivation behind the Relay connection specification and explains how to implement it.

To see the connection specification in action, take a look at the example SWAPI GraphQL API which implements the Relay specification in GraphQL. I encourage you to open the documentation window and browse the PeopleConnection. Note how they implement both edges and people fields.

If cursor pagination is too complicated for you to use/implement, you could always expose a traditional limit/offset pagination for your GraphQL API. The interface would probably look something like this:

{
  friends(limit: 5, offset: 20) {
    name
  }
}

最后,您提供的查询来自GraphQL的早期技术预览,并且实际上不符合规范()。更合适的查询(如哈菲兹所提到的)将是:

{
  friends(first: 1) {
    cursor
    node {
      name
    }
  }
}

React Europe 2015有两个很棒的演讲,我也建议您观看其中有关GraphQL Without Relay的更多演讲。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章