Query Examples¶
These examples build on the illustrative blog schema defined in
Sample Application — see examples/playground/ in the repo for a
complete runnable project. They cover single objects, filtered lists,
pagination and complex filtering.
Basic Object Queries¶
query GetPost($id: ID!) {
post(id: $id) {
id
title
slug
content
status
viewCount
createdAt
author {
id
username
firstName
lastName
profile {
bio
avatar
location
}
}
category {
id
name
slug
description
}
tags {
results {
id
name
color
}
}
comments {
results {
id
content
author {
username
}
createdAt
isApproved
parent {
id
content
}
}
totalCount
}
}
}
To-many relations are list types, not plain lists
tags and comments compile to TagListType / CommentListType, so
the sub-selection goes through results (which itself accepts limit,
offset and ordering) plus an optional totalCount. Only to-one
relations — author, category, profile, parent — are selected
directly.
{
"data": {
"post": {
"id": "1",
"title": "Getting Started with GraphQL and Django",
"slug": "getting-started-graphql-django",
"content": "GraphQL is a powerful query language...",
"status": "PUBLISHED",
"viewCount": 1245,
"createdAt": "2023-12-01T10:30:00",
"author": {
"id": "1",
"username": "john_doe",
"firstName": "John",
"lastName": "Doe",
"profile": {
"bio": "Full-stack developer passionate about GraphQL",
"avatar": "/media/avatars/john.jpg",
"location": "San Francisco, CA"
}
},
"category": {
"id": "1",
"name": "Technology",
"slug": "technology",
"description": "Latest in tech trends and tutorials"
},
"tags": {
"results": [
{
"id": "1",
"name": "GraphQL",
"color": "#e10098"
},
{
"id": "2",
"name": "Django",
"color": "#092e20"
}
]
},
"comments": {
"results": [
{
"id": "1",
"content": "Great tutorial! Very helpful.",
"author": {
"username": "reader1"
},
"createdAt": "2023-12-01T14:20:00",
"isApproved": true,
"parent": null
}
],
"totalCount": 1
}
}
}
}
Filtered Lists¶
query GetPostsByTags {
allPosts(filter: {
# Relation filters nest into the related type's own filter input,
# so the pk lookup lives under `id`, not directly on `tags`.
tags: { id: { in: ["1", "3", "5"] } } # GraphQL, React, Python
status: { exact: PUBLISHED }
}) {
results(limit: 20) {
id
title
tags {
results {
name
color
}
}
}
totalCount
}
}
Paginated Queries¶
Complex Filtering¶
query AdvancedPostSearch(
$categories: [ID!],
$tags: [ID!],
$authorName: String,
$minViews: Int,
$publishedAfter: DateTime,
$publishedBefore: DateTime
) {
allPosts(filter: {
category: { id: { in: $categories } }
tags: { id: { in: $tags } }
author: { username: { icontains: $authorName } }
viewCount: { gte: $minViews }
publishedAt: { range: [$publishedAfter, $publishedBefore] }
status: { exact: PUBLISHED }
}) {
results(limit: 20, ordering: "-published_at") {
id
title
excerpt
publishedAt
viewCount
author {
username
firstName
lastName
}
category {
name
slug
}
tags {
results {
name
color
}
}
}
totalCount
}
}