This is a sample project that demonstrates a CRUD operation GraphQL API using FastAPI, MongoDB, and Strawberry.
- Clone the repository:
git clone https://github.com/Shikha-code36/GraphQL_FastAPI.git
cd GraphQL_FastAPI
- Create and activate a virtual environment:
python3 -m venv venv
source venv/bin/activate # On Linux/MacOS
venv\Scripts\activate.bat # On Windows
- Install dependencies using pip:
pip install -r requirements.txt
-
Make sure your MongoDB server is running on
localhost
at the default port27017
. You can use your MongoDB server instead of localhost. -
Start the FastAPI server:
uvicorn main:app --reload
- The GraphQL API will be available at
http://localhost:8000/graphql
. You can access this URL in your browser or use tools like Insomnia or GraphQL Playground to interact with the API.
To run the test cases, use the following command:
python -m pytest
To create a new user, send a GraphQL mutation request:
mutation {
createUser(name: "Shikha Pandey") {
id
name
}
}
To update a user, send a GraphQL mutation request:
mutation {
updateUser(id: 1, name: "Kritika Pandey") {
id
name
}
}
To delete a user, send a GraphQL mutation request:
mutation {
deleteUser(id: 1) {
id
name
}
}
To get a list of all users, send a GraphQL query request:
query {
users {
id
name
}
}
To get a specific user by ID, send a GraphQL query request:
query {
user(id: 1) {
id
name
}
}
This project is licensed under the MIT License.
This project is a basic implementation of a graphql and python FastApi for educational purposes. It may not be suitable for production use. Use it at your own risk.
Contributions to this project are welcome. If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.