1 of 30

Deploy Graphql API

w6

adsoft

2 of 30

clone template repo

$ git clone https://github.com/adsoftsito/hackernews_402_2024.git mycv

create local database: mycv

Test locally …

3 of 30

create free database

4 of 30

in https://render.com

create new Web Service, from public Git repository

5 of 30

in https://render.com

Select environment, and create 3 variables

6 of 30

in https://render.com

Select environment, and create 3 variables

7 of 30

in https://render.com

Select settings, type project name, region and free instance type

8 of 30

in https://render.com

Select settings, in build Command

9 of 30

in https://render.com

Select settings, in Start Command type:

python -m gunicorn hacker_news.asgi:application -k uvicorn.workers.UvicornWorker

10 of 30

read ..

11 of 30

12 of 30

  • activate virtual environment

in localhost, create cv apps

13 of 30

add apps in settings.

$ vi hacker_news/settings.py

14 of 30

$ 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)

15 of 30

$ vi hacker_news/settings.py

16 of 30

makemigrations

migrate

17 of 30

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)

18 of 30

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)

19 of 30

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()

20 of 30

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)

21 of 30

create an user

22 of 30

get token

23 of 30

query degrees

24 of 30

mutation createEducation

25 of 30

query degrees (after mutation)

26 of 30

mutation createEducation (UPDATE with idEducation > 0)

27 of 30

query degreeById ( idEducation > 0)

28 of 30

query degreeById ( idEducation > 0)

29 of 30

mutation deleteEducation ( idEducation > 0)

30 of 30

mutation deleteEducation ( idEducation > 0)