Index

Graphene

  1. Queries
  2. migrations
  3. UserModel
  4. User Schema
  5. cors
  6. static
  7. headers
  8. payumoney
  9. imageupload
  10. Total count

Graphene

For psycopg2
sudo apt-get install postgresql postgresql-contrib libpq-dev
sudo apt-get install build-essential libssl-dev libffi-dev python3-dev

pip3 install psycopg2

sudo apt install virtualenv
mkdir sakhura-graphql
cd sakhura-graphql
virtualenv -p python3 env
source env/bin/activate

pip3 install django graphene_django graphene-permissions django-filter django-graphql-jwt django-cors-headers Pillow gunicorn pylint  psycopg2

django-admin startproject sakhura_backend


code .

pip3 freeze > requirements.txt



sakhura_backend/sakhura_backend/settings.py

INSTALLED_APPS = (
    # After the default packages
    'corsheaders',
    'graphene_django',
)



sakhura_backend/sakhura_backend/settings.py
change sakhura_backend to your project name

import os, datetime
from datetime import timedelta


GRAPHENE = {
         # change sakhura_backend to your project name
    'SCHEMA''sakhura_backend.schema.schema',
     'MIDDLEWARE': [
        'graphql_jwt.middleware.JSONWebTokenMiddleware',
    ],
}


AUTHENTICATION_BACKENDS = [
    'graphql_jwt.backends.JSONWebTokenBackend',
    'django.contrib.auth.backends.ModelBackend',
]


GRAPHQL_JWT = {
    'JWT_VERIFY_EXPIRATION'True,
    'JWT_EXPIRATION_DELTA': timedelta(days=1),
}


CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = [
    'http://localhost:3000'
]


static

STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT=os.path.join(BASE_DIR, "upload")
STATIC_ROOT=os.path.join(BASE_DIR,"allstatic")
HIDDEN_ROOT = os.path.join(BASE_DIR, 'files')


or create a folder as .vscode
Inside it create settings.
json

Replace sakhura_backed with your app name

{
    "python.pythonPath""env/bin/python3",
    "terminal.integrated.cwd""sakhura_backend",
}


sakhura-graphql/sakhura_backend$   python3 manage.py startapp jewellery


sakhura_backend/sakhura_backend/settings.py

INSTALLED_APPS = (
    # After the default packages
    'jewellery',
)


python3 manage.py makemigrations

python3 manage.py migrate




schema.py
sakhura_backend/sakhura_backend/schema.py
import graphene
import profiles_api.schema
import journal_api.schema
import mailing.schema
import tracking.schema
import review.schema

class Query(profiles_api.schema.Query, journal_api.schema.Query, mailing.schema.Query, tracking.schema.Query, review.schema.Query, graphene.ObjectType):
    pass

class Mutation(profiles_api.schema.Mutation, mailing.schema.Mutation, tracking.schema.Mutation, review.schema.Mutation):
    pass

schema = graphene.Schema(query=Query, mutation=Mutation)


sakhura_backend/Journal_api/schema.py
from graphene import relay, ObjectType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
import graphene
from graphql_relay.node.node import from_global_id
from journal_api.models import SubjectGroup, SubjectList


class SubjectGroupNode(DjangoObjectType):
    class Meta:
        model = SubjectGroup
        filter_fields = ["name""domainweb__name"]
        interfaces = (relay.Node, )

class SubjectListNode(DjangoObjectType):
    class Meta:
        model = SubjectList
        filter_fields = {
            "name" : ['exact''icontains'],
            "description": ['icontains'],
            "group__name": ['exact''icontains'],
            "group__domainweb__name": ['exact'],
        }
        exclude_fields = ('subject_profiles','articlesubmissiondetails_set''created_at''updated_at')
        interfaces = (relay.Node, )

class Query(ObjectType):
    subject_group = relay.Node.Field(SubjectGroupNode)
    all_subject_group = DjangoFilterConnectionField(SubjectGroupNode)

    subject_list = relay.Node.Field(SubjectListNode)
    all_subject_list = DjangoFilterConnectionField(SubjectListNode)
    

class Mutation(ObjectType):
    pass


or:
from graphene import relay, ObjectType, String
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
import graphene
from graphql_relay.node.node import from_global_id

class Person(graphene.ObjectType):
    name = graphene.String()
    age = graphene.Int()

class Query(ObjectType):
    hello = String(name=String(default_value="stranger"))
    def resolve_hello(root, info, name):
        return f'Hello {name}!'

class CreatePerson(graphene.Mutation):
    class Arguments:
        name = graphene.String()

    ok = graphene.Boolean()
    person = graphene.Field(lambda: Person)

    def mutate(root, info, name):
        person = Person(name=name)
        ok = True
        return CreatePerson(person=person, ok=ok)

class Mutation(ObjectType):
    create_person = CreatePerson.Field()