-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
resolve.go
159 lines (126 loc) · 3.1 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package core
import (
"fmt"
"hash/maphash"
"io/ioutil"
"net/http"
"strings"
"github.com/dosco/super-graph/core/internal/sdata"
"github.com/dosco/super-graph/jsn"
)
type resolvFn struct {
IDField []byte
Path [][]byte
Fn func(h http.Header, id []byte) ([]byte, error)
}
func (sg *SuperGraph) initResolvers() error {
var err error
sg.rmap = make(map[uint64]resolvFn)
for _, t := range sg.conf.Tables {
if err = sg.initRemotes(t); err != nil {
return fmt.Errorf("resolvers: %w", err)
}
}
return nil
}
func (sg *SuperGraph) initRemotes(t Table) error {
h := maphash.Hash{}
h.SetSeed(sg.hashSeed)
for _, r := range t.Remotes {
// Defines the table column to be used as an id in the
// remote reques
var col sdata.DBColumn
ti, err := sg.schema.GetTableInfo(t.Name)
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 r.ID != "" {
idcol, err := ti.GetColumn(r.ID)
if err != nil {
return err
}
col = idcol
} else {
col = ti.PrimaryCol
}
idk := fmt.Sprintf("__%s_%s", t.Name, col.Name)
// Register a relationship between the remote data
// and the database table
val := sdata.DBRel{Type: sdata.RelRemote}
val.Left.Col = col
val.Right.VTable = idk
if err := sg.schema.SetRel(r.Name, t.Name, val); err != nil {
return err
}
// The function thats called to resolve this remote
// data request
fn := buildFn(r)
path := [][]byte{}
for _, p := range strings.Split(r.Path, ".") {
path = append(path, []byte(p))
}
rf := resolvFn{
IDField: []byte(idk),
Path: path,
Fn: fn,
}
// Index resolver obj by parent and child names
sg.rmap[mkkey(&h, r.Name, t.Name)] = rf
// Index resolver obj by IDField
_, _ = h.Write(rf.IDField)
sg.rmap[h.Sum64()] = rf
}
return nil
}
func buildFn(r Remote) func(http.Header, []byte) ([]byte, error) {
reqURL := strings.Replace(r.URL, "$id", "%s", 1)
client := &http.Client{}
fn := func(hdr http.Header, id []byte) ([]byte, error) {
uri := fmt.Sprintf(reqURL, id)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
if host, ok := hdr["Host"]; ok {
req.Host = host[0]
}
for _, v := range r.SetHeaders {
req.Header.Set(v.Name, v.Value)
}
for _, v := range r.PassHeaders {
req.Header.Set(v, hdr.Get(v))
}
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to connect to '%s': %v", uri, err)
}
defer res.Body.Close()
// if r.Debug {
// reqDump, err := httputil.DumpRequestOut(req, true)
// if err != nil {
// return nil, err
// }
// resDump, err := httputil.DumpResponse(res, true)
// if err != nil {
// return nil, err
// }
// logger.Debug().Msgf("Remote Request Debug:\n%s\n%s",
// reqDump, resDump)
// }
if res.StatusCode != 200 {
return nil,
fmt.Errorf("server responded with a %d", res.StatusCode)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if err := jsn.ValidateBytes(b); err != nil {
return nil, err
}
return b, nil
}
return fn
}