Deploy Graphql API
w6
adsoft
clone template repo
$ git clone https://github.com/adsoftsito/hackernews_402_2024.git mycv
create local database: mycv
Test locally …
create free database
in https://render.com
create new Web Service, from public Git repository
in https://render.com
Select environment, and create 3 variables
in https://render.com
Select environment, and create 3 variables
in https://render.com
Select settings, type project name, region and free instance type
in https://render.com
Select settings, in build Command
in https://render.com
Select settings, in Start Command type:
python -m gunicorn hacker_news.asgi:application -k uvicorn.workers.UvicornWorker
read ..
test your graphql api: https://mysite-hdva.onrender.com/graphql/
in localhost, create cv apps
add apps in settings.
$ vi hacker_news/settings.py
$ vi education/models.py
# Create your models here.
from django.db import models
from django.conf import settings
from django.utils.timezone import now
# Create your models here.
class Education(models.Model):
degree = models.TextField(default='')
university = models.TextField(default='')
start_date = models.DateTimeField(default=now, blank=True)
end_date = models.DateTimeField(default=now, blank=True)
posted_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)
$ vi hacker_news/settings.py
makemigrations
migrate
create schema.py (Query)
$ vi education/schema,py
import graphene
from graphene_django import DjangoObjectType
from .models import Education
from users.schema import UserType
from django.db.models import Q
class EducationType(DjangoObjectType):
class Meta:
model = Education
class Query(graphene.ObjectType):
degrees = graphene.List(EducationType, search=graphene.String())
def resolve_degrees(self, info, search=None, **kwargs):
user = info.context.user
if user.is_anonymous:
raise Exception('Not logged in!')
print (user)
if (search=="*"):
filter = (
Q(posted_by=user)
)
return Education.objects.filter(filter)[:10]
else:
filter = (
Q(posted_by=user) & Q(degree__icontains=search)
)
return Education.objects.filter(filter)
create schema.py (Mutation)
$ vi education/schema,py
class CreateEducation(graphene.Mutation):
idEducation = graphene.Int()
degree = graphene.String()
university = graphene.String()
start_date = graphene.Date()
end_date = graphene.Date()
posted_by = graphene.Field(UserType)
#2
class Arguments:
idEducation= graphene.Int()
degree = graphene.String()
university = graphene.String()
start_date = graphene.Date()
end_date = graphene.Date()
#3
def mutate(self, info, idEducation, degree, university, start_date, end_date):
user = info.context.user or None
if user.is_anonymous:
raise Exception(‘Not logged in !’);
print(user)
currentEducation = Education.objects.filter(id=idEducation).first()
print (currentEducation)
create schema.py (Mutation)
$ vi education/schema,py
education = Education(
degree = degree,
university = university,
start_date = start_date,
end_date = end_date,
posted_by = user
)
if currentEducation:
education.id = currentEducation.id
education.save()
return CreateEducation(
idEducation = education.id,
degree = education.degree,
university = education.university,
start_date = education.start_date,
end_date = education.end_date,
posted_by = education.posted_by
)
#4
class Mutation(graphene.ObjectType):
create_education = CreateEducation.Field()
update global schema.py
$ vi hacker_news/schema,py
import graphene
import graphql_jwt
import links.schema
import users.schema
import education.schema
class Query(education.schema.Query, users.schema.Query, links.schema.Query, graphene.ObjectType):
pass
class Mutation(education.schema.Mutation, users.schema.Mutation, links.schema.Mutation, graphene.ObjectType):
token_auth = graphql_jwt.ObtainJSONWebToken.Field()
verify_token = graphql_jwt.Verify.Field()
refresh_token = graphql_jwt.Refresh.Field()
schema = graphene.Schema(query=Query, mutation=Mutation)
create an user
get token
query degrees
mutation createEducation
query degrees (after mutation)
mutation createEducation (UPDATE with idEducation > 0)
query degreeById ( idEducation > 0)
query degreeById ( idEducation > 0)
mutation deleteEducation ( idEducation > 0)
mutation deleteEducation ( idEducation > 0)