Robins Gupta
Robins Gupta

Reputation: 3153

Concepts of GraphQL and Relay

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

Introducing Relay and GraphQL

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

Upvotes: 2

Views: 969

Answers (1)

Calebmer
Calebmer

Reputation: 2860

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.

Upvotes: 5

Related Questions