Skip to content

Commit

Permalink
return spec correct gql errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Sep 19, 2024
1 parent 5b58c19 commit 2497113
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions http/handler_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,12 @@ type GraphQLResponse struct {
}

func (res GraphQLResponse) MarshalJSON() ([]byte, error) {
var errors []string
for _, err := range res.Errors {
errors = append(errors, err.Error())
errors := make([]map[string]any, len(res.Errors))
for i, err := range res.Errors {
errors[i] = map[string]any{"message": err.Error()}
}
if len(errors) == 0 {
return json.Marshal(map[string]any{"data": res.Data})
}
return json.Marshal(map[string]any{"data": res.Data, "errors": errors})
}
Expand All @@ -307,7 +310,12 @@ func (res *GraphQLResponse) UnmarshalJSON(data []byte) error {
switch t := out["errors"].(type) {
case []any:
for _, v := range t {
res.Errors = append(res.Errors, parseError(v))
err, ok := v.(map[string]any)
if !ok {
res.Errors = append(res.Errors, parseError(err["message"]))

Check warning on line 315 in http/handler_store.go

View check run for this annotation

Codecov / codecov/patch

http/handler_store.go#L315

Added line #L315 was not covered by tests
} else {
res.Errors = append(res.Errors, fmt.Errorf("%v", v))
}
}
default:
res.Errors = nil
Expand Down

0 comments on commit 2497113

Please sign in to comment.