GraphQL API

  • Single Endpoint: A GraphQL API typically has a single endpoint through which all data is fetched.

  • Client-Driven Queries: The client specifies exactly what data it needs, which minimizes over-fetching (getting more data than needed) and under-fetching (not getting enough data).

  • Structured Responses: The response matches the request, allowing clients to shape the structure of the response to their needs.

  • Nested Queries: It supports complex nested data structures in a single request, which is useful for related data (e.g., querying a user and their posts).

  • No Versioning: GraphQL doesn’t require versioning because clients can request only the fields they need. As a result, new features can be added to an existing schema without breaking existing clients.

  • Error Reporting: GraphQL returns data and errors in the same response structure, so clients can still receive partial data when a non-fatal error occurs.

  • Strongly Typed Schema: GraphQL uses a type system to define the schema, so clients know what data and types are available. This can improve the development experience by enabling better tooling and validation.

Single Endpoint: /graphql

query {
  user(id: 1) {
    id
    name
    email
    posts {
      postId
      title
      content
    }
  }
}

Response:

{
  "data": {
    "user": {
      "id": 1,
      "name": "John Doe",
      "email": "john.doe@example.com",
      "posts": [
        {
          "postId": 101,
          "title": "My first post",
          "content": "This is the content of my first post."
        },
        {
          "postId": 102,
          "title": "Another day, another post",
          "content": "Here's some more content."
        }
      ]
    }
  }
}

Last updated