Skip to main content

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

curl http://localhost:8080/starwars_films/1
This returns detailed information about “A New Hope” including species and homeworld data.

Configuration

From config/krakend/krakend.json:
{
  "endpoint": "/starwars_films/{movie_id}",
  "backend": [
    {
      "host": ["https://swapi-graphql.netlify.app/"],
      "url_pattern": "/.netlify/functions/index",
      "target": "data.film",
      "extra_config": {
        "backend/graphql": {
          "type": "query",
          "query": "query Query ($movie_id: ID!) {\nfilm (filmID: $movie_id) {\nid\nepisodeID\ntitle\ndirector\nreleaseDate\nspeciesConnection {\nspecies {\nname\nclassification\nhomeworld {\nname\n}\n}\n}\n}\n}",
          "variables": {
            "movie_id": "{movie_id}"
          }
        }
      }
    }
  ]
}

How It Works

1. GraphQL Query Definition

The actual GraphQL query (formatted for readability):
query Query ($movie_id: ID!) {
  film (filmID: $movie_id) {
    id
    episodeID
    title
    director
    releaseDate
    speciesConnection {
      species {
        name
        classification
        homeworld {
          name
        }
      }
    }
  }
}
This query:
  • Accepts a $movie_id parameter
  • Fetches film details including nested species and homeworld data
  • Demonstrates GraphQL’s ability to request specific nested fields

2. Variable Mapping

"variables": {
  "movie_id": "{movie_id}"
}
The {movie_id} placeholder in the endpoint path is passed as the $movie_id variable to the GraphQL query.

3. Response Targeting

"target": "data.film"
GraphQL responses typically wrap results in a data object. The target parameter extracts just the film object, removing the GraphQL envelope. GraphQL Response:
{
  "data": {
    "film": {
      "id": "ZmlsbXM6MQ==",
      "title": "A New Hope"
    }
  }
}
KrakenD Response (after targeting):
{
  "id": "ZmlsbXM6MQ==",
  "title": "A New Hope"
}

Query Types

The type parameter specifies the GraphQL operation:
"type": "query"
Supported values:
  • query - Read operations (used in this example)
  • mutation - Write operations (create, update, delete)

Example Response

{
  "id": "ZmlsbXM6MQ==",
  "episodeID": 4,
  "title": "A New Hope",
  "director": "George Lucas",
  "releaseDate": "1977-05-25",
  "speciesConnection": {
    "species": [
      {
        "name": "Human",
        "classification": "mammal",
        "homeworld": {
          "name": "Tatooine"
        }
      },
      {
        "name": "Droid",
        "classification": "artificial",
        "homeworld": null
      }
    ]
  }
}

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

Advanced Usage

Multiple GraphQL Queries

You can aggregate multiple GraphQL queries:
{
  "endpoint": "/movie-and-character/{movie_id}/{character_id}",
  "backend": [
    {
      "host": ["https://swapi-graphql.netlify.app/"],
      "url_pattern": "/.netlify/functions/index",
      "extra_config": {
        "backend/graphql": {
          "type": "query",
          "query": "query ($movie_id: ID!) { film(filmID: $movie_id) { title } }",
          "variables": { "movie_id": "{movie_id}" }
        }
      },
      "group": "film"
    },
    {
      "host": ["https://swapi-graphql.netlify.app/"],
      "url_pattern": "/.netlify/functions/index",
      "extra_config": {
        "backend/graphql": {
          "type": "query",
          "query": "query ($character_id: ID!) { person(personID: $character_id) { name } }",
          "variables": { "character_id": "{character_id}" }
        }
      },
      "group": "character"
    }
  ]
}

Combining with Data Aggregation

GraphQL responses can be combined with REST API calls using Data Aggregation.

Learn More

Build docs developers (and LLMs) love