简体   繁体   English

GraphQL和中继的概念

[英]Concepts of GraphQL and Relay

I am trying to build a GraphQL endpoints in golang . 我正在尝试在golang建立GraphQL endpoints

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 . 我一直在关注这些博客了解Golang + GraphQL +接力#1实施的graphQlgolang ,我已经成功地构建简单的端点GraphQL

There are few concepts that are still unclear to me as how can I build a pagination endpoints using graphQl , Also methods like 我仍然不清楚一些概念,因为我该如何使用graphQl构建pagination端点,还有诸如

//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简介

As, I am from Angular Background this Relay concept are still very confusing to me. 因为我来自Angular Background,所以Relay概念仍然让我感到困惑。 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 请提供一些使用Node或其他使这些概念更清晰的示例

Pagination is never an easy question and not one explicitly solved by GraphQL. 分页从来都不是一个简单的问题,也不是GraphQL明确解决的一个问题。 GraphQL provides a data fetching framework, how that framework is used to do things (like pagination) is up to the developer. GraphQL提供了一个数据获取框架,该框架如何用于做事(例如分页)取决于开发人员。

The Relay team has attempted to standardize pagination with the cursor connection specification for GraphQL. 中继团队已尝试使用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. 游标连接规范不是Relay独有的,可以与Angular一起使用,该规范的主要目标是提供一种使用GraphQL服务器处理大型集合的标准化方法。 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. 如果您对实现中继规范感兴趣,我鼓励您阅读Sashko Stubailo的《 理解分页:REST,GraphQL和Relay》 ,其中他解释了中继连接规范背后的动机并解释了如何实现它。

To see the connection specification in action, take a look at the example SWAPI GraphQL API which implements the Relay specification in GraphQL. 要查看实际的连接规范,请查看示例SWAPI GraphQL API ,该示例在GraphQL中实现了中继规范。 I encourage you to open the documentation window and browse the PeopleConnection . 我鼓励您打开文档窗口并浏览PeopleConnection Note how they implement both edges and people fields. 注意它们如何同时实现edgespeople字段。

If cursor pagination is too complicated for you to use/implement, you could always expose a traditional limit/offset pagination for your GraphQL API. 如果游标分页太复杂而无法使用/实现,则可以始终为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 ). 最后,您提供的查询来自GraphQL的早期技术预览,并且实际上不符合规范( )。 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. React Europe 2015有两个很棒的演讲,我也建议您观看其中更多有关不带中继的GraphQL的演讲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM