Skip to content

Commit

Permalink
Created a graphql server
Browse files Browse the repository at this point in the history
  • Loading branch information
MdSamsuzzohaShayon committed Nov 3, 2023
1 parent 8d3ef46 commit 7188715
Show file tree
Hide file tree
Showing 27 changed files with 365 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/server/.venv
/**/**/__pycache__
/client/node_modules
/client/.cache
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/the-freak-lesson.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added server/__init__.py
Empty file.
Empty file added server/blog/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions server/blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.contrib import admin
from .models import Tag, ArticleTags, Article, Author, Category, Comment

# Register your models here.

admin.site.register(Tag)
admin.site.register(ArticleTags)
admin.site.register(Article)
admin.site.register(Author)
admin.site.register(Category)
admin.site.register(Comment)
6 changes: 6 additions & 0 deletions server/blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BlogConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'blog'
76 changes: 76 additions & 0 deletions server/blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Generated by Django 4.2.7 on 2023-11-03 09:12

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Article',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('content', models.TextField()),
('publication_date', models.DateField()),
],
),
migrations.CreateModel(
name='Author',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('email', models.EmailField(max_length=254)),
('bio', models.TextField()),
],
),
migrations.CreateModel(
name='Category',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Tag',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name='Comment',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('author_name', models.CharField(max_length=100)),
('email', models.EmailField(max_length=254)),
('comment_text', models.TextField()),
('comment_date', models.DateField()),
('article_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blog.article')),
],
),
migrations.CreateModel(
name='ArticleTags',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('article_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blog.article')),
('tag_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blog.tag')),
],
),
migrations.AddField(
model_name='article',
name='author_id',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blog.author'),
),
migrations.AddField(
model_name='article',
name='category_id',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blog.category'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Generated by Django 4.2.7 on 2023-11-03 09:27

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('blog', '0001_initial'),
]

operations = [
migrations.RenameField(
model_name='article',
old_name='author_id',
new_name='author',
),
migrations.RenameField(
model_name='article',
old_name='category_id',
new_name='category',
),
migrations.RenameField(
model_name='article',
old_name='publication_date',
new_name='created_at',
),
migrations.RenameField(
model_name='articletags',
old_name='article_id',
new_name='article',
),
migrations.RenameField(
model_name='articletags',
old_name='tag_id',
new_name='tag',
),
migrations.RenameField(
model_name='comment',
old_name='article_id',
new_name='article',
),
migrations.RenameField(
model_name='comment',
old_name='author_name',
new_name='author',
),
migrations.RenameField(
model_name='comment',
old_name='comment_date',
new_name='created_at',
),
migrations.RenameField(
model_name='comment',
old_name='comment_text',
new_name='text',
),
]
Empty file.
51 changes: 51 additions & 0 deletions server/blog/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from django.db import models

# Create your models here.

