graphql testin'
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
package emgql
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
@@ -50,3 +55,41 @@ func New(options ...Option) (*Client, error) {
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (c *Client) graphquery(ctx context.Context, q string, vars map[string]string) (io.ReadCloser, error) {
|
||||
qObj := struct {
|
||||
Query string `json:"query"`
|
||||
Variables map[string]string `json:"variables"`
|
||||
}{
|
||||
Query: q,
|
||||
Variables: vars,
|
||||
}
|
||||
|
||||
res, err := json.Marshal(qObj)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request object: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL.String(), bytes.NewBuffer(res))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to build post request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Add("content-type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to make post request: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, err2 := io.ReadAll(resp.Body)
|
||||
if err2 != nil {
|
||||
return nil, fmt.Errorf("got unexpected response code [%d]: unable to parse request body: %v", resp.StatusCode, err)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("got unexpected response code [%d]: %s", resp.StatusCode, string(b))
|
||||
}
|
||||
return resp.Body, nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user