strawberry-sqlalchemy-filter

MIT License

Stars
5

FastAPI + SQLAlchemy

This examples shows you how to setup Strawberry with FastAPI and SQLAlchemy. It setups a GraphQL API to fetch the top rated movies from IMDB (stored in a sqlite DB).

How to use

  1. Install dependencies

Use poetry to install dependencies:

poetry install
  1. Install pre-commit hooks
poetry run pre-commit install
  1. Run migrations

Run alembic to create the database and populate it with movie data:

poetry run alembic upgrade head
  1. Run the server

Run uvicorn to run the server:

poetry run uvicorn main:app --reload

The GraphQL API should now be available at http://localhost:8000/graphql

Common Commands

  • start the application
    • poetry run uvicorn main:app --reload
  • output the schema
  • poetry run strawberry export-schema main:schema

Example query

query AllTopRatedMovies {
  topRatedMovies {
    id
    imageUrl
    imdbId
    imdbRating
    imdbRatingCount
    title
    year
    director {
      id
      name
    }
  }
}

Types of Filters

  1. Automatically Generated
  1. Simple
  2. Join

Automatically Generated

key description
eq equal
ne not equal
like like
iLike insensitive like
isNull is null
in in
notIn not in
lt less than
lte less than or equal
gt greater than
gte greater than or equal

Related Work

Examples

{
  allUsers(
    filters: {
      isActive: true
      or: [{ isAdmin: true }, { usernameIn: ["moderator", "cool guy"] }]
    }
  ) {
    edges {
      node {
        id
        username
      }
    }
  }
}
{
  allUsers(filters: { isActive: true }) {
    edges {
      node {
        id
      }
    }
  }
  allGroups {
    edges {
      node {
        users(filters: { isActive: true }) {
          edges {
            node {
              id
            }
          }
        }
      }
    }
  }
}
Related Projects