/
query.go
125 lines (106 loc) · 3.54 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package gql
import (
"fmt"
"sync"
"github.com/goccy/go-json"
"github.com/gookit/goutil"
"github.com/gookit/goutil/strutil"
)
var jsonBufferPool = sync.Pool{
New: func() interface{} {
return new([]byte)
},
}
func (b *BaseClient) convertToJSON(v any) []byte {
jsonBuffer := jsonBufferPool.Get().(*[]byte)
defer jsonBufferPool.Put(jsonBuffer)
*jsonBuffer = (*jsonBuffer)[:0]
jsonData, err := json.Marshal(v)
if err != nil {
b.Logger.Error("Can't convert to JSON", map[string]interface{}{"error": err.Error()})
return nil
}
*jsonBuffer = append(*jsonBuffer, jsonData...)
return *jsonBuffer
}
func (b *BaseClient) compileQuery(queryPartials ...any) *Query {
var query string
var variables map[string]interface{}
for _, partial := range queryPartials {
switch val := partial.(type) {
case string:
query = val
case map[string]interface{}:
variables = val
}
}
if query == "" {
b.Logger.Error("Can't compile query", map[string]interface{}{"error": "query is empty"})
return nil
}
jsonQuery := b.convertToJSON(&Query{Query: query, Variables: variables})
return &Query{
Query: query,
Variables: variables,
JsonQuery: jsonQuery,
}
}
func (b *BaseClient) Query(query string, variables map[string]interface{}, headers map[string]interface{}) (any, error) {
compiledQuery := b.compileQuery(query, variables)
if compiledQuery.JsonQuery == nil {
b.Logger.Error("Can't compile query", map[string]interface{}{"error": "query is empty"})
return nil, fmt.Errorf("can't compile query")
}
b.Logger.Debug("Compiled query", map[string]interface{}{"query": compiledQuery})
enableCache, enableRetries, recompileRequired := compiledQuery.parseHeadersAndVariables(headers)
if recompileRequired {
compiledQuery = b.compileQuery(query, variables)
}
var queryHash string
if (enableCache || b.cache_global) && strutil.HasPrefix(compiledQuery.Query, "query") {
b.Logger.Debug("Cache enabled", nil)
queryHash = calculateHash(compiledQuery)
if cachedValue := b.cacheLookup(queryHash); cachedValue != nil {
b.Logger.Debug("Cache hit", map[string]interface{}{"query": compiledQuery})
return cachedValue, nil
}
b.Logger.Debug("Cache miss", map[string]interface{}{"query": compiledQuery})
}
if enableRetries || b.retries_enable {
b.Logger.Debug("Retries enabled", nil)
}
q := &QueryExecutor{
BaseClient: b,
Query: compiledQuery.JsonQuery,
Headers: headers,
CacheKey: func() string {
if queryHash != "" {
return queryHash
}
return "no-cache"
}(),
Retries: enableRetries || b.retries_enable,
}
rv, err := q.executeQuery()
if err != nil {
b.Logger.Error("Error executing query", map[string]interface{}{"error": err.Error()})
return nil, err
}
return q.decodeResponse(rv)
}
func (q *Query) parseHeadersAndVariables(headers map[string]interface{}) (enableCache, enableRetries, recompileRequired bool) {
enableCache, _ = goutil.ToBool(searchForKeysInMapStringInterface(headers, "gqlcache"))
enableRetries, _ = goutil.ToBool(searchForKeysInMapStringInterface(headers, "gqlretries"))
if q.Variables != nil {
varEnableCache, _ := goutil.ToBool(searchForKeysInMapStringInterface(q.Variables, "gqlcache"))
varEnableRetries, _ := goutil.ToBool(searchForKeysInMapStringInterface(q.Variables, "gqlretries"))
enableCache = enableCache || varEnableCache
enableRetries = enableRetries || varEnableRetries
if varEnableCache || varEnableRetries {
delete(q.Variables, "gqlcache")
delete(q.Variables, "gqlretries")
recompileRequired = true
}
}
return enableCache, enableRetries, recompileRequired
}