Skip to main content

Overview

Protocol: GraphQL over HTTPS Endpoint: https://api.petstoreapi.com/v1/graphql Query Language: GraphQL When to Use GraphQL:
  • ✅ Complex, nested data requirements
  • ✅ Mobile applications (reduce payload size)
  • ✅ Multiple resources in single request
  • ✅ Flexible, self-documenting API
  • ✅ Different clients need different fields
When NOT to Use GraphQL:
  • ❌ Simple CRUD operations (use REST instead)
  • ❌ File uploads (use REST instead)
  • ❌ Need HTTP caching (REST has better caching)

How GraphQL Works

Key Characteristics:
  • Request exactly the data you need
  • Single request for multiple resources
  • Strongly typed schema
  • Introspection (self-documenting)
  • No overfetching or underfetching

Basic Queries

List Pets

Response:

Get Single Pet

Variables:

Filter Pets

Pagination


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

Reuse common fields:

Directives

Conditionally include fields:

Aliases

Rename fields in response:

Multiple Queries


Introspection

Query the schema:
Get type details:

Best Practices

1. Use Specific Fields

2. Batch Queries

3. Use Fragments for Reuse


Error Handling

GraphQL errors are returned in the response:
Handle errors in code:

Comparison with REST


Interactive Documentation