forked from graphql-go/relay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection_test.go
134 lines (126 loc) · 3.36 KB
/
connection_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
package relay_test
import (
"github.com/authgear/graphql-go-relay"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
"reflect"
"testing"
)
var connectionTestAllUsers = []interface{}{
&user{Name: "Dan"},
&user{Name: "Nick"},
&user{Name: "Lee"},
&user{Name: "Joe"},
&user{Name: "Tim"},
}
var connectionTestUserType *graphql.Object
var connectionTestQueryType *graphql.Object
var connectionTestSchema graphql.Schema
var connectionTestConnectionDef *relay.GraphQLConnectionDefinitions
func init() {
connectionTestUserType = graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
},
// re-define `friends` field later because `connectionTestUserType` has `connectionTestConnectionDef` has `connectionTestUserType` (cyclic-reference)
"friends": &graphql.Field{},
},
})
connectionTestConnectionDef = relay.ConnectionDefinitions(relay.ConnectionConfig{
Name: "Friend",
NodeType: connectionTestUserType,
EdgeFields: graphql.Fields{
"friendshipTime": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return "Yesterday", nil
},
},
},
ConnectionFields: graphql.Fields{
"totalCount": &graphql.Field{
Type: graphql.Int,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return len(connectionTestAllUsers), nil
},
},
},
})
// define `friends` field here after getting connection definition
connectionTestUserType.AddFieldConfig("friends", &graphql.Field{
Type: connectionTestConnectionDef.ConnectionType,
Args: relay.ConnectionArgs,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
arg := relay.NewConnectionArguments(p.Args)
res := relay.ConnectionFromArray(connectionTestAllUsers, arg)
return res, nil
},
})
connectionTestQueryType = graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"user": &graphql.Field{
Type: connectionTestUserType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return connectionTestAllUsers[0], nil
},
},
},
})
var err error
connectionTestSchema, err = graphql.NewSchema(graphql.SchemaConfig{
Query: connectionTestQueryType,
})
if err != nil {
panic(err)
}
}
func TestConnectionDefinition_IncludesConnectionAndEdgeFields(t *testing.T) {
query := `
query FriendsQuery {
user {
friends(first: 2) {
totalCount
edges {
friendshipTime
node {
name
}
}
}
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"user": map[string]interface{}{
"friends": map[string]interface{}{
"totalCount": 5,
"edges": []interface{}{
map[string]interface{}{
"friendshipTime": "Yesterday",
"node": map[string]interface{}{
"name": "Dan",
},
},
map[string]interface{}{
"friendshipTime": "Yesterday",
"node": map[string]interface{}{
"name": "Nick",
},
},
},
},
},
},
}
result := graphql.Do(graphql.Params{
Schema: connectionTestSchema,
RequestString: query,
})
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}