Concepts of GraphQL and Relay

huangapple go评论100阅读模式
英文:

Concepts of GraphQL and Relay

问题

我正在尝试在golang中构建一个GraphQL端点。

我一直在按照这些博客Learn Golang + GraphQL + Relay #1来实现golang中的GraphQL,并且我已经成功构建了简单的GraphQL端点。

有一些概念对我来说仍然不太清楚,比如如何使用GraphQL构建一个分页端点,以及如何构建用于从这个查询中获取数据的端点,例如:

//如何构建用于从此查询中获取数据的端点
friends.first(1){
   cursor,
   node {
      name
   }
}

//或者这个查询
friends.last(1){
   cursor,
   node {
      name
   }
}

Concepts of GraphQL and Relay

作为一个来自Angular背景的人,这个Relay概念对我来说仍然非常困惑。Relay在促进客户端和服务器之间的通信方面起到了什么作用。

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

英文:

I am trying to build a GraphQL endpoints in golang.

I have been following these blogs Learn Golang + GraphQL + Relay #1 for implementation of graphQl in golang and I have successfully built simple endpoints of GraphQL.

There are few concepts that are still unclear to me as how can I build a pagination endpoints using graphQl,
Also methods like

//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
   }
}

Concepts of GraphQL and Relay

As, I am from Angular Background this Relay concept are still very confusing to me. How relay works in facilitating the communication between clients and server.

Please provide some example using Node or anything which make these concepts more clear

答案1

得分: 5

分页是一个复杂的问题,并且GraphQL并没有明确解决这个问题。GraphQL提供了一个数据获取框架,如何使用这个框架来实现分页等功能是开发者的责任。

Relay团队尝试通过游标连接规范来标准化GraphQL的分页。游标连接规范不仅适用于Relay,还可以与Angular一起使用,其主要目标是提供一种标准化的方式来处理来自GraphQL服务器的大型集合。这个规范的主要特点是每个对象都有一个关联的游标。这个关联的游标允许客户端从集合的任意点开始继续分页。

如果你有兴趣实现Relay规范,我建议你阅读Sashko Stubailo的文章Understanding pagination: REST, GraphQL, and Relay,他解释了Relay连接规范背后的动机,并介绍了如何实现它。

要查看连接规范的实际应用,请查看示例的SWAPI GraphQL API,它在GraphQL中实现了Relay规范。我建议你打开文档窗口并浏览PeopleConnection。注意他们如何实现edgespeople字段。

如果游标分页对你来说太复杂,你可以为你的GraphQL API提供传统的限制/偏移分页。接口可能会像这样:

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

最后,你提供的查询是一个非常早期的GraphQL技术预览,并不符合规范(来源)。一个更合适的查询(正如Hafiz所提到的)应该是:

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

我还推荐你观看React Europe 2015的两个很棒的演讲,它们更多地讨论了不使用Relay的GraphQL。

英文:

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
  }
}

Finally, the query you provided is from a very early technical preview of GraphQL and does not actually conform to the specification (source). A more appropriate query (as mentioned by Hafiz) would be:

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

There are two great talks from React Europe 2015 that I also recommend you watch which talk more about GraphQL without Relay.

huangapple
  • 本文由 发表于 2016年4月2日 05:35:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/36366208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定