mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-13 00:48:37 +00:00
Adding few extra errors never hurt anyone.
This commit is contained in:
@@ -9,7 +9,7 @@ prepare:
|
||||
go get github.com/go-critic/go-critic/cmd/gocritic
|
||||
|
||||
test:
|
||||
gocritic check
|
||||
gocritic check -disable=unslice
|
||||
go test -race $(ADDITIONAL_BUILD_FLAGS) -v -coverprofile coverage.txt -covermode=atomic
|
||||
|
||||
lint:
|
||||
|
||||
@@ -17,29 +17,30 @@ type requestBase struct {
|
||||
Variables interface{} `json:"variables"`
|
||||
}
|
||||
|
||||
func queryBuilder(data string, variables interface{}) []byte {
|
||||
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 {
|
||||
fmt.Println(err)
|
||||
panic("Unable to process the query")
|
||||
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())
|
||||
err = json.Compact(compactedBuffer, j.Bytes())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
panic("Unable to compress json")
|
||||
return []byte{}, errors.New(fmt.Sprintf("Unable to compress your query: %s", err.Error()))
|
||||
}
|
||||
return []byte(compactedBuffer.String())
|
||||
return []byte(compactedBuffer.String()), err
|
||||
}
|
||||
|
||||
func Query(query string, variables interface{}, headers map[string]interface{}) (string, error) {
|
||||
var err error
|
||||
readyQuery := queryBuilder(query, variables)
|
||||
readyQuery, err := queryBuilder(query, variables)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req := fasthttp.AcquireRequest()
|
||||
req.Header.SetContentType("application/json")
|
||||
req.Header.Set("Accept-Encoding", "gzip")
|
||||
|
||||
+2
-2
@@ -40,8 +40,8 @@ func Test_queryBuilder(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
assert := assert.New(t)
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gets := string(queryBuilder(tt.args.data, tt.args.variables))
|
||||
assert.Equal(tt.want, gets, fmt.Sprintf("Unexpected query output in test %s", tt.name))
|
||||
gets, _ := queryBuilder(tt.args.data, tt.args.variables)
|
||||
assert.Equal(tt.want, string(gets), fmt.Sprintf("Unexpected query output in test %s", tt.name))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user