t04st3r / django-graphql

A simple project on how to expose graphQL API with Django

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CircleCI python Ruff

Django graphQL

Just a sample project on how to expose graphQL API with Django

Install

To run django locally you need python version to 3.11.3 and pipenv, you can use pyenv to set your desired python version.

Run containers

Django needs postgres and redis container to be up and running, to do so clone the project, enter in the project folder and hit:

docker compose up postgres redis

Install django deps and run django server

inside the project folder make sure you use python 3.11.3

python --version

you should see

Python 3.11.3

install requirements (listed inside Pipfile) by

pipenv install

To run tests you will need also dev dependencies

pipenv install --dev

Create a .env file using the .env.example file and changing the url to be localhost in the following env variables

POSTGRES_HOST
REDIS_URL

Your .env file should be something like this

#django
ENV=dev
DJANGO_DEBUG=True
SECRET_KEY='supersecretkey'

#db
POSTGRES_USER=postgres
POSTGRES_DB=app_db
POSTGRES_HOST=localhost
POSTGRES_PASSWORD=password
POSTGRES_PORT=5432

#redis
REDIS_URL=redis://localhost:6379/0

Once done enter inside your pipenv virtual environment by

pipenv shell

Run migrations with

python manage.py migrate

Or (if you have make installed) by

make migrate

Everything is done! You can now start the server with

python manage.py runserver

Or

make run-dev

How does it works

This apps gather data from Public Holiday API via a django command, you can populate PublicHoliday models with

python manage.py populate_models

Or

make populate-models

For each command run a random country is selected and all the public holidays for that country would be fetched and stored in the db

GraphQL

To test some graphQL queries against the API just connect with the browser to

http://localhost:8000/graphql

Possible queries can be:

  • fetching all the Public Holiday models via allHolidays
query {
    allHolidays {
        id
        name
        localName
        country
        date
    }
}
  • fetching all the Public Holiday filtered by country via holidayByCountry (in the example models are filtered by Italy country)
query {
    holidayByCountry(country: "IT"){
        id
        localName
        name
        country
    }
}
  • Mutate the localName of a PublicHoliday via updatePublicHoliday (in the example the localName field of the model with ID 1 is changed to "Pizza")
mutation MyMutation {
    updatePublicHoliday(id: "1", localName: "Pizza") {
        publicHoliday {
            id
            localName
        }
    }
}

Testing

Testing requirements can be installed by

pipenv install --dev

To run the ruff linter on your codebase hit

make lint

pre-commit (that in turn will run ruff check) can be installed as well with

pre-commit install

You can run django tests by simply run

pytest

To simulate the testing pipeline in CircleCI just run

docker compose run django ci

About

A simple project on how to expose graphQL API with Django

License:Apache License 2.0


Languages

Language:Python 96.1%Language:Makefile 2.6%Language:Dockerfile 1.3%