Compare commits

..
4 Commits
Author SHA1 Message Date
lukaszraczylo d843238d46 Add missing retry mechanism.
I have no idea how this was missed, but ho hum.
2023-04-08 01:59:28 +01:00
lukaszraczylo b9cd614ae4 Revert default output to string for backwards compatibility. 2023-04-08 01:38:55 +01:00
lukaszraczylo eef4e39611 Add ability to set the output for query. 2023-04-08 01:38:29 +01:00
lukaszraczylo d764980390 Add mapstring output and update the README. 2023-04-08 01:36:56 +01:00
4 changed files with 21 additions and 1 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ Therefore, I present you the simple client to which you can copy & paste your gr
* `GRAPHQL_ENDPOINT` - Your GraphQL endpoint. Default: `http://127.0.0.1:9090/v1/graphql`
* `GRAPHQL_CACHE_ENABLED` - Should the query cache be enabled? Default: `false`
* `GRAPHQL_CACHE_TTL` - Cache TTL in seconds for SELECT type of queries. Default: `5`
* `GRAPHQL_OUTPUT` - Output format. Default: `byte`, available: `byte`, `string`
* `GRAPHQL_OUTPUT` - Output format. Default: `string`, available: `byte`, `string`, `mapstring`
* `LOG_LEVEL` - Logging level. Default: `info` available: `debug`, `info`, `warn`, `error`
* `GRAPHQL_RETRIES_ENABLE` - Should retries be enabled? Default: `true`
* `GRAPHQL_RETRIES_NUMBER` - Number of retries: Default: `1`
+4
View File
@@ -98,3 +98,7 @@ func NewConnection() *BaseClient {
func (b *BaseClient) SetEndpoint(endpoint string) {
b.endpoint = endpoint
}
func (b *BaseClient) SetOutput(output string) {
b.responseType = output
}
+8
View File
@@ -120,6 +120,14 @@ func (c *BaseClient) Query(queryContent string, queryVariables interface{}, quer
func (c *BaseClient) decodeResponse(jsonData []byte) any {
switch c.responseType {
case "mapstring":
var response map[string]interface{}
err := json.Unmarshal(jsonData, &response)
if err != nil {
c.Logger.Error(c, "Error while converting to map[string]interface{};", "error", err.Error())
return nil
}
return response
case "string":
return helpers.BytesToString(jsonData)
case "byte":
+8
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/avast/retry-go/v4"
)
@@ -50,6 +51,13 @@ func (c *BaseClient) executeQuery(query []byte, headers any) (result any, err er
return nil
},
retry.OnRetry(func(n uint, err error) {
c.Logger.Error(c, "Retrying query", "error", err.Error())
}),
retry.Attempts(uint(c.retries.max)),
retry.DelayType(retry.BackOffDelay),
retry.Delay(time.Duration(c.retries.delay)*time.Second),
retry.LastErrorOnly(true),
)
if len(queryResult.Errors) > 0 {