Giter Site home page Giter Site logo

django-graphql's Introduction

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

django-graphql's People

Watchers

Raffaele Tosti avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.