Skip to main content

Basic Limit-Based Pagination

For simple queries, you can use basic limit-based pagination:
query {
  lessons(limit: 10, offset: 20) {
    identifier
    name
    gradeLevel
  }
}

Basic Cursor-Based Pagination

The API also supports cursor-based pagination with Connection fields. All paginated queries use the pattern sConnection:
query {
  lessonsConnection(first: 10, after: $cursor) {
    edges {
      node {
        identifier
        name
        gradeLevel
      }
      cursor
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}

Implementation

import os
from gql import Client, gql
from gql.transport.requests import RequestsHTTPTransport

# Setup client
transport = RequestsHTTPTransport(
    url=os.getenv("API_ENDPOINT"),  # Set to the API endpoint
    headers={"x-api-key": os.getenv("API_KEY")},  # Your API key from 1Password
    use_json=True,
)

client = Client(transport=transport, fetch_schema_from_transport=True)

def fetch_all_lessons():
    cursor = None
    has_next_page = True
    all_lessons = []

    query = gql("""
    query FetchLessons($first: Int!, $after: String, $where: LessonWhere) {
        lessonsConnection(first: $first, after: $after, where: $where) {
            edges {
                node {
                    identifier
                    name
                    gradeLevel
                    courseCode
                }
                cursor
            }
            pageInfo {
                hasNextPage
                endCursor
            }
        }
    }
    """)

    while has_next_page:
        variables = {
            "first": 50,
            "after": cursor,
            "where": None  # Add filters here if needed
        }

        result = client.execute(query, variable_values=variables)
        connection = result["lessonsConnection"]
        nodes = [edge["node"] for edge in connection["edges"]]
        all_lessons.extend(nodes)

        has_next_page = connection["pageInfo"]["hasNextPage"]
        cursor = connection["pageInfo"]["endCursor"]

    return all_lessons

# Example usage
if __name__ == "__main__":
    lessons = fetch_all_lessons()
    print(f"Fetched {len(lessons)} lessons")

Pagination with Filtering

You can combine pagination with filtering for more targeted queries:
query {
  lessonsConnection(
    where: { gradeLevel_INCLUDES: "K" }
    first: 10
    after: $cursor
  ) {
    edges {
      node {
        identifier
        name
        gradeLevel
      }
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}
Note: For very large datasets, consider using smaller page sizes (25-50 items) to optimize performance and avoid timeouts. The client side can use retry logic that automatically reduces page size exponentially on server errors (e.g., from 50 to 10 to 2 items) to handle resource constraints gracefully.