mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-18 01:27:25 +00:00
Query handler should return errors for easier debugging.
This commit is contained in:
@@ -3,6 +3,7 @@ package gql
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -36,7 +37,8 @@ func queryBuilder(data string, variables interface{}) []byte {
|
|||||||
return []byte(compactedBuffer.String())
|
return []byte(compactedBuffer.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Query(query string, variables interface{}, headers map[string]interface{}) string {
|
func Query(query string, variables interface{}, headers map[string]interface{}) (string, error) {
|
||||||
|
var err error
|
||||||
readyQuery := queryBuilder(query, variables)
|
readyQuery := queryBuilder(query, variables)
|
||||||
req := fasthttp.AcquireRequest()
|
req := fasthttp.AcquireRequest()
|
||||||
req.Header.SetContentType("application/json")
|
req.Header.SetContentType("application/json")
|
||||||
@@ -50,7 +52,7 @@ func Query(query string, variables interface{}, headers map[string]interface{})
|
|||||||
|
|
||||||
res := fasthttp.AcquireResponse()
|
res := fasthttp.AcquireResponse()
|
||||||
if err := fasthttp.Do(req, res); err != nil {
|
if err := fasthttp.Do(req, res); err != nil {
|
||||||
panic("Unable to execute request")
|
return "", err
|
||||||
}
|
}
|
||||||
fasthttp.ReleaseRequest(req)
|
fasthttp.ReleaseRequest(req)
|
||||||
|
|
||||||
@@ -65,26 +67,8 @@ func Query(query string, variables interface{}, headers map[string]interface{})
|
|||||||
toReturn := gjson.Get(string(body), "data")
|
toReturn := gjson.Get(string(body), "data")
|
||||||
if toReturn.String() == "" {
|
if toReturn.String() == "" {
|
||||||
fmt.Println("Probably not what you expect:", string(body))
|
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)
|
fasthttp.ReleaseResponse(res)
|
||||||
return toReturn.String()
|
return toReturn.String(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// func main() {
|
|
||||||
// headers := map[string]interface{}{
|
|
||||||
// "x-hasura-user-id": 37,
|
|
||||||
// "x-hasura-user-uuid": "bde3262e-b42e-4151-ac10-d43fb38f44a5",
|
|
||||||
// }
|
|
||||||
// variables := map[string]interface{}{
|
|
||||||
// "UserID": 37,
|
|
||||||
// "GroupID": 11007,
|
|
||||||
// }
|
|
||||||
// var query = `query checkifUserIsAdmin($UserID: bigint, $GroupID: bigint) {
|
|
||||||
// tbl_user_group_admins(where: {is_admin: {_eq: "1"}, user_id: {_eq: $UserID}, group_id: {_eq: $GroupID}}) {
|
|
||||||
// id
|
|
||||||
// is_admin
|
|
||||||
// }
|
|
||||||
// }`
|
|
||||||
// result := Query(query, variables, headers)
|
|
||||||
// fmt.Println(result)
|
|
||||||
// }
|
|
||||||
|
|||||||
+4
-1
@@ -66,7 +66,10 @@ func Test_queryAgainstDatabaseExecution(t *testing.T) {
|
|||||||
is_admin
|
is_admin
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
result := Query(query, variables, headers)
|
result, err := Query(query, variables, headers)
|
||||||
|
if err != nil {
|
||||||
|
t.Log("Query execution errored. Is GQL server up?")
|
||||||
|
}
|
||||||
expected := `{"tbl_user_group_admins":[{"id":109,"is_admin":1}]}`
|
expected := `{"tbl_user_group_admins":[{"id":109,"is_admin":1}]}`
|
||||||
assert.Equal(expected, string(result), "Query result execution should be equal")
|
assert.Equal(expected, string(result), "Query result execution should be equal")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user