mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-16 01:07:57 +00:00
Fix concurrency issue.
This commit is contained in:
@@ -70,51 +70,54 @@ 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
|
|
||||||
|
cbc := reflect.ValueOf(*c).Interface().(BaseClient)
|
||||||
|
cacheBaseClient := &cbc
|
||||||
|
|
||||||
|
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 := c.parseQueryHeaders(queryHeaders)
|
||||||
queryHeaders = c.parseQueryHeaders(queryHeaders)
|
// compare if there are any changes
|
||||||
defer func() {
|
|
||||||
*c = cacheBaseClient
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.cache.enabled {
|
if !reflect.DeepEqual(queryHeadersModified, queryHeaders) {
|
||||||
queryHash = strutil.Md5(fmt.Sprintf("%s-%+v", query.compiledQuery, queryHeaders))
|
cacheBaseClient.Logger.Debug(cacheBaseClient, "Headers modified, creating a new client")
|
||||||
c.Logger.Debug(c, "Hash calculated;", "hash:", queryHash)
|
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
|||||||
@@ -339,3 +339,77 @@ func TestBaseClient_parseQueryHeaders(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user