-
Notifications
You must be signed in to change notification settings - Fork 4
/
requested_fields_test.go
88 lines (66 loc) · 1.82 KB
/
requested_fields_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
package fields
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
)
var graphql_query_products string = `
query {
search {
products {
edges {
node { id title }
cursor
}
}
}
}
`
type QueryResolver struct {
Field Field `graphql:"query"`
}
type SearchResolver struct {
Field Field `graphql:"search"`
}
type ProductsResolver struct {
Field Field `graphql:"products"`
}
func TestRequestedFieldsForProducts(t *testing.T) {
query_resolver := &QueryResolver{}
search_resolver := &SearchResolver{}
search_resolver.Field.SetParent(query_resolver)
products_resolver := &ProductsResolver{}
products_resolver.Field.SetParent(search_resolver)
ctx := context.WithValue(context.Background(),
ContextKey, BuildTree(graphql_query_products, Variables{}))
expected_fields := []string{"edges"}
requested_fields := RequestedFor(ctx, products_resolver)
assert.Equal(t, expected_fields, requested_fields)
expected_fields = []string{"node", "cursor"}
requested_fields = RequestedForAt(ctx, products_resolver, "edges")
assert.Equal(t, expected_fields, requested_fields)
expected_fields = []string{"id", "title"}
requested_fields = RequestedForAt(ctx, products_resolver, "edges.node")
assert.Equal(t, expected_fields, requested_fields)
}
var graphql_query_user string = `
{
user(id: 3) {
id
name
}
}
`
type UserResolver struct {
Field Field `graphql:"user"`
}
func TestRequestedFieldsForUser(t *testing.T) {
query_resolver := &QueryResolver{}
user_resolver := &UserResolver{}
user_resolver.Field.SetParent(query_resolver)
ctx := context.WithValue(context.Background(),
ContextKey, BuildTree(graphql_query_user, Variables{}))
expected_fields := []string{"id", "name"}
requested_fields := RequestedFor(ctx, user_resolver)
assert.Equal(t, expected_fields, requested_fields)
}