Exercise 6: BFF Pattern - The Right Way
Reality Check: Complex response transformations (like field filtering) in API Gateways are difficult and not recommended for production.
The Production Approach:
Create a mobile-specific route that signals client type to backend:
curl http://127.0.0.1:9180/apisix/admin/routes/10 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
-X PUT -d '{
"uris": ["/mobile/api/patients/*"],
"plugins": {
"proxy-rewrite": {
"regex_uri": ["^/mobile/api/(.*)", "/api/$1"],
"headers": {
"set": {
"X-Client-Type": "mobile",
"X-Response-Format": "minimal"
}
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {"practicemanager:8080": 1}
}
}'
Backend Implementation:
The practicemanager service would read the X-Response-Format header and return only requested fields.
Why this approach?
- Gateway handles routing and headers
- Backend handles business logic (field selection)
- Separation of concerns
- Easier to test and maintain