From 7fff6cc1c4c600b59e94d3ce960ce1f77b754448 Mon Sep 17 00:00:00 2001 From: Md Shayon Date: Sun, 10 Mar 2024 12:42:49 +0600 Subject: [PATCH] Creating user successfully, now need to protect the routes --- server/READNE.md | 14 +++++++++----- server/account/admin.py | 3 ++- server/account/models.py | 2 +- server/account/resolvers/mutations.py | 2 +- server/account/resolvers/queries.py | 14 ++++++++++++++ server/account/resolvers/queryTypes.py | 6 +++++- server/core/schema.py | 3 ++- 7 files changed, 34 insertions(+), 10 deletions(-) diff --git a/server/READNE.md b/server/READNE.md index f231276..f0a484a 100644 --- a/server/READNE.md +++ b/server/READNE.md @@ -9,6 +9,8 @@ mutation RegisterUser($email: String!, $password: String!, id password lastLogin + firstName + lastName birth } } @@ -17,10 +19,12 @@ mutation RegisterUser($email: String!, $password: String!, ``` { - "email": "u1@example.com", + "email": "u2@example.com", "password": "Test1234", - "firstName": "fn1", - "lastName": "ln1", - "birth": "2024-03-06T11:26:12.306Z" + "firstName": "fn2", + "lastName": "ln2", + "birth": "2024-03-08T08:14:45.562Z" } -``` \ No newline at end of file +``` + - The `--run-syncdb` option helps to synchronize the database tables with the current state of your models without relying on the migration history. + - `./manage.py migrate --run-syncdb` \ No newline at end of file diff --git a/server/account/admin.py b/server/account/admin.py index 86d200e..5ec6f7e 100644 --- a/server/account/admin.py +++ b/server/account/admin.py @@ -1,5 +1,6 @@ from django.contrib import admin from account.models import User +from django.contrib.auth.admin import UserAdmin # Register your models here. -admin.site.register(User) \ No newline at end of file +# admin.site.register(User, UserAdmin) diff --git a/server/account/models.py b/server/account/models.py index bdcded0..b6ea506 100644 --- a/server/account/models.py +++ b/server/account/models.py @@ -33,7 +33,7 @@ class User(AbstractUser): max_length=255, unique=True, ) - birth = models.DateField() + birth = models.DateField(null=True, blank=True, default=None) is_active = models.BooleanField(default=False) is_admin = models.BooleanField(default=False) diff --git a/server/account/resolvers/mutations.py b/server/account/resolvers/mutations.py index c76dff9..e481d92 100644 --- a/server/account/resolvers/mutations.py +++ b/server/account/resolvers/mutations.py @@ -12,7 +12,7 @@ class Arguments: password = graphene.String(required=True) first_name = graphene.String(required=True) last_name = graphene.String(required=True) - birth = graphene.String() # Accept birthdate as a string in ISO 8601 format + birth = graphene.String(required=False) # Accept birthdate as a string in ISO 8601 format def mutate(self, info, email, password, first_name, last_name, birth=None): birthdate = None diff --git a/server/account/resolvers/queries.py b/server/account/resolvers/queries.py index e69de29..9eff631 100644 --- a/server/account/resolvers/queries.py +++ b/server/account/resolvers/queries.py @@ -0,0 +1,14 @@ +import graphene +from graphene_django import DjangoObjectType +from .queryTypes import GetUserType +from ..models import User + +class Query(graphene.ObjectType): + users = graphene.List(GetUserType) + question_by_id = graphene.Field(GetUserType, id=graphene.String()) + + def resolve_users(root, info, **kwargs): + return User.objects.all() + + def resolve_user_by_id(root, info, id): + return User.objects.get(pk=id) \ No newline at end of file diff --git a/server/account/resolvers/queryTypes.py b/server/account/resolvers/queryTypes.py index 1f84384..b3c089a 100644 --- a/server/account/resolvers/queryTypes.py +++ b/server/account/resolvers/queryTypes.py @@ -4,4 +4,8 @@ class UserType(DjangoObjectType): class Meta: - model = User \ No newline at end of file + model = User +class GetUserType(DjangoObjectType): + class Meta: + model = User + exclude = ("password",) \ No newline at end of file diff --git a/server/core/schema.py b/server/core/schema.py index e0aab12..fd008cf 100644 --- a/server/core/schema.py +++ b/server/core/schema.py @@ -2,11 +2,12 @@ from blog.resolvers.queries import Query as BlogQuery from blog.resolvers.mutations import Mutation as BlogMutation from account.resolvers.mutations import Mutation as AccountMutation +from account.resolvers.queries import Query as AccountQuery class Mutation(BlogMutation, AccountMutation): pass -class Query(BlogQuery): +class Query(BlogQuery, AccountQuery): pass schema = graphene.Schema(query=Query, mutation=Mutation) \ No newline at end of file