Skip to content

rafa761/flask-graphql-blog-api

Repository files navigation

Flask GraphQL Blog API

A modern blog API built with Flask and GraphQL using Strawberry.

Features

  • GraphQL API with Strawberry
  • JWT Authentication
  • Blog post management
  • User management
  • GraphQL Playground
  • Docker support

Quick Start

1. Clone and Setup

git clone <repository-url>
cd flask-graphql-blog-api
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -r requirements-dev.txt

2. Environment Setup

Copy .example.env and update with your settings:

cp .example.env .env
# Edit .env with your configuration

3. Initialize Database

python setup_db.py

This creates the database and adds sample data:

  • Admin user: admin / admin123
  • Author user: johndoe / password123
  • Sample blog posts

4. Run the Application

python app.py

Visit http://localhost:5000/api/graphql for the GraphQL Playground.

API Usage

Authentication

First, login to get an access token:

mutation {
  login(loginInput: { username: "admin", password: "admin123" }) {
    accessToken
    user {
      id
      username
      fullName
    }
  }
}

Query Posts

query {
  posts {
    id
    title
    excerpt
    isPublished
    author {
      username
      fullName
    }
  }
}

Create a PostModel

Include the JWT token in the Authorization header: Bearer <your-token>

mutation {
  createPost(
    postInput: {
      title: "My New PostModel"
      content: "This is the content of my post..."
      isPublished: true
    }
  ) {
    id
    title
    slug
    author {
      username
    }
  }
}

Search Posts

query {
  searchPosts(searchTerm: "GraphQL") {
    id
    title
    excerpt
  }
}

Graphql Login

Graphql Posts

Development

Project Structure

src/
├── app/
│   ├── __init__.py           # App factory
│   ├── config.py             # Configuration
│   ├── extensions.py         # Flask extensions
│   ├── models/               # SQLAlchemy models
│   ├── schemas/              # GraphQL types & schema
│   ├── services/             # Business logic
│   └── api/                  # GraphQL endpoints
├── app.py                    # Application entry point
└── setup_db.py              # Database initialization

Code Quality

# Format code
ruff format .

# Lint code
ruff check . --fix

# Pre-commit hooks
pre-commit run --all-files

Docker Development

cd dockerfiles
docker-compose -f docker-compose.dev.yml up --build

Available Queries

  • hello - Simple test query
  • posts(publishedOnly, limit) - Get all posts
  • post(id) - Get post by ID
  • postBySlug(slug) - Get post by slug
  • postsByAuthor(authorId) - Get posts by author
  • searchPosts(searchTerm) - Search posts
  • users - Get all users
  • user(id) - Get user by ID
  • me - Get current user (requires auth)

Available Mutations

  • register(userInput) - Register new user
  • login(loginInput) - Login user
  • createPost(postInput) - Create post (requires auth)
  • updatePost(id, postInput) - Update post (requires auth)
  • deletePost(id) - Delete post (requires auth)
  • publishPost(id) - Publish post (requires auth)

This is a portfolio project demonstrating modern Python API development practices.

About

Modern Flask GraphQL blog API built with Strawberry, JWT authentication, SQLAlchemy models, Docker support, and clean service-layer architecture.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages