logo

Writing GraphQL Queries

GraphQL queries define the shape of the data you want. You write fields, and the response mirrors that structure.

query {
  user(id: 1) {
    name
    email
  }
}

Response:

{
  "data": {
    "user": {
      "name": "Alice",
      "email": "alice@example.com"
    }
  }
}

Nest fields to follow relationships:

query {
  user(id: 1) {
    name
    posts {
      title
      comments {
        text
      }
    }
  }
}

The query acts as a template for the response. If you don't request a field, it won't be returned - even if it exists.

Arguments like id: 1 filter or specify what you want. Each field can have its own arguments.

I explain query patterns in my GraphQL with Python course.