@@ -45,7 +45,7 @@ if err != nil {
4545
4646fmt.Printf("%+v\n", resp)
4747*/
48- var GraphQL = func (query string , variables map [string ]string , v interface {}) error {
48+ var GraphQL = func (query string , variables map [string ]string , data interface {}) error {
4949 url := "https://api.github.com/graphql"
5050 reqBody , err := json .Marshal (map [string ]interface {}{"query" : query , "variables" : variables })
5151 if err != nil {
@@ -81,29 +81,31 @@ var GraphQL = func(query string, variables map[string]string, v interface{}) err
8181 }
8282
8383 debugResponse (resp , string (body ))
84- return handleResponse (resp , body , v )
84+ return handleResponse (resp , body , data )
8585}
8686
87- func handleResponse (resp * http.Response , body []byte , v interface {}) error {
87+ func handleResponse (resp * http.Response , body []byte , data interface {}) error {
8888 success := resp .StatusCode >= 200 && resp .StatusCode < 300
8989
90- if success {
91- gr := & graphQLResponse {Data : v }
92- err := json .Unmarshal (body , & gr )
93- if err != nil {
94- return err
95- }
96- if len (gr .Errors ) > 0 {
97- errorMessages := gr .Errors [0 ].Message
98- for _ , e := range gr .Errors [1 :] {
99- errorMessages += ", " + e .Message
100- }
101- return fmt .Errorf ("graphql error: '%s'" , errorMessages )
90+ if ! success {
91+ return handleHTTPError (resp , body )
92+ }
93+
94+ gr := & graphQLResponse {Data : data }
95+ err := json .Unmarshal (body , & gr )
96+ if err != nil {
97+ return err
98+ }
99+
100+ if len (gr .Errors ) > 0 {
101+ errorMessages := gr .Errors [0 ].Message
102+ for _ , e := range gr .Errors [1 :] {
103+ errorMessages += ", " + e .Message
102104 }
103- return nil
105+ return fmt . Errorf ( "graphql error: '%s'" , errorMessages )
104106 }
107+ return nil
105108
106- return handleHTTPError (resp , body )
107109}
108110
109111func handleHTTPError (resp * http.Response , body []byte ) error {
0 commit comments