/
build.go
95 lines (74 loc) · 1.63 KB
/
build.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
package core
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/dosco/graphjin/core/internal/psql"
"github.com/dosco/graphjin/core/internal/qcode"
)
type stmt struct {
role *Role
qc *qcode.QCode
md psql.Metadata
sql string
}
func (gj *GraphJin) compileQuery(cq *cquery, role string) error {
var err error
// In production mode enforce the allow list and
// compile and cache the result else compile each time
if gj.allowList != nil && gj.prod {
if cq1, ok := gj.queries[(cq.q.name + role)]; ok {
cq.q = cq1.q
} else {
return errNotFound
}
if cq.st.sql == "" {
cq.Do(func() {
err = gj.compileQueryFn(cq, role)
})
}
} else {
err = gj.compileQueryFn(cq, role)
}
return err
}
func (gj *GraphJin) compileQueryFn(cq *cquery, role string) error {
var err error
switch cq.q.op {
case qcode.QTQuery, qcode.QTSubscription, qcode.QTMutation:
err = gj.buildRoleStmt(cq, role)
default:
err = errors.New("unknown query")
}
cq.roleArg = (len(cq.stmts) > 0)
return err
}
func (gj *GraphJin) buildRoleStmt(cq *cquery, role string) error {
query := cq.q.query
vars := cq.q.vars
ro, ok := gj.roles[role]
if !ok {
return fmt.Errorf(`roles '%s' not defined in c.gj.config`, role)
}
var vm map[string]json.RawMessage
var err error
if len(vars) != 0 {
if err := json.Unmarshal(vars, &vm); err != nil {
return fmt.Errorf("variables: %w", err)
}
}
qc, err := gj.qc.Compile(query, vm, ro.Name)
if err != nil {
return err
}
var w bytes.Buffer
cq.st.md, err = gj.pc.Compile(&w, qc)
if err != nil {
return err
}
cq.st.role = ro
cq.st.qc = qc
cq.st.sql = w.String()
return nil
}