Skip to content

GraphQL API

GraphQL is a query language for APIs and a runtime for executing those queries with your existing data. It was developed by Facebook and later open-sourced as a specification. GraphQL provides a more efficient, powerful, and flexible alternative to the traditional REST API.

In a GraphQL API, clients can request exactly the data they need, and nothing more. This allows for a more efficient use of network resources and eliminates over-fetching of data. GraphQL also provides a strongly-typed schema, making it easier to understand and work with the API.

Why GraphQL?

  1. Efficiency: Clients can request only the data they need, reducing the amount of data transferred over the network.
  2. Flexibility: Clients can specify their data requirements, and the server can evolve its schema without impacting existing clients.
  3. Stability: GraphQL provides a more stable API compared to REST by allowing the addition of new objects while still maintaining compatibility with existing objects. This means that as your API evolves and new features are added, clients can continue to use the existing objects without any disruptions.

Example Query

Consider a simple example of querying information about a product:

Request:

query {
product(request: { slug: "Juul-vervet-jersey-blazer" }) {
name
description
slug
priceInclTax {
currencyCode
amount
}
}
}

Response:

{
"data": {
"product": {
"name": "Juul Velvet Jersey Blazer",
"description": "A softer, contemporary take on classic velvet. Woman by Earn goes to lengths when it comes to elegance and sophistication. Sand colored blazer Juul comes in lush velvet stretch jersey with a smooth reverse side. Equipped with Lasercut edge finishes.\n\n\n- Lapel collar\n- 2-button closure\n- Long slim sleeves\n- Welt pockets\n- Inside-out seams\n- Lasercut edge finish\n- Medium-weight velvet stretch jersey, smooth reverse side\n- Runs true to size, fitted\n- Hip length\n- Rosalinde is 1.75m and wears size34 (XS)",
"slug": "Juul-vervet-jersey-blazer",
"priceInclTax": {
"currencyCode": "EUR",
"amount": 289.95
}
}
}
}

This query requests information about a user with ID 123, including their ID, name, email, and a list of posts with their titles and content.

Making Queries

To make GraphQL queries, send a POST request to the GraphQL endpoint with a JSON payload containing the query. Set the Content-Type header to application/json.