Repository navigation
Expand file tree
/
Copy pathselection_test.go
More file actions
291 lines (263 loc) · 8.9 KB
/
Copy pathselection_test.go
File metadata and controls
291 lines (263 loc) · 8.9 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package graphql_test
import (
"context"
"sync"
"testing"
"github.com/graph-gophers/graphql-go"
)
const selectionTestSchema = `
schema { query: Query }
type Query { customer: Customer }
type Customer { id: ID! name: String items: [Item!]! }
type Item { id: ID! name: String category: Category }
type Category { id: ID! }
`
type selectionRoot struct {
t *testing.T
expectNames []string
expectSorted []string
hasChecks map[string]bool
}
type selectionCustomer struct {
t *testing.T
id, name string
}
func (r *selectionRoot) Customer(ctx context.Context) *selectionCustomer {
if r.expectNames != nil {
names := graphql.SelectedFieldNames(ctx)
if !equalStringSlices(names, r.expectNames) {
r.t.Errorf("SelectedFieldNames = %v, want %v", names, r.expectNames)
}
}
if r.expectSorted != nil {
sorted := graphql.SortedSelectedFieldNames(ctx)
if !equalStringSlices(sorted, r.expectSorted) {
r.t.Errorf("SortedSelectedFieldNames = %v, want %v", sorted, r.expectSorted)
}
}
for n, want := range r.hasChecks {
if got := graphql.HasSelectedField(ctx, n); got != want {
r.t.Errorf("HasSelectedField(%q) = %v, want %v", n, got, want)
}
}
return &selectionCustomer{t: r.t, id: "c1", name: "Alice"}
}
func (h *selectionCustomer) ID() graphql.ID { return graphql.ID(h.id) }
func (h *selectionCustomer) Name(ctx context.Context) *string {
if len(graphql.SelectedFieldNames(ctx)) != 0 {
h.t.Errorf("leaf selections should be empty")
}
if graphql.HasSelectedField(ctx, "anything") {
h.t.Errorf("unexpected leaf HasSelectedField true")
}
if len(graphql.SortedSelectedFieldNames(ctx)) != 0 {
h.t.Errorf("leaf sorted selections should be empty")
}
return &h.name
}
// nested types for extended schema
type selectionItem struct {
id, name string
category *selectionCategory
}
type selectionCategory struct{ id string }
func (h *selectionCustomer) Items() []*selectionItem {
return []*selectionItem{{id: "i1", name: "Item", category: &selectionCategory{id: "cat1"}}}
}
func (p *selectionItem) ID() graphql.ID { return graphql.ID(p.id) }
func (p *selectionItem) Name() *string { return &p.name }
func (p *selectionItem) Category() *selectionCategory { return p.category }
func (c *selectionCategory) ID() graphql.ID { return graphql.ID(c.id) }
func TestFieldSelectionHelpers(t *testing.T) {
tests := []struct {
name string
schemaOpts []graphql.SchemaOpt
query string
expectNames []string
expectSorted []string
hasChecks map[string]bool
}{
{
name: "enabled order",
query: `query { customer { name id } }`,
expectNames: []string{"name", "id"},
expectSorted: []string{"id", "name"},
hasChecks: map[string]bool{"id": true, "name": true},
},
{
name: "one field",
query: `query { customer { id } }`,
expectNames: []string{"id"},
expectSorted: []string{"id"},
hasChecks: map[string]bool{"id": true, "name": false},
},
{
name: "nested paths",
query: `query { customer { items { id name category { id } } id } }`,
expectNames: []string{"items", "items.id", "items.name", "items.category", "items.category.id", "id"},
expectSorted: []string{"id", "items", "items.category", "items.category.id", "items.id", "items.name"},
hasChecks: map[string]bool{"items": true, "items.id": true, "items.name": true, "items.category": true, "items.category.id": true, "id": true},
},
{
name: "disabled",
schemaOpts: []graphql.SchemaOpt{graphql.DisableFieldSelections()},
query: `query { customer { name id } }`,
expectNames: []string{},
expectSorted: []string{},
hasChecks: map[string]bool{"id": false, "name": false},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := &selectionRoot{t: t, expectNames: tt.expectNames, expectSorted: tt.expectSorted, hasChecks: tt.hasChecks}
s := graphql.MustParseSchema(selectionTestSchema, root, tt.schemaOpts...)
resp := s.Exec(context.Background(), tt.query, "", nil)
if len(resp.Errors) > 0 {
t.Fatalf("execution errors: %v", resp.Errors)
}
})
}
}
type selectionTraceCtxKey struct{}
type selectionTraceCtxRoot struct{ t *testing.T }
func (r *selectionTraceCtxRoot) Customer(ctx context.Context) *selectionCustomer {
if got, _ := ctx.Value(selectionTraceCtxKey{}).(string); got != "trace-field-context" {
r.t.Errorf("missing trace field context value: %q", got)
}
return &selectionCustomer{t: r.t, id: "c1", name: "Alice"}
}
func TestTraceFieldContextPassedToResolver(t *testing.T) {
t.Parallel()
tr := &testTracer{mu: &sync.Mutex{}, fieldContextHook: func(ctx context.Context, fieldName string) context.Context {
if fieldName == "customer" {
return context.WithValue(ctx, selectionTraceCtxKey{}, "trace-field-context")
}
return ctx
}}
s := graphql.MustParseSchema(selectionTestSchema, &selectionTraceCtxRoot{t: t}, graphql.Tracer(tr))
resp := s.Exec(context.Background(), `query { customer { id } }`, "", nil)
if len(resp.Errors) > 0 {
t.Fatalf("execution errors: %v", resp.Errors)
}
}
type traceCtxFieldKey struct{}
// traceCtxRoot records the trace field context value each resolver receives.
type traceCtxRoot struct {
mu sync.Mutex
seen map[string]string
}
func (r *traceCtxRoot) record(ctx context.Context, field string) {
got, _ := ctx.Value(traceCtxFieldKey{}).(string)
r.mu.Lock()
defer r.mu.Unlock()
r.seen[field] = got
}
func (r *traceCtxRoot) Leaf(ctx context.Context) string {
r.record(ctx, "Query.leaf")
return ""
}
func (r *traceCtxRoot) Node(ctx context.Context) *traceCtxNode {
r.record(ctx, "Query.node")
return &traceCtxNode{r: r}
}
type traceCtxNode struct{ r *traceCtxRoot }
func (n *traceCtxNode) Leaf(ctx context.Context) string {
n.r.record(ctx, "Node.leaf")
return ""
}
func TestTraceFieldContextPassedToEveryResolver(t *testing.T) {
t.Parallel()
const schema = `
schema { query: Query }
type Query { leaf: String! node: Node! }
type Node { leaf: String! }
`
tests := []struct {
name string
opts []graphql.SchemaOpt
}{
{name: "field selections enabled"},
{name: "field selections disabled", opts: []graphql.SchemaOpt{graphql.DisableFieldSelections()}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
tr := &testTracer{mu: &sync.Mutex{}}
tr.fieldContextHook = func(ctx context.Context, fieldName string) context.Context {
// The hook only receives the field name, and "leaf" exists on
// both types; a nested field is traced under its parent's
// context, which carries "Query.node".
prefix := "Query."
if parent, _ := ctx.Value(traceCtxFieldKey{}).(string); parent == "Query.node" {
prefix = "Node."
}
return context.WithValue(ctx, traceCtxFieldKey{}, prefix+fieldName)
}
root := &traceCtxRoot{seen: map[string]string{}}
opts := append([]graphql.SchemaOpt{graphql.Tracer(tr)}, tt.opts...)
s := graphql.MustParseSchema(schema, root, opts...)
resp := s.Exec(context.Background(), `query { leaf node { leaf } }`, "", nil)
if len(resp.Errors) > 0 {
t.Fatalf("execution errors: %v", resp.Errors)
}
for _, field := range []string{"Query.leaf", "Query.node", "Node.leaf"} {
if got := root.seen[field]; got != field {
t.Errorf("%s resolver received trace field context %q, want %q", field, got, field)
}
}
})
}
}
func TestSelectedFieldNames_FragmentsAliasesMeta(t *testing.T) {
tests := []struct {
name, query string
expectNames []string
hasChecks map[string]bool
}{
{
name: "alias ignored",
query: `query { customer { idAlias: id name } }`,
expectNames: []string{"id", "name"},
hasChecks: map[string]bool{"id": true, "idAlias": false, "name": true},
},
{
name: "fragment spread",
query: `fragment CFields on Customer { id name } query { customer { ...CFields } }`,
expectNames: []string{"id", "name"},
hasChecks: map[string]bool{"id": true, "name": true},
},
{
name: "inline fragment",
query: `query { customer { id ... on Customer { id name } } }`,
expectNames: []string{"id", "name"},
hasChecks: map[string]bool{"id": true, "name": true},
},
{
name: "meta excluded",
query: `query { customer { id __typename name } }`,
expectNames: []string{"id", "name"},
hasChecks: map[string]bool{"id": true, "name": true, "__typename": false},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := &selectionRoot{t: t, expectNames: tt.expectNames, expectSorted: tt.expectNames, hasChecks: tt.hasChecks}
s := graphql.MustParseSchema(selectionTestSchema, root)
resp := s.Exec(context.Background(), tt.query, "", nil)
if len(resp.Errors) > 0 {
t.Fatalf("execution errors: %v", resp.Errors)
}
})
}
}
func equalStringSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}