class Author(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
bio = models.TextField()

def __str__(self):
return self.name

class Category(models.Model):
name = models.CharField(max_length=100)

def __str__(self):
return self.name


class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
created_at = models.DateField()
author = models.ForeignKey(Author, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)

def __str__(self):
return self.title


class Comment(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
author = models.CharField(max_length=100) # Who made the comment
email = models.EmailField()
text = models.TextField()
created_at = models.DateField()

def __str__(self):
return self.comment_date


class Tag(models.Model):
name = models.CharField(max_length=255)

def __str__(self):
return self.name

# ArticleTags Table (a junction table to implement many-to-many relationship between Articles and Tags):
class ArticleTags(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
tag = models.ForeignKey(Tag, on_delete=models.CASCADE)
Empty file.
57 changes: 57 additions & 0 deletions server/blog/resolvers/mutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import graphene
from graphene_django import DjangoObjectType
from ..models import Category, Article
from .queryTypes import CategoryType, ArticleType


class CategoryMutation(graphene.Mutation):
class Arguments:
name = graphene.String(required=True)
id = graphene.ID()

category = graphene.Field(CategoryType)

@classmethod
def mutate(cls, root, info, name, id=None):
if id:
category = Category.objects.get(pk=id)
category.name = name
category.save()
else:
category = Category.objects.create(name=name)
return CategoryMutation(category=category)


class ArticleMutation(graphene.Mutation):
class Arguments:
title = graphene.String(required=True)
content = graphene.String(required=True)
created_at = graphene.Date()
author_id = graphene.ID()
category_id = graphene.ID()
id = graphene.ID()

article = graphene.Field(ArticleType)

@classmethod
def mutate(cls, root, info, title, content, created_at, author_id, category_id, id=None):
if id:
article = Article.objects.get(pk=id)
article.title = title
article.content = content
if created_at:
article.created_at = created_at
if author_id:
article.author_id = author_id
if category_id:
article.category_id = category_id
article.save()
else:
article = Article.objects.create(title=title, content=content, created_at=created_at, author_id=author_id,
category_id=category_id)
return ArticleMutation(article=article)


class Mutation(graphene.ObjectType):
create_or_update_category = CategoryMutation.Field()
create_or_update_article = ArticleMutation.Field()
48 changes: 48 additions & 0 deletions server/blog/resolvers/queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import graphene
from .queryTypes import AuthorType, CategoryType, ArticleType, CommentType, TagType
from ..models import Author, Category, Article, Comment, Tag

class Query(graphene.ObjectType):

all_authors = graphene.List(AuthorType)
all_categories = graphene.List(CategoryType)
all_articles = graphene.List(ArticleType, start=graphene.Int(), limit=graphene.Int())
all_comments = graphene.List(CommentType)
all_tags = graphene.List(TagType)

author_by_id = graphene.Field(AuthorType, id=graphene.Int())
category_by_id = graphene.Field(CategoryType, id=graphene.Int())
article_by_id = graphene.Field(ArticleType, id=graphene.Int())
# comment_by_id = graphene.Field(CommentType, id=graphene.Int())
# tag_by_id = graphene.Field(TagType, id=graphene.Int())

def resolve_all_authors(self, info):
return Author.objects.all()

def resolve_all_categories(self, info):
return Category.objects.all()

def resolve_all_articles(self, info, start: int=0, limit: int=20):
return Article.objects.all()[start:start+limit]

def resolve_all_comments(self, info):
return Comment.objects.all()

def resolve_all_tags(self, info):
return Tag.objects.all()

def resolve_author_by_id(self, info, id):
return Author.objects.get(pk=id)

def resolve_category_by_id(self, info, id):
return Category.objects.get(pk=id)

def resolve_article_by_id(self, info, id):
return Article.objects.get(pk=id)

# def resolve_comment_by_id(self, info, id):
# return Comment.objects.get(pk=id)

# def resolve_tag_by_id(self, info, id):
# return Tag.objects.get(pk=id)

22 changes: 22 additions & 0 deletions server/blog/resolvers/queryTypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from graphene_django.types import DjangoObjectType
from ..models import Author, Category, Article, Comment, Tag

class AuthorType(DjangoObjectType):
class Meta:
model = Author

class CategoryType(DjangoObjectType):
class Meta:
model = Category

class ArticleType(DjangoObjectType):
class Meta:
model = Article

class CommentType(DjangoObjectType):
class Meta:
model = Comment

class TagType(DjangoObjectType):
class Meta:
model = Tag
5 changes: 5 additions & 0 deletions server/blog/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import graphene
from .resolvers.queries import Query
from .resolvers.mutations import Mutation

schema = graphene.Schema(query=Query, mutation=Mutation)
3 changes: 3 additions & 0 deletions server/blog/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
7 changes: 7 additions & 0 deletions server/blog/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path
from graphene_django.views import GraphQLView

urlpatterns = [
# ...
path("graphql", GraphQLView.as_view(graphiql=True)),
]
3 changes: 3 additions & 0 deletions server/blog/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Binary file removed server/core/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file removed server/core/__pycache__/settings.cpython-310.pyc
Binary file not shown.
Binary file removed server/core/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file removed server/core/__pycache__/wsgi.cpython-310.pyc
Binary file not shown.
Loading

0 comments on commit 7188715

Please sign in to comment.