Overview
KrakenD can consume GraphQL APIs and expose them as traditional REST endpoints. This allows clients to access GraphQL data using simple HTTP GET requests without learning GraphQL query syntax.Example: Star Wars Films
The/starwars_films/{movie_id} endpoint queries the Star Wars GraphQL API and returns film data as a REST response.
Try It
Configuration
Fromconfig/krakend/krakend.json:
How It Works
1. GraphQL Query Definition
The actual GraphQL query (formatted for readability):- Accepts a
$movie_idparameter - Fetches film details including nested species and homeworld data
- Demonstrates GraphQL’s ability to request specific nested fields
2. Variable Mapping
{movie_id} placeholder in the endpoint path is passed as the $movie_id variable to the GraphQL query.
3. Response Targeting
data object. The target parameter extracts just the film object, removing the GraphQL envelope.
GraphQL Response:
Query Types
Thetype parameter specifies the GraphQL operation:
query- Read operations (used in this example)mutation- Write operations (create, update, delete)
Example Response
Benefits
For Clients
- Simple REST Interface - No need to construct GraphQL queries
- Standard HTTP GET - Works with any HTTP client
- URL Parameters - Familiar RESTful patterns
For APIs
- GraphQL Power - Leverage GraphQL’s efficient data fetching
- Nested Data - Request related data in a single query
- Type Safety - GraphQL schema validation
For Teams
- Gradual Migration - Expose GraphQL gradually as REST
- Legacy Support - Support clients that only speak REST
- API Gateway Pattern - Centralize GraphQL complexity