Repository navigation
Expand file tree
/
Copy pathintrospection_extended_test.go
More file actions
209 lines (201 loc) · 4.72 KB
/
Copy pathintrospection_extended_test.go
File metadata and controls
209 lines (201 loc) · 4.72 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
package graphql_test
import (
"testing"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
)
func TestIntrospection_SchemaWithMutationAndSubscription(t *testing.T) {
mutationType := graphql.NewObject(graphql.ObjectConfig{
Name: "MutationRoot",
Fields: graphql.Fields{
"doSomething": &graphql.Field{
Type: graphql.String,
},
},
})
subscriptionType := graphql.NewObject(graphql.ObjectConfig{
Name: "SubscriptionRoot",
Fields: graphql.Fields{
"onEvent": &graphql.Field{
Type: graphql.String,
},
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "QueryRoot",
Fields: graphql.Fields{
"f1": &graphql.Field{Type: graphql.String},
},
}),
Mutation: mutationType,
Subscription: subscriptionType,
})
if err != nil {
t.Fatalf("unexpected error creating schema: %v", err)
}
query := `
{
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"__schema": map[string]interface{}{
"queryType": map[string]interface{}{
"name": "QueryRoot",
},
"mutationType": map[string]interface{}{
"name": "MutationRoot",
},
"subscriptionType": map[string]interface{}{
"name": "SubscriptionRoot",
},
},
},
}
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})
if !testutil.EqualResults(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
func TestIntrospection_SchemaWithCustomDirective(t *testing.T) {
customDirective := graphql.NewDirective(graphql.DirectiveConfig{
Name: "customDirective",
Locations: []string{
graphql.DirectiveLocationField,
graphql.DirectiveLocationSubscription,
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "QueryRoot",
Fields: graphql.Fields{
"f1": &graphql.Field{Type: graphql.String},
},
}),
Directives: append(graphql.SpecifiedDirectives, customDirective),
})
if err != nil {
t.Fatalf("unexpected error creating schema: %v", err)
}
query := `
{
__schema {
directives {
name
onOperation
onFragment
onField
}
}
}
`
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})
if result.HasErrors() {
t.Fatalf("unexpected errors: %v", result.Errors)
}
data, ok := result.Data.(map[string]interface{})
if !ok {
t.Fatal("expected data map")
}
schemaData, ok := data["__schema"].(map[string]interface{})
if !ok {
t.Fatal("expected __schema in data")
}
directives, ok := schemaData["directives"].([]interface{})
if !ok {
t.Fatal("expected directives list")
}
found := false
for _, d := range directives {
dir, ok := d.(map[string]interface{})
if !ok {
continue
}
if dir["name"] == "customDirective" {
found = true
onOp, ok := dir["onOperation"].(bool)
if !ok || !onOp {
t.Fatal("expected onOperation=true for directive with SUBSCRIPTION location")
}
break
}
}
if !found {
t.Fatal("expected customDirective in schema directives")
}
}
func TestIntrospection_EnumValuesWithDeprecated(t *testing.T) {
testEnum := graphql.NewEnum(graphql.EnumConfig{
Name: "TestEnum",
Values: graphql.EnumValueConfigMap{
"NONDEPRECATED": &graphql.EnumValueConfig{
Value: 0,
},
"DEPRECATED": &graphql.EnumValueConfig{
Value: 1,
DeprecationReason: "Removed in 2.0",
},
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "QueryRoot",
Fields: graphql.Fields{
"testEnum": &graphql.Field{
Type: testEnum,
},
},
}),
})
if err != nil {
t.Fatalf("unexpected error creating schema: %v", err)
}
query := `
{
__type(name: "TestEnum") {
enumValues(includeDeprecated: true) {
name
isDeprecated
deprecationReason
}
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"__type": map[string]interface{}{
"enumValues": []interface{}{
map[string]interface{}{
"name": "NONDEPRECATED",
"isDeprecated": false,
"deprecationReason": nil,
},
map[string]interface{}{
"name": "DEPRECATED",
"isDeprecated": true,
"deprecationReason": "Removed in 2.0",
},
},
},
},
}
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})
if !testutil.ContainSubset(result.Data.(map[string]interface{}), expected.Data.(map[string]interface{})) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}