Blog GraphQL API#
GraphQL API for a blog platform supporting posts, comments, user authentication, and real-time updates via subscriptions.
Overview#
This API follows standard GraphQL conventions:
Single endpoint:
/graphqlQuery operations for reading data
Mutation operations for writing data
Subscription operations for real-time updates
JWT-based authentication via Authorization header
Authentication#
All mutations and protected queries require a valid JWT token:
Authorization: Bearer <jwt_token>
Public queries (getPost, listPosts) do not require authentication.
Schema Types#
User#
type User {
id: ID!
username: String!
email: String!
displayName: String
avatarUrl: String
createdAt: String!
posts: [Post!]!
}
Post#
type Post {
id: ID!
title: String!
content: String!
excerpt: String
author: User!
published: Boolean!
tags: [String!]!
comments: [Comment!]!
createdAt: String!
updatedAt: String
}
Comment#
type Comment {
id: ID!
content: String!
author: User!
post: Post!
createdAt: String!
}
AuthPayload#
type AuthPayload {
token: String!
user: User!
}
PostInput#
input PostInput {
title: String!
content: String!
excerpt: String
tags: [String!]
published: Boolean
}
input CommentInput {
content: String!
}
Queries#
Get Single Post#
Retrieve a single post by ID with author and comments.
Query Example#
query GetPost($id: ID!) {
getPost(id: $id) {
id
title
content
author {
username
displayName
}
comments {
id
content
author {
username
}
}
createdAt
}
}
Response Example#
{
"data": {
"getPost": {
"id": "post_123",
"title": "Introduction to GraphQL",
"content": "GraphQL is a query language for APIs...",
"author": {
"username": "john_doe",
"displayName": "John Doe"
},
"comments": [
{
"id": "comment_456",
"content": "Great article!",
"author": {
"username": "jane_smith"
}
}
],
"createdAt": "2026-07-01T10:00:00Z"
}
}
}
List Posts#
Retrieve a paginated list of posts with optional filtering.
Query Example#
query ListPosts($page: Int, $limit: Int, $tag: String) {
listPosts(page: $page, limit: $limit, tag: $tag) {
id
title
excerpt
author {
username
}
tags
published
createdAt
}
}
Me (Current User)#
Get the currently authenticated user's profile.
Mutations#
Login#
Authenticate a user and obtain JWT token.
Mutation Example#
mutation Login($username: String!, $password: String!) {
login(username: $username, password: $password) {
token
user {
id
username
email
displayName
}
}
}
Response Example#
{
"data": {
"login": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": "user_789",
"username": "john_doe",
"email": "john@example.com",
"displayName": "John Doe"
}
}
}
}
Create Post#
Create a new blog post (authentication required).
Mutation Example#
mutation CreatePost($input: PostInput!) {
createPost(input: $input) {
id
title
content
author {
username
}
published
createdAt
}
}
Variables Example#
{
"input": {
"title": "Getting Started with MDI",
"content": "MDI (Markdown Interface) is a new way to define APIs...",
"excerpt": "Introduction to MDI concepts",
"tags": ["api", "markdown", "mdi"],
"published": true
}
}
Add Comment#
Add a comment to a post (authentication required).
Subscriptions#
New Comment Subscription#
Receive real-time notifications when new comments are added to a post.
Subscription Example#
subscription OnNewComment($postId: ID!) {
onNewComment(postId: $postId) {
id
content
author {
username
displayName
}
createdAt
}
}
Error Handling#
Errors follow the standard GraphQL error format with extensions.code for machine-readable error types:
Error Code |
Meaning |
|---|---|
UNAUTHORIZED |
Missing or invalid JWT token |
FORBIDDEN |
Insufficient permissions |
NOT_FOUND |
Requested resource does not exist |
VALIDATION_ERROR |
Input validation failed |
RATE_LIMITED |
Too many requests |
Checklist#
[x] Login mutation returns valid JWT token with correct credentials
[x] Login mutation returns INVALID_CREDENTIALS error with wrong password
[x] GetPost returns post data for valid ID
[x] GetPost returns NOT_FOUND for non-existent post ID
[x] ListPosts returns array of posts with default pagination
[x] CreatePost requires authentication
[x] CreatePost returns VALIDATION_ERROR for empty title
[x] Me query returns UNAUTHORIZED without token
[ ] Subscription real-time functionality
[ ] Rate limiting verification
[ ] Complex nested query performance testing