Skip to content

Markos96/GraphQL-FastAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Player CRUD project with:

  • 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.


Pre-requisites

Make sure you have the following installed before running the project:

  • Python 3.7 o superior
  • PIP
  • PostgreSQL

Environment configuration

  1. Clone this repository on your local machine
git clone https://github.com/user/name-repository.git
  1. Go to the project root directory
cd name-repository
  1. Create and activate environment (Optional)
python3 -m venv venv
source venv/bin/activate
  1. Install dependencies
pip install -r requirements.txt
  1. Configure the PostgreSQL database
* Create a PostgreSQL database
* Update details of conection in the file 'app/config.py'
  1. Run the migrations of the PostgreSQL database
alembic upgrade head
  1. Init server FastAPI
uvicorn main:app --reload
  1. Open your browser and visit http://localhost:8000/graphql to access the GraphQL interface.

GraphQL queries

1) View all players

query {
  players {
    id
    name
    number
    team
  }
}

2) Search for a player by ID

query {
  player(playerId: 1) {
    id
    name
    number
    team
  }
}

3) Create a new player

mutation {
  createPlayer(player_input: {
    name: "John Doe"
    number: 25
    team: "Team A"
  }) {
    id
    name
    number
    team
  }
}

4) Update player

mutation{
  updatePlayer(playerID: 1, player_input:{
  name: "John"
  number: 26
  team: "Team B"
}) {
  id
  name
  number
  team
  }
}

5) Delete player

mutation{
  deletePlayer(playerID: 2)
}

6) Search for player by name

query{
  playerByName(playerName: "John")
}

7) Search for player by team name

query{
  playerByTeam(teamName: "Team B")
}

Project structure

  • 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
  • migrations: Contains the database migrations using Alembic.
  • alembic.ini: Alembic configuration for migrations.
  • main.py: Entry point of the application

About

Crud Player & Team

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors