adding graphql integration

This commit is contained in:
2026-04-29 11:31:31 -04:00
parent 3a4ffce7a3
commit a41bd7716f
6 changed files with 170 additions and 3 deletions

View File

@@ -14,6 +14,7 @@ from .alarms import router as alarms_router
from .cellsites import router as cellsites_router
from .simulator import router as simulator_router
from .robots import router as robots_router
from .gqlapi import graphql_app as test_graphql
origins: list[str] = settings.cors_origins.split(",")
@@ -58,5 +59,7 @@ def create_app() -> FastAPI:
app.include_router(alarms_router, prefix="/api/v1")
app.include_router(simulator_router, prefix="/api/v1")
app.include_router(robots_router, prefix="/api/v1")
# Graphql test
app.include_router(test_graphql, prefix="/graphql")
return app

View File

@@ -10,7 +10,7 @@ router = APIRouter(prefix="/cellsites", tags=["cellsites"])
def get_cellsites() -> list[dict[Any, Any]]: # type: ignore
conn = db.connect_to_db()
try:
with db.connect_to_db() as conn:
with conn:
return db.get_cellsites(conn)
except Exception as e:
logger.error(f"Error fetching cellsites: {e}", exc_info=True)
@@ -23,7 +23,7 @@ def get_cellsites() -> list[dict[Any, Any]]: # type: ignore
def get_cellsite(id: int) -> list[dict[str, Any]]: # type: ignore
conn = db.connect_to_db()
try:
with db.connect_to_db() as conn:
with conn:
return db.get_cellsite(conn, id)
except Exception as e:
logger.error(f"Error fetching cellsite {id}: {e}", exc_info=True)

View File

@@ -0,0 +1,87 @@
# https://fastapi.tiangolo.com/how-to/graphql/#graphql-with-strawberry
import typing
import strawberry
from strawberry.fastapi import GraphQLRouter
import database as db
from logger import get_logger
logger = get_logger(__name__)
def get_sites():
conn = db.connect_to_db()
try:
with db.connect_to_db() as conn:
sites = db.get_cellsites(conn)
ql_list = []
for site in sites:
print(site)
ql_list.append(
Cellsite(
id=site['id'],
lat=site['lat'],
lon=site['lon']
)
)
return ql_list
except Exception as e:
logger.error(f"Error fetching cellsite {id}: {e}", exc_info=True)
return []
finally:
conn.close()
def get_incidents():
conn = db.connect_to_db()
try:
with conn:
incidents = db.get_incidents(conn)
ql_list = []
for incident in incidents:
print(incident)
ql_list.append(
Incident(
id=incident['id'],
text=incident['text'],
severity=incident['severity'],
status=incident['status'],
created_by=incident['created_by'],
site_id=incident['site_id']
)
)
return ql_list
except Exception as e:
logger.error(f"Error fetching cellsite {id}: {e}", exc_info=True)
return []
finally:
conn.close()
@strawberry.type
class Cellsite:
id: int
lat: float
lon: float
# TODO: Add incident and join with cellsite
# TODO: Query a site ID and get all related incidents
# TODO: Query an incident and get all related site id
@strawberry.type
class Incident:
id: int
text: str
severity: int
status: str
created_by: str
# site_id: typing.List[Cellsite]
site_id: int
@strawberry.type
class Query:
cellsites: typing.List[Cellsite] = strawberry.field(resolver=get_sites)
incidents: typing.List[Incident] = strawberry.field(resolver=get_incidents)
schema = strawberry.Schema(query=Query)
graphql_app = GraphQLRouter(schema)