Senior
How do we return the entire order and its partials (API design)?
sobes.tech AI
Answer from AI
To return a full order and its partial data via API, you can use a REST approach with different endpoints or query parameters.
For example:
- GET /orders/{id} — returns the full order with details.
- GET /orders/{id}?fields=items,total — returns only the specified fields (partial).
Or use GraphQL, where the client specifies which fields it needs.
In Go, this can be implemented as follows:
func GetOrder(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
fields := r.URL.Query().Get("fields")
order := fetchOrderFromDB(id)
if fields != "" {
// Parse fields and form response with required fields
partialResponse := filterOrderFields(order, fields)
json.NewEncoder(w).Encode(partialResponse)
return
}
json.NewEncoder(w).Encode(order)
}
This approach allows flexible data delivery based on client requests.