File size: 3,698 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
[Table of contents](../../readme.md) / [API](../intro.md) / [GraphQL](./intro.md)

# Pagination with GraphQL

Even though there is no specific built-in way to paginate GraphQL queries there is a quasi-standard that most people (including us) follow called [Relay Connections Specification](https://facebook.github.io/relay/graphql/connections.htm). Rather than reading a spec that might not help you in pratical applications we recommend reading these two article to get a grasp of the why and how:

- [Understanding pagination: REST, GraphQL and Relay](https://dev-blog.apollodata.com/understanding-pagination-rest-graphql-and-relay-b10f835549e7): Get a sense of the issues we're facing with the app and how to solve them
- [Explaining GraphQL Connections](https://dev-blog.apollodata.com/explaining-graphql-connections-c48b7c3d6976): We follow this specific structure to the dot. (with one tiny change in naming)

## TL;DR

The TL;DR of how to use it is:

```GraphQL

{

  thread(id: "some-thread-id") {

    # Fetch the messages of a certain thread

    messageConnection {

      pageInfo {

        # Do we have another page to fetch after this

        hasNextPage

      }

      edges {

        # Pass the cursor of the last message to messageConnections

        # to fetch the next page

        cursor

        # The actual message:

        node {

          id

          message {

            content

          }

        }

      }

    }

  }

}

```

This query would get the first 10 (or less if there's less in total) messages of that thread. To get the next page you have to take the `cursor` of the last message edge and pass that to messageConnections:

```GraphQL

{

  thread(id: "some-thread-id") {

    # Fetch the next messages after the last message in the thread

    messageConnection(after: $lastMessageCursor) {

      edges {

        node {

          message {

            content

          }

        }

      }

    }

  }

}

```

> Note: The cursor is an opaque data structure, meaning it might refer to something you understand or it might not. It's also not promised to be consistent, especially between sessions, resources, etc. This boils down to **do not use the cursor for anything other than passing it to the query for the next page**, no matter if you want to use it for something else.

To specify how many messages you want to load use the `first` parameter:

```GraphQL

{

  thread(id: "some-thread-id") {

    # Fetch the first 5 messages after the last message

    messageConnection(first: 5, after: $lastMessageCursor) {

      edges {

        node {

          message {

            content

          }

        }

      }

    }

  }

}

```

> Note: The default for `first` is normally 10, but might be changed depending on the resource being fetched. Make sure to check GraphiQL/the types to figure out what the default is.

## Naming conventions

Connections and edges have standard names and structures across all resources:

```GraphQL

# A connection of a story to messages

type StoryMessagesConnection {

  pageInfo: PageInfo!

  edges: [StoryMessageEdge!]

}



# An edge from a story to a message

type StoryMessageEdge {

  cursor: String!

  node: Message!

}



type Story {

  messageConnection(first: Int = 10, after: String): StoryMessagesConnection!

}

```

> Note: This is where we diverge slightly from the article linked above, it recommends naming your edges in plural (`StoryMessagesEdge`) to make it consistent with the connection but we've found that the singular (`StoryMessageEdge`) makes it clear only a single resource is being fetched and think that's more important.