Skip to content

rhoboro/async-fastapi-prisma

Repository files navigation

Async Web API with FastAPI + Prisma + GraphQL

This is a sample project of Async Web API with FastAPI + Prisma.

If you want to use sqlalchemy instead of prisma, see rhoboro/async-fastapi-sqlalchemy.

Setup

Install

$ python3 -m venv venv --upgrade-deps
$ . venv/bin/activate
(venv) $ pip install -r requirements.lock

Setup a database and create tables

(venv) $ docker run -d --name db \
  -e POSTGRES_PASSWORD=password \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v $(pwd)/pgdata:/var/lib/postgresql/data \
  -p 5432:5432 \
  postgres:16.0-alpine

(venv) $ DATABASE_URL=postgresql://postgres:password@localhost:5432 prisma db push
Prisma schema loaded from schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "localhost:5432"

🚀  Your database is now in sync with your Prisma schema. Done in 69ms

✔ Generated Prisma Client Python (v0.11.0) to ./app/prisma in 98ms

Run

(venv) $ APP_CONFIG_FILE=local uvicorn app.main:app --reload-dir app

You can now access localhost:8000/docs to see the API documentation.

GraphQL

This application has additional endpoint /graphql. If installed graphene, you can also access localhost:8000/graphql.

(venv) $ pip install graphene
(venv) $ APP_CONFIG_FILE=local uvicorn app.main:app --reload-dir app
(venv) $ pip install 'gql[aiohttp]'
(venv) $ echo 'query { notebook(id:1) { id title notes { title notebookId }}}' | gql-cli  http://localhost:8000/graphql | jq
{
  "notebook": {
    "id": "1",
    "title": "test notebook",
    "notes": [
      {
        "title": "test note",
        "notebookId": 1
      }
    ]
  }
}

Test

(venv) $ pip install -r requirements_test.txt
(venv) $ black app
(venv) $ isort app
(venv) $ pytest app
(venv) $ mypy app
(venv) $ pyright

Prisma Commands

See https://www.prisma.io/docs/reference/api-reference/command-reference

TODOs

  • Add Unit tests
  • Setup a temporary db for unit test

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published