runGQLRequest runs a HTTP GraphQL request and returns the data or any errors.
(req *http.Request)
| 129 | |
| 130 | // runGQLRequest runs a HTTP GraphQL request and returns the data or any errors. |
| 131 | func runGQLRequest(req *http.Request) ([]byte, error) { |
| 132 | config, err := x.LoadClientTLSConfig(Upgrade.Conf) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | tr := &http.Transport{TLSClientConfig: config} |
| 137 | client := &http.Client{Timeout: 50 * time.Second, Transport: tr} |
| 138 | resp, err := client.Do(req) |
| 139 | if err != nil { |
| 140 | return nil, err |
| 141 | } |
| 142 | |
| 143 | // GraphQL server should always return OK, even when there are errors |
| 144 | if status := resp.StatusCode; status != http.StatusOK { |
| 145 | return nil, errors.Errorf("unexpected status code: %v", status) |
| 146 | } |
| 147 | |
| 148 | if strings.ToLower(resp.Header.Get("Content-Type")) != "application/json" { |
| 149 | return nil, errors.Errorf("unexpected content type: %v", resp.Header.Get("Content-Type")) |
| 150 | } |
| 151 | |
| 152 | if resp.Header.Get("Access-Control-Allow-Origin") != "*" { |
| 153 | return nil, errors.Errorf("cors headers weren't set in response") |
| 154 | } |
| 155 | |
| 156 | defer func() { |
| 157 | if err := resp.Body.Close(); err != nil { |
| 158 | glog.Warningf("error closing body: %v", err) |
| 159 | } |
| 160 | }() |
| 161 | body, err := io.ReadAll(resp.Body) |
| 162 | if err != nil { |
| 163 | return nil, errors.Errorf("unable to read response body: %v", err) |
| 164 | } |
| 165 | |
| 166 | return body, nil |
| 167 | } |
| 168 | |
| 169 | // getQueryResult executes the given query and unmarshals the result in given pointer queryResPtr. |
| 170 | // If any error is encountered, it returns the error. |
no test coverage detected