Initial commit

This commit is contained in:
2021-03-27 09:11:37 +00:00
commit 6b59e24be5
8 changed files with 259 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
on: push
name: Run unit tests
jobs:
checks:
name: Run unit tests
runs-on: ubuntu-latest
env:
CI: true
steps:
- uses: actions/checkout@master
- name: run
uses: cedrickring/[email protected]
with:
args: |
make prepare
make test CI_RUN=${CI}
+2
View File
@@ -0,0 +1,2 @@
*.out
*.test
+16
View File
@@ -0,0 +1,16 @@
CI_RUN?=false
ADDITIONAL_BUILD_FLAGS=""
ifeq ($(CI_RUN), true)
ADDITIONAL_BUILD_FLAGS="-test.short"
endif
prepare:
go get -u -v github.com/go-critic/go-critic/cmd/gocritic
test:
gocritic check
go test $(ADDITIONAL_BUILD_FLAGS) -v -coverprofile cover.out -memprofile mem.out
lint:
go fmt
+38
View File
@@ -0,0 +1,38 @@
# Simple GraphQL client
[![Run unit tests](https://github.com/lukaszraczylo/simple-gql-client/actions/workflows/test.yaml/badge.svg)](https://github.com/lukaszraczylo/simple-gql-client/actions/workflows/test.yaml)
It's Hasura friendly.
## Reasoning
I've tried to run few graphQL clients with hasura, all of them required conversion of the data into
the appropriate structures, causing issues with non-existing types ( thanks to Hasura ), for example `bigint` which was difficult to export.
Therefore, I present you the simple client to which you can copy & paste your graphQL query, variables and you are good to go.
## Usage
```go
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)
```
**Result**
```json
{"tbl_user_group_admins":[{"id":109,"is_admin":1}]}
```
+11
View File
@@ -0,0 +1,11 @@
module github.com/lukaszraczylo/simple-gql-client
go 1.16
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.7.3
github.com/valyala/fasthttp v1.22.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
+42
View File
@@ -0,0 +1,42 @@
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/klauspost/compress v1.11.8 h1:difgzQsp5mdAz9v8lm3P/I+EpDKMU/6uTMw1y1FObuo=
github.com/klauspost/compress v1.11.8/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.7.3 h1:9dOulDrkCJf1mwljVMhXNQr9ZL2NvajRX7A1R8c6Qxw=
github.com/tidwall/gjson v1.7.3/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.22.0 h1:OpwH5KDOJ9cS2bq8fD+KfT4IrksK0llvkHf4MZx42jQ=
github.com/valyala/fasthttp v1.22.0/go.mod h1:0mw2RjXGOzxf4NL2jni3gUQ7LfjjUSiG5sskOUUSEpU=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20210226101413-39120d07d75e/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+73
View File
@@ -0,0 +1,73 @@
package gql
import (
"bytes"
"encoding/json"
"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 {
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 j.Bytes()
}
func Query(query string, variables interface{}, headers map[string]interface{}) string {
readyQuery := queryBuilder(query, variables)
req := fasthttp.AcquireRequest()
req.Header.SetContentType("application/json")
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 {
panic("Unable to execute request")
}
fasthttp.ReleaseRequest(req)
body := res.Body()
toReturn := gjson.Get(string(body), "data")
if toReturn.String() == "" {
fmt.Println("Probably not what you expect:", string(body))
}
fasthttp.ReleaseResponse(res)
return toReturn.String()
}
// 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)
// }
+60
View File
@@ -0,0 +1,60 @@
package gql
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_simpleQueryBuilder(t *testing.T) {
assert := assert.New(t)
q := `query listUserBots {
tbl_bots {
bot_name
}
}`
result := queryBuilder(q, nil)
expected := `{"query":"query listUserBots {\n\t\ttbl_bots {\n \tbot_name\n \t}\n\t}","variables":null}`
assert.Equal(expected, string(result), "Produced simple query should be equal")
}
func Test_advancedQueryBuilder(t *testing.T) {
assert := assert.New(t)
q := `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
}
}`
v := map[string]interface{}{
"UserID": 37,
"GroupID": 11007,
}
result := queryBuilder(q, v)
expected := `{"query":"query checkifUserIsAdmin($UserID: bigint, $GroupID: bigint) {\n\t\ttbl_user_group_admins(where: {is_admin: {_eq: \"1\"}, user_id: {_eq: $UserID}, group_id: {_eq: $GroupID}}) {\n\t\t\tid\n\t\t\tis_admin\n\t\t}\n\t}","variables":{"GroupID":11007,"UserID":37}}`
assert.Equal(expected, string(result), "Produced advanced query should be equal")
}
func Test_queryAgainstDatabaseExecution(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short / CI mode")
}
assert := assert.New(t)
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)
expected := `{"tbl_user_group_admins":[{"id":109,"is_admin":1}]}`
assert.Equal(expected, string(result), "Query result execution should be equal")
}