- FastAPI
- GraphQL
- Ariadne
- PostgreSQL
- SQLAlchemy
This is a sample project showing how to implement a CRUD (Create - Read - Update and Delete) system for players using FastAPI, GraphQL with Aridadne and PostgreSQL as database.
Make sure you have the following installed before running the project:
- Python 3.7 o superior
- PIP
- PostgreSQL
- Clone this repository on your local machine
git clone https://github.com/user/name-repository.git- Go to the project root directory
cd name-repository- Create and activate environment (Optional)
python3 -m venv venv
source venv/bin/activate- Install dependencies
pip install -r requirements.txt- Configure the PostgreSQL database
* Create a PostgreSQL database
* Update details of conection in the file 'app/config.py'- Run the migrations of the PostgreSQL database
alembic upgrade head- Init server FastAPI
uvicorn main:app --reload- Open your browser and visit http://localhost:8000/graphql to access the GraphQL interface.
query {
players {
id
name
number
team
}
}query {
player(playerId: 1) {
id
name
number
team
}
}mutation {
createPlayer(player_input: {
name: "John Doe"
number: 25
team: "Team A"
}) {
id
name
number
team
}
}mutation{
updatePlayer(playerID: 1, player_input:{
name: "John"
number: 26
team: "Team B"
}) {
id
name
number
team
}
}mutation{
deletePlayer(playerID: 2)
}query{
playerByName(playerName: "John")
}query{
playerByTeam(teamName: "Team B")
}- app
- config
- db.py: Contains the configuration for the database
- core
- config.py: Configuration of environment variables
- models
- player.py: Definition of data models.
- repository
- player_repository.py: Implementation of the business logic
- resolvers
- mutations.py: Implementation resolvers of type mutations
- queries.py: Implementation resolvers of type queries
- schema
- types.py: Implementation of the schema GraphQL
- config
- migrations: Contains the database migrations using Alembic.
- alembic.ini: Alembic configuration for migrations.
- main.py: Entry point of the application