forked from samsarahq/thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_syncer_test.go
200 lines (171 loc) · 5.05 KB
/
schema_syncer_test.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package federation
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"os"
"testing"
"time"
"github.com/denkhaus/thunder/graphql/introspection"
"github.com/denkhaus/thunder/graphql/schemabuilder"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
type FileSchemaSyncer struct {
services []string
add chan string // new URL channel
ticker *time.Ticker
currentSchema []byte
}
func newFileSchemaSyncer(ctx context.Context, services []string) *FileSchemaSyncer {
ss := &FileSchemaSyncer{
services: services,
ticker: time.NewTicker(time.Second * 1),
add: make(chan string),
}
return ss
}
func (s *FileSchemaSyncer) FetchPlanner(ctx context.Context, optionalArgs interface{}) (*Planner, error) {
schemas := make(map[string]*introspectionQueryResult)
for _, server := range s.services {
schema, err := readFile(server)
if err != nil {
return nil, errors.Wrapf(err, "error reading file for server %s", server)
}
var iq introspectionQueryResult
if err := json.Unmarshal(schema, &iq); err != nil {
return nil, errors.Wrapf(err, "unmarshaling schema %s", server)
}
schemas[server] = &iq
}
types, err := convertSchema(schemas)
if err != nil {
return nil, errors.Wrapf(err, "converting schemas error")
}
introspectionSchema := introspection.BareIntrospectionSchema(types.Schema)
schema, err := introspection.RunIntrospectionQuery(introspection.BareIntrospectionSchema(introspectionSchema))
if err != nil || schema == nil {
return nil, errors.Wrapf(err, "error running introspection query")
}
var iq introspectionQueryResult
if err := json.Unmarshal(schema, &iq); err != nil {
return nil, errors.Wrapf(err, "unmarshaling introspection schema")
}
schemas["introspection"] = &iq
types, err = convertSchema(schemas)
if err != nil {
return nil, errors.Wrapf(err, "converting schemas error")
}
return NewPlanner(types)
}
// WriteToFile will print any string of text to a file safely by
// checking for errors and syncing at the end.
func WriteToFile(filename string, data string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
_, err = io.WriteString(file, data)
if err != nil {
return err
}
return file.Sync()
}
func writeSchemaToFile(name string, data []byte) error {
fileName := "./testdata/fileschemasyncer" + name
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()
_, err = io.WriteString(file, string(data))
if err != nil {
return err
}
return file.Sync()
}
func readFile(name string) ([]byte, error) {
fileName := "./testdata/fileschemasyncer" + name
return ioutil.ReadFile(fileName)
}
func TestExecutorQueriesWithCustomSchemaSyncer(t *testing.T) {
s1 := buildTestSchema1()
s2 := buildTestSchema2()
ctx := context.Background()
execs, err := makeExecutors(map[string]*schemabuilder.Schema{
"s1": s1,
"s2": s2,
})
require.NoError(t, err)
// Write the schemas to a file
services := []string{"s1", "s2"}
for _, service := range services {
schema, err := fetchSchema(ctx, execs[service], nil)
require.NoError(t, err)
err = writeSchemaToFile(service, schema.Result)
require.NoError(t, err)
}
// Creata file schema syncer that reads the schemas from the
// written files and listens to updates if those change
schemaSyncer := newFileSchemaSyncer(ctx, services)
e, err := NewExecutor(ctx, execs, &CustomExecutorArgs{
SchemaSyncer: schemaSyncer,
SchemaSyncIntervalSeconds: func(ctx context.Context) int64 { return 5 },
})
require.NoError(t, err)
query := `query Foo {
s2root
s1fff {
s1hmm
}
}`
expectedOutput := `{
"s2root": "hello",
"s1fff":[
{
"s1hmm":"jimbo!!!"
},
{
"s1hmm":"bob!!!"
}
]
}`
// Run a federated query and ensure that it works
runAndValidateQueryResults(t, ctx, e, query, expectedOutput)
time.Sleep(5 * time.Second)
// Add a new field to schema2
s2.Query().FieldFunc("s2root2", func() string {
return "hello"
})
newExecs, err := makeExecutors(map[string]*schemabuilder.Schema{
"s1": s1,
"s2": s2,
})
require.NoError(t, err)
// We need to do this to udpate the executor in our test
// But when run locally it should already know about the new
// field when the new service starts
e.Executors = newExecs
query2 := `query Foo {
s2root2
}`
expectedOutput2 := `{
"s2root2":"hello"
}`
// Since we havent written the new schema to the file, the merged schema and planner
// dont know about the new field. We should see an error
runAndValidateQueryError(t, ctx, e, query2, expectedOutput2, "unknown field s2root2 on typ Query")
// Writes the new schemas to the file
for _, service := range services {
schema, err := fetchSchema(ctx, newExecs[service], nil)
require.NoError(t, err)
err = writeSchemaToFile(service, schema.Result)
require.NoError(t, err)
}
// Sleep for 5 seconds to wait for the schema syncer to get the update
time.Sleep(5 * time.Second)
// Run the same query and validate that it works
runAndValidateQueryResults(t, ctx, e, query2, expectedOutput2)
}