Files
go-simple-graphql/gql.go
T

76 lines
2.0 KiB
Go

package gql
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/tidwall/gjson"
"github.com/valyala/fasthttp"
)
var GraphQLUrl = "http://127.0.0.1:9090/v1/graphql"
type requestBase struct {
Query string `json:"query"`
Variables interface{} `json:"variables"`
}
func queryBuilder(data string, variables interface{}) ([]byte, error) {
var err error
var qb requestBase
qb.Query = data
qb.Variables = variables
j := new(bytes.Buffer)
j2, _ := json.Marshal(qb)
if err := json.Compact(j, j2); err != nil {
return []byte{}, errors.New(fmt.Sprintf("Unable to process specified query. Check if it's valid: %s", err.Error()))
}
// Attemt to decrease size of the generated JSON
compactedBuffer := new(bytes.Buffer)
err = json.Compact(compactedBuffer, j.Bytes())
if err != nil {
return []byte{}, errors.New(fmt.Sprintf("Unable to compress your query: %s", err.Error()))
}
return []byte(compactedBuffer.String()), err
}
func Query(query string, variables interface{}, headers map[string]interface{}) (string, error) {
readyQuery, err := queryBuilder(query, variables)
if err != nil {
return "", err
}
req := fasthttp.AcquireRequest()
req.Header.SetContentType("application/json")
req.Header.Set("Accept-Encoding", "gzip")
for header, value := range headers {
req.Header.Set(fmt.Sprintf("%v", header), fmt.Sprintf("%v", value))
}
req.Header.SetMethodBytes([]byte("POST"))
req.SetBody(readyQuery)
req.SetRequestURI(GraphQLUrl)
res := fasthttp.AcquireResponse()
if err := fasthttp.Do(req, res); err != nil {
return "", err
}
fasthttp.ReleaseRequest(req)
contentEncoding := res.Header.Peek("Content-Encoding")
var body []byte
if bytes.EqualFold(contentEncoding, []byte("gzip")) {
body, _ = res.BodyGunzip()
} else {
body = res.Body()
}
toReturn := gjson.Get(string(body), "data")
if toReturn.String() == "" {
fmt.Println("Probably not what you expect:", string(body))
return string(body), errors.New(fmt.Sprintf("Probably not what you expect: %s", body))
}
fasthttp.ReleaseResponse(res)
return toReturn.String(), err
}