-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
resolve.go
115 lines (92 loc) · 2.19 KB
/
resolve.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
package core
import (
"fmt"
"strings"
"github.com/dosco/graphjin/core/internal/sdata"
)
type refunc func(v ResolverProps) (Resolver, error)
type resItem struct {
IDField []byte
Path [][]byte
Fn Resolver
}
func (gj *GraphJin) initResolvers() error {
gj.rmap = make(map[string]resItem)
rtmap := map[string]refunc{
"remote_api": func(v ResolverProps) (Resolver, error) {
return newRemoteAPI(v)
},
}
for name, fn := range gj.conf.rtmap {
rtmap[name] = fn
}
for i, r := range gj.conf.Resolvers {
if r.Schema == "" {
gj.conf.Resolvers[i].Schema = gj.dbinfo.Schema
r.Schema = gj.dbinfo.Schema
}
if err := gj.initRemote(r, rtmap); err != nil {
return fmt.Errorf("resolvers: %w", err)
}
}
return nil
}
func (gj *GraphJin) initRemote(rc ResolverConfig, rtmap map[string]refunc) error {
// Defines the table column to be used as an id in the
// remote reques
var col sdata.DBColumn
ti, err := gj.dbinfo.GetTable(rc.Schema, rc.Table)
if err != nil {
return err
}
// If no table column specified in the config then
// use the primary key of the table as the id
if rc.Column != "" {
idcol, err := ti.GetColumn(rc.Column)
if err != nil {
return err
}
col = idcol
} else {
col = ti.PrimaryCol
}
idk := fmt.Sprintf("__%s_%s", rc.Name, col.Name)
col1 := sdata.DBColumn{
PrimaryKey: true,
Schema: rc.Schema,
Table: rc.Name,
Name: idk,
Type: col.Type,
FKeySchema: col.Schema,
FKeyTable: col.Table,
FKeyCol: col.Name,
}
nt := sdata.NewDBTable(rc.Schema, rc.Name, "remote", nil)
nt.PrimaryCol = col1
gj.dbinfo.AddTable(nt)
// The function thats called to resolve this remote
// data request
var fn Resolver
if v, ok := rtmap[rc.Type]; ok {
fn, err = v(rc.Props)
} else {
err = fmt.Errorf("unknown resolver type: %s", rc.Type)
}
if err != nil {
return err
}
path := [][]byte{}
for _, p := range strings.Split(rc.StripPath, ".") {
path = append(path, []byte(p))
}
rf := resItem{
IDField: []byte(idk),
Path: path,
Fn: fn,
}
// Index resolver obj by parent and child names
gj.rmap[(rc.Name + rc.Table)] = rf
// Index resolver obj by IDField
gj.rmap[string(rf.IDField)] = rf
return nil
}