# Paginate with Record Connections

| AVAILABLE API VERSION |
| --------------------- |
| API v56.0 and later   |

To retrieve more than 200 records, we recommend using [an upper-bound limit](/docs/platform/graphql/guide/paginate-use-upperbound.md). Using pagination without an upper-bound limit lets you retrieve up to 4,000 records only. In API v60.0 and later, you can [switch from record connection pagination to upper-bound limit pagination](/docs/platform/graphql/guide/paginate-use-upperbound.md#use-the-upper-bound-limit-in-a-subsequent-query), allowing you to retrieve more than 4,000 records.

## Paging Through Results

Record Connections paging uses the `first` and `after` arguments based on [relay record connections](https://relay.dev/graphql/connections.htm), which follows the specification for [forward pagination arguments](https://relay.dev/graphql/connections.htm#sec-Forward-pagination-arguments).

- The `first` argument is the maximum number of records to fetch as part of your query.

  :::note
  The initial query results can return any number of records up to the number you specify in the `first` argument. For example, if you request five records and the initial query result returns four records, use the `after` argument to page to the remaining record.
  :::

- The `after` argument is a cursor used to page through the result set. Use a returned cursor to continue paging through the result set from that cursor position. The `after` argument is typically set to the value of the `endCursor` field on the relay connection's `PageInfo`. If you want to page from a particular element in the result set, use the `cursor` field from a relay edge.

```sfdocs-code {"lang":"graphql", "title": "Query accounts with pagination", "src": "" }
query accounts {
  uiapi {
    query {
      Account {
        edges {
          node {
            Id
            Name {
              value
            }
          }
        }
        totalCount
        pageInfo {
          endCursor
          hasNextPage
          hasPreviousPage
        }
      }
    }
  }
}
```

The query results are as follows.

```sfdocs-code {"lang":"json", "title": "Query results with pagination information", "src": "" }
{
  "data": {
    "uiapi": {
      "query": {
        "Account": {
          "edges": [
            {
              "node": {
                "Id": "0011a000005slmbAAA",
                "Name": {
                  "value": "GenePoint"
                }
              }
            },
            {
              "node": {
                "Id": "0011a000005slmlAAA",
                "Name": {
                  "value": "United Oil &amp; Gas Corp"
                }
              }
            }, // 8 more account records (10 results by default)
          ],
          "totalCount": 26,
          "pageInfo": {
            "endCursor": "djE6OQ==",
            "hasNextPage": true,
            "hasPreviousPage": false
          }
        }
      }
    }
  },
  "errors": []
}
```

This example returns the next set of results after the `djE6OQ==` end cursor from the previous example.

```sfdocs-code {"lang":"graphql", "title": "Query with first and after arguments", "src": "" }
query accounts {
  uiapi {
    query {
      Account (first:5, after:"djE6OQ==") {
        edges {
          node {
            Id
            Name {
              value
            }
          }
        }
      }
    }
  }
}
```

The query results are as follows.

```sfdocs-code {"lang":"json", "title": "Query results after a given cursor", "src": "" }
{
  "data": {
    "uiapi": {
      "query": {
        "Account": {
          "edges": [
            {
              "node": {
                "Id": "0011a000005slmlAAA",
                "Name": {
                  "value": "United Oil &amp; Gas Corp"
                }
              }
            }, // more results
            }
          ]
        }
      }
    }
  },
  "errors": []
}
```

Here's an example query for accounts with cursors [using variables](https://graphql.org/learn/queries/#variables).

```sfdocs-code {"lang":"graphql", "title": "Query accounts with cursors", "src": "" }
query accountsWithCursors($first: Int, $after: String) {
  uiapi {
    query {
      Account(first: $first, after: $after) {
        edges {
          node {
            Id
            Name {
              value
            }
          }
          cursor
        }
        pageInfo {
          hasNextPage
          hasPreviousPage
          startCursor
          endCursor
        }
      }
    }
  }
}
```

The query returns the account IDs and names with cursor information.

```sfdocs-code {"lang":"json", "title": "Response for accounts with cursors query", "src": "" }
{
  "data": {
    "uiapi": {
      "query": {
        "Account": {
          "edges": [
            {
              "node": {
                "Id": "0015f00000Afmo9AAB",
                "Name": {
                  "value": "Burlington Textiles Corp of America"
                }
              },
              "cursor": "djE6Mw=="
            },
            {
              "node": {
                "Id": "0015f00000AfmoAAAR",
                "Name": {
                  "value": "Pyramid Construction Inc."
                }
              },
              "cursor": "djE6NA=="
            },
            {
              "node": {
                "Id": "0015f00000AfmoBAAR",
                "Name": {
                  "value": "Dickenson plc"
                }
              },
              "cursor": "djE6NQ=="
            },
            {
              "node": {
                "Id": "0015f00000AfmoCAAR",
                "Name": {
                  "value": "Grand Hotels &amp; Resorts Ltd"
                }
              },
              "cursor": "djE6Ng=="
            },
            {
              "node": {
                "Id": "0015f00000AfmoDAAR",
                "Name": {
                  "value": "United Oil &amp; Gas Corp."
                }
              },
              "cursor": "djE6Nw=="
            },
            {
              "node": {
                "Id": "0015f00000AfmoEAAR",
                "Name": {
                  "value": "Express Logistics and Transport"
                }
              },
              "cursor": "djE6OA=="
            },
            {
              "node": {
                "Id": "0015f00000AfmoFAAR",
                "Name": {
                  "value": "University of Arizona"
                }
              },
              "cursor": "djE6OQ=="
            }
          ],
          "pageInfo": {
            "hasNextPage": true,
            "hasPreviousPage": false,
            "startCursor": "djE6MA==",
            "endCursor": "djE6OQ=="
          }
        }
      }
    }
  },
  "errors": []
}
```

## Paging Through Record Connections Child Relationships

You can page through the child records on a queried parent record. Paging on the parent and children records at the same time isn't supported.
The child relationships records in the response are paginated and include cursor and pagination information.
Use the `first` argument to specify the number of records to return. The default is 10.

```sfdocs-code {"lang":"graphql", "title": "Query an account with children contacts", "src": "" }
query accounts {
  uiapi {
    query {
      Account ( where: { Name: { eq: "Edge Communications" } } ) {
        edges {
          node {
            Contacts ( first: 5 ) {
                edges {
                    cursor
                    node {
                        Name {
                          value
                        }
                    }
                }
                totalCount
                pageInfo {
                  hasNextPage
              }
             }
          }
        }
      }
    }
  }
}
```

The query results are as follows.

```sfdocs-code {"lang":"json", "title": "Query results with 2 children contact records", "src": "" }
{
  "data": {
    "uiapi": {
      "query": {
        "Account": {
          "edges": [
            {
              "node": {
                "Contacts": {
                  "edges": [
                    {
                      "cursor": "djE6MA==",
                      "node": {
                        "Name": {
                          "value": "Sean Forbes"
                        }
                      }
                    },
                    {
                      "cursor": "djE6MQ==",
                      "node": {
                        "Name": {
                          "value": "Rose Gonzalez"
                        }
                      }
                    }
                  ],
                  "totalCount": 2
                }
              }
            },
            {
              "node": {
                "Contacts": {
                  "edges": [],
                  "totalCount": 0
                }
              }
            }
          ]
        }
      }
    }
  },
  "errors": []
}
```

The maximum number of child relationship records to return can be specified using the `first` argument. However, if supplying a cursor value to the `after` argument of a child relationship, then the parent record must be limited to a single result. For example:

```sfdocs-code {"lang":"graphql", "title": "Query children records with cursors ", "src": "" }
query paginateChild {
  uiapi {
    query {
      Account(first:1) { # must limit to one result as we are paging through the child
        edges {
          node {
            Cases(first:100, after:"djE6Mw==") {
              edges {
                cursor
                node {
                  Id
                }
              }
            }
          }
        }
      }
    }
  }
}
```
