-
Notifications
You must be signed in to change notification settings - Fork 12
/
selection_test.go
95 lines (80 loc) · 2.44 KB
/
selection_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
package gqlengine
import (
"context"
"testing"
"github.com/karfield/graphql"
)
type SelectionTestInnerObject struct {
IsGraphQLObject
IntField int
}
type SelectionTestObject struct {
IsGraphQLObject
StringField string
InnerObject *SelectionTestInnerObject
}
type selectionTestFunc func(selection FieldSelection)
func (o *SelectionTestObject) ResolveInnerObject(ctx context.Context, selection FieldSelection) *SelectionTestInnerObject {
test := ctx.Value("innerObject").(selectionTestFunc)
test(selection)
return &SelectionTestInnerObject{}
}
func GetTestObject(ctx context.Context, selection FieldSelection) *SelectionTestObject {
test := ctx.Value("getTestObject").(selectionTestFunc)
test(selection)
return &SelectionTestObject{}
}
func makeSelectionTestContext(funcs map[string]selectionTestFunc) context.Context {
ctx := context.Background()
for k, v := range funcs {
ctx = context.WithValue(ctx, k, v)
}
return ctx
}
func TestFieldSelectionSet(t *testing.T) {
engine := NewEngine(Options{})
engine.NewQuery(GetTestObject)
if err := engine.Init(); err != nil {
t.Fatal(err)
}
ctx := makeSelectionTestContext(map[string]selectionTestFunc{
"getTestObject": func(selection FieldSelection) {
if !selection.IsSelected("stringField") {
t.Error("expected stringField selected")
}
if !selection.IsSelected("innerObject") {
t.Error("expected innerObject selected")
}
if !selection.IsSelected("innerObject/*") {
t.Error("expected any fields of innerObject selected")
}
if !selection.IsSelected("innerObject/intField") {
t.Error("expected intField of innerObject selected")
}
},
"innerObject": func(selection FieldSelection) {
if !selection.IsSelected("intField") {
t.Error("expected intField selected")
}
},
})
_, _ = graphql.Do(graphql.Params{
Schema: engine.schema,
RequestString: `query { getTestObject { stringField, innerObject { intField} } }`,
OperationName: "",
Context: ctx,
})
_, _ = graphql.Do(graphql.Params{
Schema: engine.schema,
RequestString: `query { getTestObject { ... on SelectionTestObject { stringField, innerObject { intField} } } }`,
OperationName: "",
Context: ctx,
})
_, _ = graphql.Do(graphql.Params{
Schema: engine.schema,
RequestString: `query { getTestObject { ... fieldsFragment } }
fragment fieldsFragment on SelectionTestObject { stringField, innerObject { intField} }`,
OperationName: "",
Context: ctx,
})
}