mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-13 00:48:37 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8044c5d67
|
||
|
|
cf3076d701
|
||
|
|
5a222aa316
|
||
|
|
d843238d46
|
||
|
|
b9cd614ae4
|
||
|
|
eef4e39611
|
||
|
|
d764980390
|
||
|
|
689b1efc43
|
@@ -9,6 +9,7 @@ Ps. It's Hasura friendly.
|
|||||||
- [Features](#features)
|
- [Features](#features)
|
||||||
- [Usage example](#usage-example)
|
- [Usage example](#usage-example)
|
||||||
- [Environment variables](#environment-variables)
|
- [Environment variables](#environment-variables)
|
||||||
|
- [Modifiers on the fly](#modifiers-on-the-fly)
|
||||||
- [Cache](#cache)
|
- [Cache](#cache)
|
||||||
- [Example reader code](#example-reader-code)
|
- [Example reader code](#example-reader-code)
|
||||||
- [Tips](#tips)
|
- [Tips](#tips)
|
||||||
@@ -34,25 +35,34 @@ 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_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_ENABLED` - Should the query cache be enabled? Default: `false`
|
||||||
* `GRAPHQL_CACHE_TTL` - Cache TTL in seconds for SELECT type of queries. Default: `5`
|
* `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`
|
* `LOG_LEVEL` - Logging level. Default: `info` available: `debug`, `info`, `warn`, `error`
|
||||||
* `GRAPHQL_RETRIES_ENABLE` - Should retries be enabled? Default: `true`
|
* `GRAPHQL_RETRIES_ENABLE` - Should retries be enabled? Default: `true`
|
||||||
* `GRAPHQL_RETRIES_NUMBER` - Number of retries: Default: `1`
|
* `GRAPHQL_RETRIES_NUMBER` - Number of retries: Default: `1`
|
||||||
* `GRAPHQL_RETRIES_DELAY` - Delay in retries in milliseconds. Default: `250`
|
* `GRAPHQL_RETRIES_DELAY` - Delay in retries in milliseconds. Default: `250`
|
||||||
|
|
||||||
|
### Modifiers on the fly
|
||||||
|
|
||||||
|
* `gql.SetEndpoint('your-endpoint-url')` - modifies endpoint, without the need to set the environment variable
|
||||||
|
* `gql.SetOutput('byte')` - modifies output format, without the need to set the environment variable
|
||||||
|
|
||||||
### Cache
|
### Cache
|
||||||
|
|
||||||
You have two options to enable the cache:
|
You have two options to enable the cache:
|
||||||
|
|
||||||
* Use `GRAPHQL_CACHE_ENABLED` environment variable which will enable the cache globally. It may be desired if you want to use the cache for all queries.
|
* Use `GRAPHQL_CACHE_ENABLED` environment variable which will enable the cache globally. It may be desired if you want to use the cache for all queries.
|
||||||
* Add `gqlcache: true` header for your query which will enable the cache for this query only with `GRAPHQL_CACHE_TTL` TTL.
|
* Add `gqlcache: true` header for your query which will enable the cache for this query only with `GRAPHQL_CACHE_TTL` TTL.
|
||||||
|
* You can check the list of supported per-query modifiers below
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
// following values passed as headers will modify behaviour of the query
|
||||||
|
// and disregard settings provided via environment variables
|
||||||
headers := map[string]interface{}{
|
headers := map[string]interface{}{
|
||||||
...
|
...
|
||||||
"gqlcache": true,
|
"gqlcache": true, // sets the cache as on for this query only
|
||||||
|
"gqlretries": false, // disables retries for this query only
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -30,8 +30,6 @@ func NewConnection() *BaseClient {
|
|||||||
|
|
||||||
if b.LoggerWriter == nil {
|
if b.LoggerWriter == nil {
|
||||||
b.LoggerWriter = log.New(os.Stdout, "", log.LstdFlags)
|
b.LoggerWriter = log.New(os.Stdout, "", log.LstdFlags)
|
||||||
} else {
|
|
||||||
b.LoggerWriter = b.LoggerWriter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
b.endpoint = envutil.Getenv("GRAPHQL_ENDPOINT", "https://api.github.com/graphql")
|
b.endpoint = envutil.Getenv("GRAPHQL_ENDPOINT", "https://api.github.com/graphql")
|
||||||
@@ -96,3 +94,11 @@ func NewConnection() *BaseClient {
|
|||||||
b.Logger.Info(b, "graphQL client initialized;")
|
b.Logger.Info(b, "graphQL client initialized;")
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BaseClient) SetEndpoint(endpoint string) {
|
||||||
|
b.endpoint = endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BaseClient) SetOutput(output string) {
|
||||||
|
b.responseType = output
|
||||||
|
}
|
||||||
|
|||||||
+29
-1
@@ -5,7 +5,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/lukaszraczylo/go-simple-graphql/utils/logger"
|
"github.com/lukaszraczylo/go-simple-graphql/utils/logger"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -101,3 +100,32 @@ func TestNewConnectionLogLevels(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBaseClient_SetEndpoint(t *testing.T) {
|
||||||
|
type fields struct {
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
endpoint string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
fields fields
|
||||||
|
args args
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Test SetEndpoint",
|
||||||
|
args: args{
|
||||||
|
endpoint: "https://potato/graphql",
|
||||||
|
},
|
||||||
|
want: "https://potato/graphql",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
b := NewConnection()
|
||||||
|
b.SetEndpoint(tt.args.endpoint)
|
||||||
|
assert.Equal(t, tt.want, b.endpoint)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -42,22 +42,14 @@ github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavM
|
|||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||||
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9 h1:yZNXmy+j/JpX19vZkVktWqAo7Gny4PBWYYK3zskGpx4=
|
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9 h1:yZNXmy+j/JpX19vZkVktWqAo7Gny4PBWYYK3zskGpx4=
|
||||||
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
||||||
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
|
|
||||||
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
|
|
||||||
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
|
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
|
||||||
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
|
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
|
||||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
|
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
|
||||||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
|
|
||||||
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
|
|
||||||
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
|
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
|
||||||
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
|
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
|
||||||
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
|
|
||||||
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
|
||||||
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
||||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ package gql
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/lukaszraczylo/go-simple-graphql/utils/helpers"
|
"github.com/lukaszraczylo/go-simple-graphql/utils/helpers"
|
||||||
|
|
||||||
@@ -67,45 +70,66 @@ func (c *BaseClient) NewQuery(q ...any) *Query {
|
|||||||
// It looks a bit weird because of the backward compatibility
|
// It looks a bit weird because of the backward compatibility
|
||||||
|
|
||||||
func (c *BaseClient) Query(queryContent string, queryVariables interface{}, queryHeaders map[string]interface{}) (any, error) {
|
func (c *BaseClient) Query(queryContent string, queryVariables interface{}, queryHeaders map[string]interface{}) (any, error) {
|
||||||
query := c.NewQuery(queryContent, queryVariables)
|
|
||||||
|
|
||||||
var queryHash string
|
var queryHash string
|
||||||
var cachedResponse []byte
|
var cachedResponse []byte
|
||||||
|
|
||||||
if c.cache.enabled {
|
cbc := reflect.ValueOf(*c).Interface().(BaseClient)
|
||||||
queryHash = strutil.Md5(query.compiledQuery)
|
cacheBaseClient := &cbc
|
||||||
c.Logger.Debug(c, "Hash calculated;", "hash:", queryHash)
|
|
||||||
|
|
||||||
cachedResponse = c.cacheLookup(queryHash)
|
query := cacheBaseClient.NewQuery(queryContent, queryVariables)
|
||||||
if cachedResponse != nil {
|
|
||||||
c.Logger.Debug(c, "Found cached response")
|
// Check for library specific headers
|
||||||
return c.decodeResponse(cachedResponse), nil
|
if len(queryHeaders) > 0 {
|
||||||
} else {
|
queryHeadersModified := c.parseQueryHeaders(queryHeaders)
|
||||||
c.Logger.Debug(c, "No cached response found")
|
// compare if there are any changes
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(queryHeadersModified, queryHeaders) {
|
||||||
|
cacheBaseClient.Logger.Debug(cacheBaseClient, "Headers modified, creating a new client")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := c.executeQuery(query.compiledQuery, queryHeaders)
|
if cacheBaseClient.cache.enabled {
|
||||||
|
queryHash = strutil.Md5(fmt.Sprintf("%s-%+v", query.compiledQuery, queryHeaders))
|
||||||
|
cacheBaseClient.Logger.Debug(cacheBaseClient, "Hash calculated;", "hash:", queryHash)
|
||||||
|
|
||||||
|
cachedResponse = cacheBaseClient.cacheLookup(queryHash)
|
||||||
|
if cachedResponse != nil {
|
||||||
|
cacheBaseClient.Logger.Debug(cacheBaseClient, "Found cached response")
|
||||||
|
return cacheBaseClient.decodeResponse(cachedResponse), nil
|
||||||
|
} else {
|
||||||
|
cacheBaseClient.Logger.Debug(cacheBaseClient, "No cached response found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := cacheBaseClient.executeQuery(query.compiledQuery, queryHeaders)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Logger.Error(c, "Error while executing query;", "error", err.Error())
|
cacheBaseClient.Logger.Error(cacheBaseClient, "Error while executing query;", "error", err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonData, err := json.Marshal(response)
|
jsonData, err := json.Marshal(response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Logger.Error(c, "Error while converting to json;", "error", err.Error())
|
cacheBaseClient.Logger.Error(cacheBaseClient, "Error while converting to json;", "error", err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.cache.enabled {
|
if cacheBaseClient.cache.enabled {
|
||||||
c.cache.client.Set(queryHash, jsonData)
|
cacheBaseClient.cache.client.Set(queryHash, jsonData)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.decodeResponse(jsonData), err
|
return cacheBaseClient.decodeResponse(jsonData), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *BaseClient) decodeResponse(jsonData []byte) any {
|
func (c *BaseClient) decodeResponse(jsonData []byte) any {
|
||||||
switch c.responseType {
|
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":
|
case "string":
|
||||||
return helpers.BytesToString(jsonData)
|
return helpers.BytesToString(jsonData)
|
||||||
case "byte":
|
case "byte":
|
||||||
@@ -115,3 +139,19 @@ func (c *BaseClient) decodeResponse(jsonData []byte) any {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *BaseClient) parseQueryHeaders(queryHeaders map[string]interface{}) (returnHeaders map[string]interface{}) {
|
||||||
|
returnHeaders = make(map[string]interface{})
|
||||||
|
for k, v := range queryHeaders {
|
||||||
|
if k == "gqlcache" {
|
||||||
|
c.cache.enabled, _ = strconv.ParseBool(fmt.Sprintf("%v", v))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if k == "gqlretries" {
|
||||||
|
c.retries.enabled, _ = strconv.ParseBool(fmt.Sprintf("%v", v))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
returnHeaders[k] = v
|
||||||
|
}
|
||||||
|
return returnHeaders
|
||||||
|
}
|
||||||
|
|||||||
+124
@@ -289,3 +289,127 @@ func BenchmarkQueryWithCache(b *testing.B) {
|
|||||||
}`, nil, nil)
|
}`, nil, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBaseClient_parseQueryHeaders(t *testing.T) {
|
||||||
|
type fields struct {
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
queryHeaders map[string]interface{}
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
fields fields
|
||||||
|
args args
|
||||||
|
wantReturnHeaders map[string]interface{}
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Test parseQueryHeaders - no change",
|
||||||
|
fields: fields{},
|
||||||
|
args: args{
|
||||||
|
queryHeaders: map[string]interface{}{
|
||||||
|
"x-test-header": "test",
|
||||||
|
"x-test-header-next": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantReturnHeaders: map[string]interface{}{
|
||||||
|
"x-test-header": "test",
|
||||||
|
"x-test-header-next": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test parseQueryHeaders - lib-headers removed",
|
||||||
|
fields: fields{},
|
||||||
|
args: args{
|
||||||
|
queryHeaders: map[string]interface{}{
|
||||||
|
"x-test-header": "test",
|
||||||
|
"gqlcache": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantReturnHeaders: map[string]interface{}{
|
||||||
|
"x-test-header": "test",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
c := NewConnection()
|
||||||
|
if gotReturnHeaders := c.parseQueryHeaders(tt.args.queryHeaders); !reflect.DeepEqual(gotReturnHeaders, tt.wantReturnHeaders) {
|
||||||
|
t.Errorf("BaseClient.parseQueryHeaders() = %v, want %v", gotReturnHeaders, tt.wantReturnHeaders)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBaseClient_QueryCache(t *testing.T) {
|
||||||
|
type fields struct {
|
||||||
|
graphql_endpoint string
|
||||||
|
cache_enabled bool
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
queryVariables interface{}
|
||||||
|
queryHeaders map[string]interface{}
|
||||||
|
queryContent string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
want any
|
||||||
|
args args
|
||||||
|
name string
|
||||||
|
fields fields
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Test QueryCache - enabled",
|
||||||
|
fields: fields{
|
||||||
|
graphql_endpoint: "https://spacex-production.up.railway.app/",
|
||||||
|
cache_enabled: true,
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
queryContent: `query Dragons {
|
||||||
|
dragons {
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
queryHeaders: map[string]interface{}{
|
||||||
|
"x-apollo-operation-name": "Missions-Potato-Test-Golang",
|
||||||
|
"content-type": "application/json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test QueryCache - disabled",
|
||||||
|
fields: fields{
|
||||||
|
graphql_endpoint: "https://spacex-production.up.railway.app/",
|
||||||
|
cache_enabled: false,
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
queryContent: `query Dragons {
|
||||||
|
dragons {
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
queryHeaders: map[string]interface{}{
|
||||||
|
"x-apollo-operation-name": "Missions-Potato-Test-Golang",
|
||||||
|
"content-type": "application/json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
c := NewConnection()
|
||||||
|
c.cache.enabled = tt.fields.cache_enabled
|
||||||
|
|
||||||
|
if tt.fields.graphql_endpoint != "" {
|
||||||
|
c.endpoint = tt.fields.graphql_endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := c.Query(tt.args.queryContent, tt.args.queryVariables, tt.args.queryHeaders)
|
||||||
|
if !tt.wantErr {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
} else {
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+9
-1
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/avast/retry-go/v4"
|
"github.com/avast/retry-go/v4"
|
||||||
)
|
)
|
||||||
@@ -18,7 +19,7 @@ func (c *BaseClient) executeQuery(query []byte, headers any) (result any, err er
|
|||||||
}
|
}
|
||||||
|
|
||||||
for key, value := range headers.(map[string]interface{}) {
|
for key, value := range headers.(map[string]interface{}) {
|
||||||
httpRequest.Header.Add(key, value.(string))
|
httpRequest.Header.Add(key, fmt.Sprintf("%s", value))
|
||||||
}
|
}
|
||||||
|
|
||||||
var httpResponse *http.Response
|
var httpResponse *http.Response
|
||||||
@@ -50,6 +51,13 @@ func (c *BaseClient) executeQuery(query []byte, headers any) (result any, err er
|
|||||||
|
|
||||||
return nil
|
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 {
|
if len(queryResult.Errors) > 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user