Skip to content

Commit d7fe37f

Browse files
authored
Fix gateway example (#59)
1 parent 6efee69 commit d7fe37f

File tree

1 file changed

+9
-3
lines changed
  • examples/shop_api_gateway

1 file changed

+9
-3
lines changed

examples/shop_api_gateway/app.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ async def _client(ctx: EnrichContext) -> httpx.AsyncClient:
7676

7777
@app.resource
7878
async def list_users(ctx: EnrichContext) -> list[User]:
79+
"""Fetch all users from the backend service."""
7980
client = await _client(ctx)
8081
resp = await client.get("/users")
8182
resp.raise_for_status()
@@ -84,6 +85,7 @@ async def list_users(ctx: EnrichContext) -> list[User]:
8485

8586
@app.resource
8687
async def get_user(user_id: int, ctx: EnrichContext) -> User:
88+
"""Return a single user by ID."""
8789
client = await _client(ctx)
8890
resp = await client.get(f"/users/{user_id}")
8991
resp.raise_for_status()
@@ -92,6 +94,7 @@ async def get_user(user_id: int, ctx: EnrichContext) -> User:
9294

9395
@app.resource
9496
async def list_products(ctx: EnrichContext) -> list[Product]:
97+
"""Retrieve all products available for sale."""
9598
client = await _client(ctx)
9699
resp = await client.get("/products")
97100
resp.raise_for_status()
@@ -100,6 +103,7 @@ async def list_products(ctx: EnrichContext) -> list[Product]:
100103

101104
@app.resource
102105
async def get_product(product_id: int, ctx: EnrichContext) -> Product:
106+
"""Get a single product by ID."""
103107
client = await _client(ctx)
104108
resp = await client.get(f"/products/{product_id}")
105109
resp.raise_for_status()
@@ -111,6 +115,7 @@ async def list_orders(
111115
user_id: int | None = None,
112116
ctx: EnrichContext | None = None,
113117
) -> list[Order]:
118+
"""List orders optionally filtered by user."""
114119
if ctx is None:
115120
raise RuntimeError("Context required")
116121
client = await _client(ctx)
@@ -122,19 +127,20 @@ async def list_orders(
122127

123128
@app.resource
124129
async def get_order(order_id: int, ctx: EnrichContext) -> Order:
130+
"""Retrieve a specific order."""
125131
client = await _client(ctx)
126132
resp = await client.get(f"/orders/{order_id}")
127133
resp.raise_for_status()
128134
return Order(**resp.json())
129135

130136

131137
@User.orders.resolver
132-
async def get_orders_for_user(user_id: int, ctx: EnrichContext) -> list[Order]:
138+
async def get_orders_for_user(user_id: int, ctx: EnrichContext) -> list["Order"]:
133139
return await list_orders(user_id=user_id, ctx=ctx)
134140

135141

136142
@Order.user.resolver
137-
async def get_order_user(user_id: int, ctx: EnrichContext) -> User:
143+
async def get_order_user(user_id: int, ctx: EnrichContext) -> "User":
138144
return await get_user(user_id=user_id, ctx=ctx)
139145

140146

@@ -154,4 +160,4 @@ async def get_order_products(order_id: int, ctx: EnrichContext) -> list[Product]
154160

155161
if __name__ == "__main__":
156162
print("Starting Shop API Gateway...")
157-
app.run(port=8000)
163+
app.run()

0 commit comments

Comments
 (0)