SetStatusWithData sets the errors in the response and ensures that the data key in the data is present with value nil. In case an error was encountered after the query execution started, we have to return data key with null value according to GraphQL spec.
(w http.ResponseWriter, code, msg string)
| 405 | // In case an error was encountered after the query execution started, we have to return data |
| 406 | // key with null value according to GraphQL spec. |
| 407 | func SetStatusWithData(w http.ResponseWriter, code, msg string) { |
| 408 | var qr QueryResWithData |
| 409 | ext := make(map[string]interface{}) |
| 410 | ext["code"] = code |
| 411 | qr.Errors = append(qr.Errors, &GqlError{Message: msg, Extensions: ext}) |
| 412 | // This would ensure that data key is present with value null. |
| 413 | if js, err := json.Marshal(qr); err == nil { |
| 414 | if _, err := w.Write(js); err != nil { |
| 415 | glog.Errorf("Error while writing: %+v", err) |
| 416 | } |
| 417 | } else { |
| 418 | Panic(errors.Errorf("Unable to marshal: %+v", qr)) |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // Reply sets the body of an HTTP response to the JSON representation of the given reply. |
| 423 | func Reply(w http.ResponseWriter, rep interface{}) { |
no test coverage detected