PetstoreAPI GraphQL Protocol Guide
Modern Petstore API provides GraphQL support for flexible, efficient data fetching where clients request exactly what they need in a single query. Our GraphQL endpoint at https://api.petstoreapi.com/v1/graphql enables complex nested queries, multiple resources in single requests, and precise field selection to reduce payload size for mobile applications.
Overview#
Protocol: GraphQL over HTTPSEndpoint: https://api.petstoreapi.com/v1/graphql✅ Complex, nested data requirements
✅ Mobile applications (reduce payload size)
✅ Multiple resources in single request
✅ Flexible, self-documenting API
✅ Different clients need different fields
❌ Simple CRUD operations (use REST instead)
❌ File uploads (use REST instead)
❌ Need HTTP caching (REST has better caching)
How GraphQL Works#
Client GraphQL Server
│ │
├──── Query ─────────────────────>│
│ { pets { id name species } } │
│ │
│ ┌────────┴────────┐
│ │ Parse Query │
│ │ Validate │
│ │ Execute │
│ └────────┬────────┘
│ │
│<──── JSON Response ─────────────┤
│ { "data": { "pets": [...] } } │
Request exactly the data you need
Single request for multiple resources
Introspection (self-documenting)
No overfetching or underfetching
Basic Queries#
List Pets#
{
"data": {
"pets": [
{
"id": "019b4132-70aa-764f-b315-e2803d882a24",
"name": "Buddy",
"species": "DOG",
"breed": "Golden Retriever",
"ageMonths": 24,
"status": "AVAILABLE"
}
]
}
}
Get Single Pet#
{
"id": "019b4132-70aa-764f-b315-e2803d882a24"
}
Filter Pets#
Mutations#
Create Pet#
Update Pet#
Delete Pet#
Nested Queries#
Pet with Owner#
Order with Items#
Authentication#
Include JWT token in Authorization header:
Code Examples#
JavaScript (fetch)#
JavaScript (Apollo Client)#
Python (gql)#
Advanced Features#
Fragments#
Directives#
Conditionally include fields:Aliases#
Rename fields in response:Multiple Queries#
Introspection#
Best Practices#
1. Use Specific Fields#
2. Batch Queries#
3. Use Fragments for Reuse#
Error Handling#
GraphQL errors are returned in the response:{
"data": {
"pet": null
},
"errors": [
{
"message": "Pet not found",
"path": ["pet"],
"extensions": {
"code": "PET_NOT_FOUND"
}
}
]
}
Comparison with REST#
| Feature | GraphQL | REST |
|---|
| Data Fetching | Exact fields | Fixed responses |
| Requests | Single for multiple resources | Multiple endpoints |
| Overfetching | No | Yes (often) |
| Underfetching | No | Yes (n+1 problem) |
| Caching | Complex | HTTP caching built-in |
| Schema | Strongly typed | Varies |
| Documentation | Self-documenting | Separate (OpenAPI) |
Interactive Documentation#
Modified at 2026-01-06 09:25:30