-
Notifications
You must be signed in to change notification settings - Fork 3
/
response.go
34 lines (27 loc) · 890 Bytes
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package graphql
import (
"encoding/json"
"fmt"
"github.com/chirino/graphql/qerrors"
)
// Response represents a typical response of a GraphQL server. It may be encoded to JSON directly or
// it may be further processed to a custom response type, for example to include custom error data.
type Response struct {
Data json.RawMessage `json:"data,omitempty"`
Errors ErrorList `json:"errors,omitempty"`
Extensions interface{} `json:"extensions,omitempty"`
Details map[string]interface{} `json:"-"`
}
func NewResponse() *Response {
return &Response{}
}
func (r *Response) Error() error {
return r.Errors.Error()
}
func (r *Response) String() string {
return fmt.Sprintf("{Data: %s, Errors: %v}", string(r.Data), r.Errors)
}
func (r *Response) AddError(err error) *Response {
r.Errors = qerrors.AppendErrors(r.Errors, err)
return r
}