Skip to content

Instantly share code, notes, and snippets.

@canburaks
canburaks / djr-2-create-api.sh
Last active November 22, 2019 02:56
Create API App
# djr/
# create app with the name gql
# (name is not necessarily to be gql)
python manage.py startapp gql
@canburaks
canburaks / djr-2-configure-url.py
Created November 22, 2019 02:57
Step-2: Configure Url Endpoints And Necessary Settings
from django.contrib import admin
from django.urls import path
from graphene_django.views import GraphQLView
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
# apiclient on client-side will request this adress later
@canburaks
canburaks / djr-2-schema-loc.py
Created November 22, 2019 03:02
Define the schema location
# djr/djr/settings.py
GRAPHENE = {
'SCHEMA': 'gql.schema.schema'
}
@canburaks
canburaks / schema.py
Created November 22, 2019 03:03
The final code of schema.py file is here:
import graphene
from items.models import Movie
from graphene_django.types import DjangoObjectType
# api-movie-model
class MovieType(DjangoObjectType):
id = graphene.Int()
name = graphene.String()
year = graphene.Int()
summary = graphene.String()
query {
#the query name
movieList{
# the fields which we will request
id,
name,
posterUrl, #camelCase
}
}
query {
#the query and argument
movie(slug:"the-matrix-1999"){
# the fields which we will request
id,
name,
posterUrl, #camelCase
}
}
#<---Install python 3.8--->
cd /opt
sudo wget <https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz>
#extract the source
sudo tar xzf Python-3.8.0.tgz
cd Python-3.8.0
sudo ./configure --enable-loadable-sqlite-extensions
# <--- Create Django Project --->
# We will create the project in the Blog folder
# with a name djr
# install our dependencies
pip install ipython django django_extensions django-cors-headers "graphene-django>=2.0"
#start a django project
django-admin startproject djr
# change directory
cd djr
# create templates directory
mkdir templates
# create static folder
# create our app and activate it on the settings.py
python manage.py startapp items