mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-14 00:58:32 +00:00
Bugfixes + ability to set values per query.
This commit is contained in:
@@ -2,6 +2,9 @@ package gql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/lukaszraczylo/go-simple-graphql/utils/helpers"
|
||||
|
||||
@@ -71,6 +74,13 @@ func (c *BaseClient) Query(queryContent string, queryVariables interface{}, quer
|
||||
|
||||
var queryHash string
|
||||
var cachedResponse []byte
|
||||
var cacheBaseClient BaseClient
|
||||
|
||||
// Check for library specific headers
|
||||
if len(queryHeaders) > 0 {
|
||||
cacheBaseClient = reflect.ValueOf(*c).Interface().(BaseClient)
|
||||
queryHeaders = c.parseQueryHeaders(queryHeaders)
|
||||
}
|
||||
|
||||
if c.cache.enabled {
|
||||
queryHash = strutil.Md5(query.compiledQuery)
|
||||
@@ -101,6 +111,10 @@ func (c *BaseClient) Query(queryContent string, queryVariables interface{}, quer
|
||||
c.cache.client.Set(queryHash, jsonData)
|
||||
}
|
||||
|
||||
if len(queryHeaders) > 0 {
|
||||
*c = cacheBaseClient
|
||||
}
|
||||
|
||||
return c.decodeResponse(jsonData), err
|
||||
}
|
||||
|
||||
@@ -115,3 +129,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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user