| 
 | 1 | +[[pagination]]  | 
 | 2 | += Pagination  | 
 | 3 | +:page-aliases: pagination/index.adoc, pagination/offset-based.adoc, pagination/cursor-based.adoc, queries-aggregation/pagination/index.adoc, queries-aggregation/pagination/offset-based.adoc, queries-aggregation/pagination/cursor-based.adoc  | 
 | 4 | + | 
 | 5 | + | 
 | 6 | +The Neo4j GraphQL Library offers two mechanisms for pagination:  | 
 | 7 | + | 
 | 8 | +* Offset-based pagination - Pagination based on offsets, often associated with navigation via pages.  | 
 | 9 | +* Cursor-based pagination - Pagination based on cursors, often associated with infinitely-scrolling applications.  | 
 | 10 | + | 
 | 11 | + | 
 | 12 | +[[pagination-offset-based]]  | 
 | 13 | +== Offset-based pagination  | 
 | 14 | + | 
 | 15 | +Offset-based pagination, often associated with navigation via pages, can be achieved through the use of the `offset` and `limit` options available when querying for data.  | 
 | 16 | + | 
 | 17 | +Using the following type definition:  | 
 | 18 | + | 
 | 19 | +[source, graphql, indent=0]  | 
 | 20 | +----  | 
 | 21 | +type User @node {  | 
 | 22 | +    name: String!  | 
 | 23 | +}  | 
 | 24 | +----  | 
 | 25 | + | 
 | 26 | +Fetch the first page of 10 by executing:  | 
 | 27 | + | 
 | 28 | +[source, graphql, indent=0]  | 
 | 29 | +----  | 
 | 30 | +query {  | 
 | 31 | +    users(limit: 10) {  | 
 | 32 | +        name  | 
 | 33 | +    }  | 
 | 34 | +}  | 
 | 35 | +----  | 
 | 36 | + | 
 | 37 | +And then on subsequent calls, introduce the `offset` argument and increment it by 10 on each call.  | 
 | 38 | + | 
 | 39 | +Fetch the second page with:  | 
 | 40 | + | 
 | 41 | +[source, graphql, indent=0]  | 
 | 42 | +----  | 
 | 43 | +query {  | 
 | 44 | +    users(offset: 10, limit: 10) {  | 
 | 45 | +        name  | 
 | 46 | +    }  | 
 | 47 | +}  | 
 | 48 | +----  | 
 | 49 | + | 
 | 50 | +Fetch the third page with:  | 
 | 51 | + | 
 | 52 | +[source, graphql, indent=0]  | 
 | 53 | +----  | 
 | 54 | +query {  | 
 | 55 | +    users(offset: 20, limit: 10) {  | 
 | 56 | +        name  | 
 | 57 | +    }  | 
 | 58 | +}  | 
 | 59 | +----  | 
 | 60 | + | 
 | 61 | +And so on.  | 
 | 62 | + | 
 | 63 | + | 
 | 64 | +=== Total number of pages  | 
 | 65 | + | 
 | 66 | +You can fetch the total number of records for a certain type using its count query, and then divide that number by your entries per page in order to calculate the total number of pages.  | 
 | 67 | +This determines what the last page is, and whether there is a next page.  | 
 | 68 | + | 
 | 69 | +See xref::queries-aggregations/queries.adoc[Count] queries for details on how to execute these queries.  | 
 | 70 | + | 
 | 71 | + | 
 | 72 | +=== Paginating relationship fields  | 
 | 73 | + | 
 | 74 | +Say that in addition to the `User` type above, there is also a `Post` type which a `User` has many of.  | 
 | 75 | +You can also fetch a `User` and then paginate through their posts:  | 
 | 76 | + | 
 | 77 | +[source, graphql, indent=0]  | 
 | 78 | +----  | 
 | 79 | +query {  | 
 | 80 | +    users(where: {  | 
 | 81 | +        name_EQ: "Billy"  | 
 | 82 | +    }) {  | 
 | 83 | +        name  | 
 | 84 | +        posts(offset: 20, limit: 10) {  | 
 | 85 | +            content  | 
 | 86 | +        }  | 
 | 87 | +    }  | 
 | 88 | +}  | 
 | 89 | +----  | 
 | 90 | + | 
 | 91 | + | 
 | 92 | +[[pagination-cursor-based]]  | 
 | 93 | +== Cursor-based pagination  | 
 | 94 | + | 
 | 95 | +On relationship fields, you are able to take advantage of cursor-based pagination, which is often associated with infinitely-scrolling applications.  | 
 | 96 | + | 
 | 97 | +Using the following type definition:  | 
 | 98 | + | 
 | 99 | +[source, graphql, indent=0]  | 
 | 100 | +----  | 
 | 101 | +type User @node {  | 
 | 102 | +    name: String!  | 
 | 103 | +    posts: [Post!]! @relationship(type: "HAS_POST", direction: OUT)  | 
 | 104 | +}  | 
 | 105 | +
  | 
 | 106 | +type Post @node {  | 
 | 107 | +    content: String!  | 
 | 108 | +}  | 
 | 109 | +----  | 
 | 110 | + | 
 | 111 | +If you wanted to fetch the posts of user "John Smith" 10 at a time, you would first fetch 10:  | 
 | 112 | + | 
 | 113 | +[source, graphql, indent=0]  | 
 | 114 | +----  | 
 | 115 | +query {  | 
 | 116 | +    users(where: { name_EQ: "John Smith" }) {  | 
 | 117 | +        name  | 
 | 118 | +        postsConnection(first: 10) {  | 
 | 119 | +            edges {  | 
 | 120 | +                node {  | 
 | 121 | +                    content  | 
 | 122 | +                }  | 
 | 123 | +            }  | 
 | 124 | +            pageInfo {  | 
 | 125 | +                endCursor  | 
 | 126 | +                hasNextPage  | 
 | 127 | +            }  | 
 | 128 | +        }  | 
 | 129 | +    }  | 
 | 130 | +}  | 
 | 131 | +----  | 
 | 132 | + | 
 | 133 | +In the return value, if `hasNextPage` is `true`, you would pass `endCursor` into the next query of 10.  | 
 | 134 | +You can do this with a variable, for example, `$after` in the following query:  | 
 | 135 | + | 
 | 136 | +[source, graphql, indent=0]  | 
 | 137 | +----  | 
 | 138 | +query Users($after: String) {  | 
 | 139 | +    users(where: { name_EQ: "John Smith" }) {  | 
 | 140 | +        name  | 
 | 141 | +        postsConnection(first: 10, after: $after) {  | 
 | 142 | +            edges {  | 
 | 143 | +                node {  | 
 | 144 | +                    content  | 
 | 145 | +                }  | 
 | 146 | +            }  | 
 | 147 | +            pageInfo {  | 
 | 148 | +                endCursor  | 
 | 149 | +                hasNextPage  | 
 | 150 | +            }  | 
 | 151 | +        }  | 
 | 152 | +    }  | 
 | 153 | +}  | 
 | 154 | +----  | 
 | 155 | + | 
 | 156 | +You may continue until `hasNextPage` is `false` in the return - this is when you have reached the end of the data.  | 
 | 157 | + | 
 | 158 | + | 
 | 159 | +=== `totalCount`  | 
 | 160 | + | 
 | 161 | +The Connection fields also offer a `totalCount` field which can be used to calculate page numbers.  | 
 | 162 | +This is useful if you want to paginate by cursor but use page numbers in your application.  | 
 | 163 | + | 
 | 164 | +Add the `totalCount` field which returns the total number of results matching the filter used, for example:  | 
 | 165 | + | 
 | 166 | +[source, graphql, indent=0]  | 
 | 167 | +----  | 
 | 168 | +query Users($after: String) {  | 
 | 169 | +    users(where: { name_EQ: "John Smith" }) {  | 
 | 170 | +        name  | 
 | 171 | +        postsConnection(first: 10) {  | 
 | 172 | +            edges {  | 
 | 173 | +                node {  | 
 | 174 | +                    content  | 
 | 175 | +                }  | 
 | 176 | +            }  | 
 | 177 | +            pageInfo {  | 
 | 178 | +                endCursor  | 
 | 179 | +                hasNextPage  | 
 | 180 | +            }  | 
 | 181 | +            totalCount  | 
 | 182 | +        }  | 
 | 183 | +    }  | 
 | 184 | +}  | 
 | 185 | +----  | 
0 commit comments