Query Examples¶
These examples build on the schema from Sample Application. 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 {
id
name
color
}
comments {
id
content
author {
username
}
createdAt
isApproved
parent {
id
content
}
}
}
}
{
"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": [
{
"id": "1",
"name": "GraphQL",
"color": "#e10098"
},
{
"id": "2",
"name": "Django",
"color": "#092e20"
}
],
"comments": [
{
"id": "1",
"content": "Great tutorial! Very helpful.",
"author": {
"username": "reader1"
},
"createdAt": "2023-12-01T14:20:00",
"isApproved": true,
"parent": null
}
]
}
}
}
Filtered Lists¶
Paginated Queries¶
Complex Filtering¶
query AdvancedPostSearch(
$categories: [ID!],
$tags: [ID!],
$authorName: String,
$minViews: Int,
$publishedAfter: Date,
$publishedBefore: Date
) {
allPosts(filter: {
category: { in: $categories }
tags: { 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 {
name
color
}
}
totalCount
}
}