This is a simple GraphQL-based API for managing authors, posts, and comments. It is built with Django and Graphene.
- Python 3.8+
- PostgreSQL
- Django
- Graphene-Django
- GraphQL-JWT
- Clone the repository:
git clone https://github.com/Marrwan/blog_api.git
cd blog-apiCreate and activate a virtual environment:
python3 -m venv venv
source venv/bin/activateInstall dependencies:
pip install -r requirements.txtSet up PostgreSQL database: Create a PostgreSQL database using the credentials provided in the .env file.
CREATE DATABASE blog_api;
CREATE USER postgres WITH PASSWORD 'your-password';
GRANT ALL PRIVILEGES ON DATABASE blog_api TO postgres;Run migrations:
python manage.py migrateCreate a superuser:
python manage.py createsuperuserRun the development server:
python manage.py runserverOpen your browser and navigate to http://127.0.0.1:8000/graphql/ to access the GraphQL playground.
A. First Create a user
python manage.py create_user.pyYou'll, get a reply like this:
User 'Abdulbasit' created successfully.
Username: Abdulbasit, password: marrwanah12B. After that, Login using the below query:
mutation {
tokenAuth(username: "Abdulbasit", password: "marrwanah12") {
token
payload
refreshExpiresIn
}
}You'll get a reply that looks lke this:
{
"data": {
"tokenAuth": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkFiZHVsYmFzaXQiLCJleHAiOjE3MjM5MDM4OTgsIm9yaWdJYXQiOjE3MjM4MTc0OTh9.RcLCT_fdKdiMt2c59eLvctsmTqiJChvcdJTqF3LSGjE",
"payload": {
"username": "Abdulbasit",
"exp": 1723903898,
"origIat": 1723817498
},
"refreshExpiresIn": 1724422298
}
}
}C. Copy the token: In GraphQL Playground, you can set HTTP headers for authentication. Follow these steps:
1. Open GraphQL Playground.
2. Click on the "HTTP HEADERS" button at the bottom left.
3. Add the Authorization header with the value Bearer YOUR_ACCESS_TOKEN.
like this
{
"Authorization": "Bearer YOUR_ACCESS_TOKEN"
}
Continue- Create an Author
mutation {
createAuthor(name: "John Doe", email: "john@example.com", bio: "A passionate writer.") {
author {
id
name
email
bio
}
errors
}
}- Update an Author
mutation {
updateAuthor(id: "1", name: "Jane Doe", email: "jane@example.com", bio: "An updated bio.") {
author {
id
name
email
bio
}
errors
}
}- Create a Post
mutation {
createPost(title: "My First Post", content: "This is the content of my first post.", authorId: 1) {
post {
id
title
content
createdAt
updatedAt
author {
id
name
}
}
errors
}
}- Update a Post
mutation {
updatePost(id: "1", title: "Updated Title", content: "Updated content.") {
post {
id
title
content
updatedAt
}
errors
}
}- Delete a Post
mutation {
deletePost(id: "1") {
success
errors
}
}- Create a Comment
mutation {
createComment(content: "This is a comment.", postId: 2) {
comment {
id
content
createdAt
post {
id
title
}
}
errors
}
}- Query All Posts
{
allPosts(authorId: 1) {
edges {
node {
id
title
content
author {
id
name
}
comments {
id
content
}
}
}
}
}- Query a Single Post
{
post(id: 2) {
id
title
content
author {
id
name
}
comments {
id
content
}
}
}- Query All Comments for a Post
{
allComments(postId: 2) {
id
content
createdAt
post {
id
title
}
}
}
Make sure the development server is running.
Run Tests
python manage.py test