|
| 1 | +// locations service: owns Location @key(fields: "coordinate { lat lng }") |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const sdl = `extend schema |
| 13 | + @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key", "@shareable"]) |
| 14 | +
|
| 15 | +type Location @key(fields: "coordinate { lat lng }") { |
| 16 | + coordinate: Coordinate! |
| 17 | + name: String! |
| 18 | +} |
| 19 | +
|
| 20 | +type Coordinate @shareable { |
| 21 | + lat: Float! |
| 22 | + lng: Float! |
| 23 | +} |
| 24 | +
|
| 25 | +type Query { |
| 26 | + location(id: ID!): Location |
| 27 | +}` |
| 28 | + |
| 29 | +type coordinate struct { |
| 30 | + Lat float64 `json:"lat"` |
| 31 | + Lng float64 `json:"lng"` |
| 32 | +} |
| 33 | + |
| 34 | +type location struct { |
| 35 | + Name string `json:"name"` |
| 36 | + Coordinate coordinate `json:"coordinate"` |
| 37 | +} |
| 38 | + |
| 39 | +// locationDB stores all known locations indexed by id. |
| 40 | +var locationDB = map[string]location{ |
| 41 | + "loc1": {Name: "Tokyo Station", Coordinate: coordinate{Lat: 35.6762, Lng: 139.6503}}, |
| 42 | + "loc2": {Name: "Shibuya", Coordinate: coordinate{Lat: 35.6580, Lng: 139.7016}}, |
| 43 | + "loc3": {Name: "Shinjuku", Coordinate: coordinate{Lat: 35.6896, Lng: 139.6917}}, |
| 44 | +} |
| 45 | + |
| 46 | +// findByCoordinate returns the location that has the given (lat, lng) coordinate. |
| 47 | +func findByCoordinate(lat, lng float64) *location { |
| 48 | + for _, loc := range locationDB { |
| 49 | + if loc.Coordinate.Lat == lat && loc.Coordinate.Lng == lng { |
| 50 | + l := loc |
| 51 | + return &l |
| 52 | + } |
| 53 | + } |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +type gqlRequest struct { |
| 58 | + Query string `json:"query"` |
| 59 | + Variables map[string]interface{} `json:"variables"` |
| 60 | +} |
| 61 | + |
| 62 | +func writeJSON(w http.ResponseWriter, v interface{}) { |
| 63 | + w.Header().Set("Content-Type", "application/json") |
| 64 | + json.NewEncoder(w).Encode(v) //nolint:errcheck |
| 65 | +} |
| 66 | + |
| 67 | +func handleQuery(w http.ResponseWriter, r *http.Request) { |
| 68 | + var req gqlRequest |
| 69 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 70 | + writeJSON(w, map[string]interface{}{"errors": []map[string]interface{}{{"message": err.Error()}}}) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + switch { |
| 75 | + case strings.Contains(req.Query, "_service"): |
| 76 | + writeJSON(w, map[string]interface{}{ |
| 77 | + "data": map[string]interface{}{"_service": map[string]interface{}{"sdl": sdl}}, |
| 78 | + }) |
| 79 | + case strings.Contains(req.Query, "_entities"): |
| 80 | + handleEntities(w, req) |
| 81 | + case strings.Contains(req.Query, "location"): |
| 82 | + handleLocationQuery(w, req) |
| 83 | + default: |
| 84 | + writeJSON(w, map[string]interface{}{"data": map[string]interface{}{"__typename": "Query"}}) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func handleLocationQuery(w http.ResponseWriter, req gqlRequest) { |
| 89 | + id, _ := req.Variables["id"].(string) |
| 90 | + loc, ok := locationDB[id] |
| 91 | + if !ok { |
| 92 | + writeJSON(w, map[string]interface{}{"data": map[string]interface{}{"location": nil}}) |
| 93 | + return |
| 94 | + } |
| 95 | + writeJSON(w, map[string]interface{}{ |
| 96 | + "data": map[string]interface{}{ |
| 97 | + "location": map[string]interface{}{ |
| 98 | + "__typename": "Location", |
| 99 | + "name": loc.Name, |
| 100 | + "coordinate": map[string]interface{}{ |
| 101 | + "lat": loc.Coordinate.Lat, |
| 102 | + "lng": loc.Coordinate.Lng, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +func handleEntities(w http.ResponseWriter, req gqlRequest) { |
| 110 | + repsRaw, _ := req.Variables["representations"].([]interface{}) |
| 111 | + entities := make([]interface{}, 0, len(repsRaw)) |
| 112 | + for _, repRaw := range repsRaw { |
| 113 | + repMap, ok := repRaw.(map[string]interface{}) |
| 114 | + if !ok { |
| 115 | + entities = append(entities, nil) |
| 116 | + continue |
| 117 | + } |
| 118 | + coordRaw, ok := repMap["coordinate"].(map[string]interface{}) |
| 119 | + if !ok { |
| 120 | + entities = append(entities, nil) |
| 121 | + continue |
| 122 | + } |
| 123 | + lat, _ := coordRaw["lat"].(float64) |
| 124 | + lng, _ := coordRaw["lng"].(float64) |
| 125 | + loc := findByCoordinate(lat, lng) |
| 126 | + if loc == nil { |
| 127 | + entities = append(entities, nil) |
| 128 | + continue |
| 129 | + } |
| 130 | + entities = append(entities, map[string]interface{}{ |
| 131 | + "__typename": "Location", |
| 132 | + "name": loc.Name, |
| 133 | + "coordinate": map[string]interface{}{"lat": loc.Coordinate.Lat, "lng": loc.Coordinate.Lng}, |
| 134 | + }) |
| 135 | + } |
| 136 | + writeJSON(w, map[string]interface{}{ |
| 137 | + "data": map[string]interface{}{"_entities": entities}, |
| 138 | + }) |
| 139 | +} |
| 140 | + |
| 141 | +func main() { |
| 142 | + port := os.Getenv("PORT") |
| 143 | + if port == "" { |
| 144 | + port = "8080" |
| 145 | + } |
| 146 | + http.HandleFunc("/query", handleQuery) |
| 147 | + log.Printf("locations service listening on :%s", port) |
| 148 | + log.Fatal(http.ListenAndServe(":"+port, nil)) |
| 149 | +} |
0 commit comments