forked from graphql-go/relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_global_test.go
182 lines (171 loc) · 4.45 KB
/
node_global_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
package relay_test
import (
"context"
"errors"
"fmt"
"github.com/housinganywhere/graphql"
"github.com/housinganywhere/graphql/testutil"
"github.com/housinganywhere/relay"
"reflect"
"testing"
)
type photo2 struct {
PhotoId int `json:"photoId"`
Width int `json:"width"`
}
var globalIDTestUserData = map[string]*user{
"1": &user{1, "John Doe"},
"2": &user{2, "Jane Smith"},
}
var globalIDTestPhotoData = map[string]*photo2{
"1": &photo2{1, 300},
"2": &photo2{2, 400},
}
// declare types first, define later in init()
// because they all depend on nodeTestDef
var globalIDTestUserType *graphql.Object
var globalIDTestPhotoType *graphql.Object
var globalIDTestDef = relay.NewNodeDefinitions(relay.NodeDefinitionsConfig{
IDFetcher: func(globalID string, info graphql.ResolveInfo, ctx context.Context) (interface{}, error) {
resolvedGlobalID := relay.FromGlobalID(globalID)
if resolvedGlobalID == nil {
return nil, errors.New("Unknown node id")
}
switch resolvedGlobalID.Type {
case "User":
return globalIDTestUserData[resolvedGlobalID.ID], nil
case "Photo":
return globalIDTestPhotoData[resolvedGlobalID.ID], nil
default:
return nil, errors.New("Unknown node type")
}
},
TypeResolve: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
switch value.(type) {
case *user:
return globalIDTestUserType
case *photo2:
return globalIDTestPhotoType
default:
panic(fmt.Sprintf("Unknown object type `%v`", value))
}
},
})
var globalIDTestQueryType = graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"node": globalIDTestDef.NodeField,
"allObjects": &graphql.Field{
Type: graphql.NewList(globalIDTestDef.NodeInterface),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return []interface{}{
globalIDTestUserData["1"],
globalIDTestUserData["2"],
globalIDTestPhotoData["1"],
globalIDTestPhotoData["2"],
}, nil
},
},
},
})
// becareful not to define schema here, since globalIDTestUserType and globalIDTestPhotoType wouldn't be defined till init()
var globalIDTestSchema graphql.Schema
func init() {
globalIDTestUserType = graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"id": relay.GlobalIDField("User", nil),
"name": &graphql.Field{
Type: graphql.String,
},
},
Interfaces: []*graphql.Interface{globalIDTestDef.NodeInterface},
})
photoIDFetcher := func(obj interface{}, info graphql.ResolveInfo, ctx context.Context) (string, error) {
switch obj := obj.(type) {
case *photo2:
return fmt.Sprintf("%v", obj.PhotoId), nil
}
return "", errors.New("Not a photo")
}
globalIDTestPhotoType = graphql.NewObject(graphql.ObjectConfig{
Name: "Photo",
Fields: graphql.Fields{
"id": relay.GlobalIDField("Photo", photoIDFetcher),
"width": &graphql.Field{
Type: graphql.Int,
},
},
Interfaces: []*graphql.Interface{globalIDTestDef.NodeInterface},
})
globalIDTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: globalIDTestQueryType,
})
}
func TestGlobalIDFields_GivesDifferentIDs(t *testing.T) {
query := `{
allObjects {
id
}
}`
expected := &graphql.Result{
Data: map[string]interface{}{
"allObjects": []interface{}{
map[string]interface{}{
"id": "VXNlcjox",
},
map[string]interface{}{
"id": "VXNlcjoy",
},
map[string]interface{}{
"id": "UGhvdG86MQ==",
},
map[string]interface{}{
"id": "UGhvdG86Mg==",
},
},
},
}
result := graphql.Do(graphql.Params{
Schema: globalIDTestSchema,
RequestString: query,
})
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}
func TestGlobalIDFields_RefetchesTheIDs(t *testing.T) {
query := `{
user: node(id: "VXNlcjox") {
id
... on User {
name
}
},
photo: node(id: "UGhvdG86MQ==") {
id
... on Photo {
width
}
}
}`
expected := &graphql.Result{
Data: map[string]interface{}{
"user": map[string]interface{}{
"id": "VXNlcjox",
"name": "John Doe",
},
"photo": map[string]interface{}{
"id": "UGhvdG86MQ==",
"width": 300,
},
},
}
result := graphql.Do(graphql.Params{
Schema: globalIDTestSchema,
RequestString: query,
})
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}