A GraphQL API server for querying bank branch information built with Python, Strawberry GraphQL, and Flask.
- GraphQL API with Strawberry
- Bank and Branch data queries
- CSV data source
- Comprehensive test suite
- Relay-style connections with edges and nodes
- Python 3.8 or higher
- pip (Python package installer)
-
Clone the repository:
git clone https://github.com/BlackBuck/py-graphql cd py-graphql -
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
Create a CSV file named bank_branches.csv in the root directory with the following columns:
bank_id,bank_name,branch,ifsc,address,city,district,state
1,State Bank of India,Mumbai Main,SBIN0000001,SBI Building Mumbai,Mumbai,Mumbai,Maharashtra
2,HDFC Bank,Delhi Main,HDFC0000001,HDFC Building Delhi,Delhi,Delhi,Delhi
3,ICICI Bank,Bangalore Main,ICIC0000001,ICICI Building Bangalore,Bangalore,Bangalore,KarnatakaRequired CSV columns:
bank_id: Unique identifier for the bankbank_name: Name of the bankbranch: Branch nameifsc: IFSC codeaddress: Branch addresscity: City namedistrict: District namestate: State name
py-graphql/
├── app.py # Main GraphQL schema and data loading
├── server.py # Flask server wrapper
├── app_test.py # Test suite
├── requirements.txt # Python dependencies
├── bank_branches.csv # Your data file
└── README.md # This file
python server.pyThe server will start at http://localhost:5000
- Home page:
http://localhost:5000/ - GraphQL endpoint:
http://localhost:5000/graphql
For development, you can also run just the GraphQL schema:
python -c "from app import schema; print('Schema loaded successfully')"URL: POST http://localhost:5000/graphql
query {
branches {
edges {
node {
branch
ifsc
address
city
district
state
bank {
id
name
}
}
}
}
}query {
branches {
edges {
node {
branch
ifsc
bank {
name
}
}
}
}
}query {
branches {
edges {
node {
branch
city
district
state
bank {
name
}
}
}
}
}curl -X POST http://localhost:5000/graphql \
-H "Content-Type: application/json" \
-d '{
"query": "query { branches { edges { node { branch ifsc bank { name } } } } }"
}'import requests
url = "http://localhost:5000/graphql"
query = """
query {
branches {
edges {
node {
branch
ifsc
bank {
name
}
}
}
}
}
"""
response = requests.post(url, json={"query": query})
print(response.json())Run the test suite to ensure everything is working correctly:
# Run all tests
python app_test.py
# Run with verbose output
python app_test.py -v
# Run with pytest (if installed)
pytest app_test.py -vThe test suite includes:
- Schema validation
- Query structure verification
- Data type checking
- Bank name field validation
- Basic functionality tests
To add new queries, modify the Query class in app.py:
@strawberry.type
class Query:
@strawberry.field
def branches(self) -> BranchConnection:
return BranchConnection(edges=[BranchEdge(node=b) for b in BRANCHES])
# Add your new query here
@strawberry.field
def branch_by_ifsc(self, ifsc: str) -> Branch:
for branch in BRANCHES:
if branch.ifsc == ifsc:
return branch
return None- Update the CSV file with new columns
- Modify the
BranchorBankclasses inapp.py - Update the data loading logic
- Add corresponding tests
-
CSV file not found:
FileNotFoundError: [Errno 2] No such file or directory: 'bank_branches.csv'Solution: Make sure
bank_branches.csvexists in the root directory. -
Import errors:
ModuleNotFoundError: No module named 'strawberry'Solution: Install dependencies with
pip install -r requirements.txt -
Empty results: If queries return empty results, check:
- CSV file has data
- Column names match expected names
- No encoding issues in CSV file
Run the server in debug mode to see detailed error messages:
export FLASK_DEBUG=1
python server.py- strawberry-graphql: GraphQL library for Python
- Flask: Web framework
- pandas: Data manipulation library
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
This project is open source and available under the MIT License.
type Bank {
id: Int!
name: String!
}
type Branch {
branch: String!
ifsc: String!
address: String!
city: String!
district: String!
state: String!
bank: Bank!
}
type BranchEdge {
node: Branch!
}
type BranchConnection {
edges: [BranchEdge!]!
}
type Query {
branches: BranchConnection!
}