mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-13 00:48:37 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dfb082ad51
|
||
|
|
ce736958c6
|
||
|
|
4472041b21
|
||
|
|
b81e612f51
|
||
|
|
3c508eaf15
|
||
|
|
4d0c04cb26
|
||
|
|
55182f2f3d
|
||
|
|
841c136e71
|
||
|
|
833517f35c
|
||
|
|
07e4f75a0a
|
||
|
|
b8044c5d67
|
||
|
|
cf3076d701
|
||
|
|
5a222aa316
|
||
|
|
d843238d46
|
@@ -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)
|
||||||
@@ -40,6 +41,11 @@ Therefore, I present you the simple client to which you can copy & paste your gr
|
|||||||
* `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:
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/allegro/bigcache"
|
||||||
"github.com/lukaszraczylo/go-simple-graphql/utils/concurrency"
|
"github.com/lukaszraczylo/go-simple-graphql/utils/concurrency"
|
||||||
"github.com/lukaszraczylo/go-simple-graphql/utils/logger"
|
"github.com/lukaszraczylo/go-simple-graphql/utils/logger"
|
||||||
|
|
||||||
@@ -14,7 +15,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/allegro/bigcache"
|
|
||||||
"github.com/gookit/goutil/envutil"
|
"github.com/gookit/goutil/envutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,11 +43,7 @@ func NewConnection() *BaseClient {
|
|||||||
b.retries.delay = envutil.GetInt("GRAPHQL_RETRIES_DELAY", 300)
|
b.retries.delay = envutil.GetInt("GRAPHQL_RETRIES_DELAY", 300)
|
||||||
|
|
||||||
if b.cache.enabled {
|
if b.cache.enabled {
|
||||||
var err error
|
b.enableCache()
|
||||||
b.cache.client, err = bigcache.NewBigCache(bigcache.DefaultConfig(time.Duration(b.cache.ttl) * time.Second))
|
|
||||||
if err != nil {
|
|
||||||
b.Logger.Error(b, "Error while creating cache client;", "error", err.Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
b_tmp_log_level := envutil.Getenv("LOG_LEVEL", "info")
|
b_tmp_log_level := envutil.Getenv("LOG_LEVEL", "info")
|
||||||
@@ -102,3 +98,11 @@ func (b *BaseClient) SetEndpoint(endpoint string) {
|
|||||||
func (b *BaseClient) SetOutput(output string) {
|
func (b *BaseClient) SetOutput(output string) {
|
||||||
b.responseType = output
|
b.responseType = output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BaseClient) enableCache() {
|
||||||
|
var err error
|
||||||
|
b.cache.client, err = bigcache.NewBigCache(bigcache.DefaultConfig(time.Duration(b.cache.ttl) * time.Second))
|
||||||
|
if err != nil {
|
||||||
|
b.Logger.Error(b, "Error while creating cache client;", "error", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -70,52 +70,65 @@ 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
|
||||||
var cacheBaseClient BaseClient
|
|
||||||
|
cacheBaseClient := c
|
||||||
|
defer func() {
|
||||||
|
cacheBaseClient = nil
|
||||||
|
}()
|
||||||
|
|
||||||
|
query := cacheBaseClient.NewQuery(queryContent, queryVariables)
|
||||||
|
|
||||||
// Check for library specific headers
|
// Check for library specific headers
|
||||||
if len(queryHeaders) > 0 {
|
if len(queryHeaders) > 0 {
|
||||||
cacheBaseClient = reflect.ValueOf(*c).Interface().(BaseClient)
|
queryHeadersModified, cache_enabled := cacheBaseClient.parseQueryHeaders(queryHeaders)
|
||||||
queryHeaders = c.parseQueryHeaders(queryHeaders)
|
// compare if there are any changes
|
||||||
}
|
|
||||||
|
|
||||||
if c.cache.enabled {
|
if !reflect.DeepEqual(queryHeadersModified, queryHeaders) {
|
||||||
queryHash = strutil.Md5(query.compiledQuery)
|
if queryHeadersModified["gqlcache"] != nil && c.cache.enabled != cache_enabled {
|
||||||
c.Logger.Debug(c, "Hash calculated;", "hash:", queryHash)
|
cacheBaseClient.Logger.Debug(cacheBaseClient, "Switching cache on as per single-request header")
|
||||||
|
cbc := reflect.ValueOf(*c).Interface().(BaseClient)
|
||||||
cachedResponse = c.cacheLookup(queryHash)
|
cacheBaseClient := &cbc
|
||||||
if cachedResponse != nil {
|
cacheBaseClient.enableCache()
|
||||||
c.Logger.Debug(c, "Found cached response")
|
}
|
||||||
return c.decodeResponse(cachedResponse), nil
|
|
||||||
} else {
|
|
||||||
c.Logger.Debug(c, "No cached response found")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = c.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 && jsonData != nil && queryHash != "" {
|
||||||
c.cache.client.Set(queryHash, jsonData)
|
c.cache.client.Set(queryHash, jsonData)
|
||||||
|
} else if cacheBaseClient.cache.enabled && jsonData == nil {
|
||||||
|
cacheBaseClient.Logger.Warn(cacheBaseClient, "Response is empty")
|
||||||
|
} else if cacheBaseClient.cache.enabled && queryHash == "" {
|
||||||
|
cacheBaseClient.Logger.Warn(cacheBaseClient, "Query hash is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(queryHeaders) > 0 {
|
return cacheBaseClient.decodeResponse(jsonData), err
|
||||||
*c = cacheBaseClient
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.decodeResponse(jsonData), err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *BaseClient) decodeResponse(jsonData []byte) any {
|
func (c *BaseClient) decodeResponse(jsonData []byte) any {
|
||||||
@@ -138,11 +151,12 @@ func (c *BaseClient) decodeResponse(jsonData []byte) any {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *BaseClient) parseQueryHeaders(queryHeaders map[string]interface{}) (returnHeaders map[string]interface{}) {
|
func (c *BaseClient) parseQueryHeaders(queryHeaders map[string]interface{}) (returnHeaders map[string]interface{}, cache_enabled bool) {
|
||||||
returnHeaders = make(map[string]interface{})
|
returnHeaders = make(map[string]interface{})
|
||||||
for k, v := range queryHeaders {
|
for k, v := range queryHeaders {
|
||||||
if k == "gqlcache" {
|
if k == "gqlcache" {
|
||||||
c.cache.enabled, _ = strconv.ParseBool(fmt.Sprintf("%v", v))
|
c.cache.enabled, _ = strconv.ParseBool(fmt.Sprintf("%v", v))
|
||||||
|
cache_enabled = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if k == "gqlretries" {
|
if k == "gqlretries" {
|
||||||
@@ -151,5 +165,5 @@ func (c *BaseClient) parseQueryHeaders(queryHeaders map[string]interface{}) (ret
|
|||||||
}
|
}
|
||||||
returnHeaders[k] = v
|
returnHeaders[k] = v
|
||||||
}
|
}
|
||||||
return returnHeaders
|
return returnHeaders, cache_enabled
|
||||||
}
|
}
|
||||||
|
|||||||
+192
-1
@@ -1,6 +1,7 @@
|
|||||||
package gql
|
package gql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -333,9 +334,199 @@ func TestBaseClient_parseQueryHeaders(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
c := NewConnection()
|
c := NewConnection()
|
||||||
if gotReturnHeaders := c.parseQueryHeaders(tt.args.queryHeaders); !reflect.DeepEqual(gotReturnHeaders, tt.wantReturnHeaders) {
|
if gotReturnHeaders, _ := c.parseQueryHeaders(tt.args.queryHeaders); !reflect.DeepEqual(gotReturnHeaders, tt.wantReturnHeaders) {
|
||||||
t.Errorf("BaseClient.parseQueryHeaders() = %v, want %v", 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBaseClient_QueryCacheRandomizedRace(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/",
|
||||||
|
},
|
||||||
|
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()
|
||||||
|
if tt.fields.graphql_endpoint != "" {
|
||||||
|
c.endpoint = tt.fields.graphql_endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c.cache.enabled = rand.Intn(2) == 0
|
||||||
|
go func() {
|
||||||
|
_, err := c.Query(tt.args.queryContent, tt.args.queryVariables, tt.args.queryHeaders)
|
||||||
|
if !tt.wantErr {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
} else {
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBaseClient_QueryCacheRandomizedRaceViaHeader(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/",
|
||||||
|
},
|
||||||
|
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()
|
||||||
|
if tt.fields.graphql_endpoint != "" {
|
||||||
|
c.endpoint = tt.fields.graphql_endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
tt.args.queryHeaders["gqlcache"] = rand.Intn(2) == 0
|
||||||
|
go func() {
|
||||||
|
_, err := c.Query(tt.args.queryContent, tt.args.queryVariables, tt.args.queryHeaders)
|
||||||
|
if !tt.wantErr {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
} else {
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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"
|
||||||
)
|
)
|
||||||
@@ -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