
Why GraphQL over REST for GitHub data
REST endpoints often return large JSON payloads containing many fields you don’t need, which increases bandwidth and parsing overhead. GraphQL lets you request exactly the fields you want and compose nested data in a single request. Key benefits:- Select only the fields you need.
- Fetch nested related objects in one request (avoid multiple REST calls).
- Receive responses exactly shaped to your query, reducing bandwidth and parsing.
Example: REST workflow runs (problem statement)
A typical GET to list workflow runs returns many fields—even if you only care about the workflowname and its status.
HTTP request (example):
name, status, and conclusion, the remainder of this payload is wasted. GraphQL fixes that by letting you select only the fields you want.
GraphQL examples
Example: list organization members (name + avatar) — GraphQL:Making a GraphQL request to GitHub
Endpoint:query (and optional variables). Example: request rateLimit plus repository default branch and the latest commit metadata:
GraphQL query:
variables as the body of your POST to /graphql. The response will include only requested fields and a rateLimit object. GraphQL also returns a cost that indicates how many request points the query consumed.
GraphQL queries have a computed request cost. Large nested queries cost more tokens. Unauthenticated requests have much lower rate limits; you will likely hit the unauthenticated rate limit quickly for public repositories. Use an authenticated token to get higher limits and more headroom for expensive queries.
Example: large nested query (cost demonstration)
Complex nested query against the publickubernetes/kubernetes repository:
rateLimit object will show the cost used by the query.
Best practices and summary
- Use GraphQL when you need precise control over response fields or to aggregate nested related data in a single request.
- Always monitor the
rateLimitobject and the querycostto avoid surprises. - Authenticate your requests (use a PAT or app token) to increase rate limits and handle more expensive queries.
- Keep queries focused (limit
first:values and the depth of nested selections) to reduce cost and latency.
Links and references
- GitHub GraphQL API documentation
- GitHub REST API rate limiting
- GitHub Developer: Authenticating to the API