mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-12 00:45:31 +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)
|
||||
- [Usage example](#usage-example)
|
||||
- [Environment variables](#environment-variables)
|
||||
- [Modifiers on the fly](#modifiers-on-the-fly)
|
||||
- [Cache](#cache)
|
||||
- [Example reader code](#example-reader-code)
|
||||
- [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_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`
|
||||
* `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
|
||||
|
||||
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.
|
||||
* 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:
|
||||
|
||||
```go
|
||||
// following values passed as headers will modify behaviour of the query
|
||||
// and disregard settings provided via environment variables
|
||||
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 {
|
||||
b.LoggerWriter = log.New(os.Stdout, "", log.LstdFlags)
|
||||
} else {
|
||||
b.LoggerWriter = b.LoggerWriter
|
||||
}
|
||||
|
||||
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;")
|
||||
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"
|
||||
|
||||
"github.com/lukaszraczylo/go-simple-graphql/utils/logger"
|
||||
|
||||
"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=
|
||||
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/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/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
|
||||
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/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/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/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/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
||||
@@ -2,6 +2,9 @@ package gql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"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
|
||||
|
||||
func (c *BaseClient) Query(queryContent string, queryVariables interface{}, queryHeaders map[string]interface{}) (any, error) {
|
||||
query := c.NewQuery(queryContent, queryVariables)
|
||||
|
||||
var queryHash string
|
||||
var cachedResponse []byte
|
||||
|
||||
if c.cache.enabled {
|
||||
queryHash = strutil.Md5(query.compiledQuery)
|
||||
c.Logger.Debug(c, "Hash calculated;", "hash:", queryHash)
|
||||
cbc := reflect.ValueOf(*c).Interface().(BaseClient)
|
||||
cacheBaseClient := &cbc
|
||||
|
||||
cachedResponse = c.cacheLookup(queryHash)
|
||||
if cachedResponse != nil {
|
||||
c.Logger.Debug(c, "Found cached response")
|
||||
return c.decodeResponse(cachedResponse), nil
|
||||
} else {
|
||||
c.Logger.Debug(c, "No cached response found")
|
||||
query := cacheBaseClient.NewQuery(queryContent, queryVariables)
|
||||
|
||||
// Check for library specific headers
|
||||
if len(queryHeaders) > 0 {
|
||||
queryHeadersModified := c.parseQueryHeaders(queryHeaders)
|
||||
// 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 {
|
||||
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
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(response)
|
||||
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
|
||||
}
|
||||
|
||||
if c.cache.enabled {
|
||||
c.cache.client.Set(queryHash, jsonData)
|
||||
if cacheBaseClient.cache.enabled {
|
||||
cacheBaseClient.cache.client.Set(queryHash, jsonData)
|
||||
}
|
||||
|
||||
return c.decodeResponse(jsonData), err
|
||||
return cacheBaseClient.decodeResponse(jsonData), err
|
||||
}
|
||||
|
||||
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":
|
||||
@@ -115,3 +139,19 @@ func (c *BaseClient) decodeResponse(jsonData []byte) any {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"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{}) {
|
||||
httpRequest.Header.Add(key, value.(string))
|
||||
httpRequest.Header.Add(key, fmt.Sprintf("%s", value))
|
||||
}
|
||||
|
||||
var httpResponse *http.Response
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user