-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathnormalization_test.go
More file actions
229 lines (197 loc) · 6.62 KB
/
Copy pathnormalization_test.go
File metadata and controls
229 lines (197 loc) · 6.62 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package graphql
import (
"encoding/json"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/wundergraph/graphql-go-tools/v2/pkg/astprinter"
"github.com/wundergraph/graphql-go-tools/v2/pkg/graphqlerrors"
"github.com/wundergraph/graphql-go-tools/v2/pkg/operationreport"
"github.com/wundergraph/graphql-go-tools/v2/pkg/starwars"
)
func TestRequest_Normalize(t *testing.T) {
t.Parallel()
t.Run("should return error when schema is nil", func(t *testing.T) {
t.Parallel()
request := Request{
OperationName: "Hello",
Variables: nil,
Query: `query Hello { hello }`,
}
result, err := request.Normalize(nil)
assert.Error(t, err)
assert.Equal(t, ErrNilSchema, err)
assert.False(t, result.Successful)
assert.False(t, request.isNormalized)
})
t.Run("should successfully normalize request with fragments", func(t *testing.T) {
t.Parallel()
schema := StarwarsSchema(t)
request := StarwarsRequestForQuery(t, starwars.FileFragmentsQuery)
request.OperationName = "Fragments"
documentBeforeNormalization := request.document
result, err := request.Normalize(schema)
assert.NoError(t, err)
assert.NotEqual(t, documentBeforeNormalization, request.document)
assert.True(t, result.Successful)
assert.True(t, request.isNormalized)
normalizedOperation := `query Fragments($droidID: ID!){
hero {
name
}
droid(id: $droidID){
name
}
}`
op, _ := astprinter.PrintStringIndent(&request.document, " ")
assert.Equal(t, normalizedOperation, op)
})
runNormalizationWithSchema := func(t *testing.T, schema *Schema, request *Request, expectedVars string, expectedNormalizedOperation string) {
t.Helper()
documentBeforeNormalization := request.document
result, err := request.Normalize(schema)
assert.NoError(t, err)
assert.NotEqual(t, documentBeforeNormalization, request.document)
assert.Equal(t, []byte(expectedVars), request.document.Input.Variables)
assert.True(t, result.Successful)
assert.True(t, request.isNormalized)
op, _ := astprinter.PrintStringIndent(&request.document, " ")
assert.Equal(t, expectedNormalizedOperation, op)
}
runNormalization := func(t *testing.T, request *Request, expectedVars string, expectedNormalizedOperation string) {
t.Helper()
schema := StarwarsSchema(t)
runNormalizationWithSchema(t, schema, request, expectedVars, expectedNormalizedOperation)
}
t.Run("should successfully normalize single query with arguments", func(t *testing.T) {
t.Parallel()
request := StarwarsRequestForQuery(t, starwars.FileDroidWithArgQuery)
runNormalization(t, &request, `{"a":"R2D2"}`, `query($a: ID!){
droid(id: $a){
name
}
}`)
})
t.Run("should successfully normalize query and remove unused variables", func(t *testing.T) {
t.Parallel()
request := Request{
OperationName: "MySearch",
Variables: stringify(map[string]any{
"s": "Luke",
"other": "other",
}),
Query: `query MySearch($s: String!, $other: String) {search(name: $s) {...on Human {name}}}`,
}
runNormalization(t, &request, `{"other":"other","s":"Luke"}`, `query MySearch($s: String!, $other: String){
search(name: $s){
... on Human {
name
}
}
}`)
})
t.Run("should successfully normalize query and remove variables with no value provided", func(t *testing.T) {
t.Parallel()
request := Request{
OperationName: "MySearch",
Variables: stringify(map[string]any{
"s": "Luke",
}),
Query: `query MySearch($s: String!, $other: String) {search(name: $s) {...on Human {name}}}`,
}
runNormalization(t, &request, `{"s":"Luke"}`, `query MySearch($s: String!, $other: String){
search(name: $s){
... on Human {
name
}
}
}`)
})
t.Run("should successfully normalize multiple queries with arguments", func(t *testing.T) {
t.Parallel()
request := StarwarsRequestForQuery(t, starwars.FileMultiQueriesWithArguments)
request.OperationName = "GetDroid"
runNormalization(t, &request, `{"a":"1"}`,
`query GetDroid($a: ID!){
droid(id: $a){
name
}
}`)
})
t.Run("input coercion for lists without variables", func(t *testing.T) {
t.Parallel()
schema := InputCoercionForListSchema(t)
request := Request{
OperationName: "charactersByIds",
Variables: stringify(map[string]any{"a": 1}),
Query: `query charactersByIds($a: [Int]) { charactersByIds(ids: $a) { name }}`,
}
runNormalizationWithSchema(t, schema, &request, `{"a":[1]}`, `query charactersByIds($a: [Int]){
charactersByIds(ids: $a){
name
}
}`)
})
t.Run("input coercion for lists with variable extraction", func(t *testing.T) {
t.Parallel()
schema := InputCoercionForListSchema(t)
request := Request{
OperationName: "GetCharactersByIds",
Variables: stringify(map[string]any{}),
Query: `query GetCharactersByIds { charactersByIds(ids: 1) { name }}`,
}
runNormalizationWithSchema(t, schema, &request, `{"a":[1]}`, `query GetCharactersByIds($a: [Int]){
charactersByIds(ids: $a){
name
}
}`)
})
t.Run("input coercion for lists with variables", func(t *testing.T) {
t.Parallel()
schema := InputCoercionForListSchema(t)
request := Request{
OperationName: "charactersByIds",
Variables: stringify(map[string]any{
"ids": 1,
}),
Query: `query charactersByIds($ids: [Int]) {charactersByIds(ids: $ids) { name }}`,
}
runNormalizationWithSchema(t, schema, &request, `{"ids":[1]}`, `query charactersByIds($ids: [Int]){
charactersByIds(ids: $ids){
name
}
}`)
})
}
func Test_normalizationResultFromReport(t *testing.T) {
t.Parallel()
t.Run("should return successful result when report does not have errors", func(t *testing.T) {
t.Parallel()
report := operationreport.Report{}
result, err := NormalizationResultFromReport(report)
assert.NoError(t, err)
assert.Equal(t, NormalizationResult{Successful: true, Errors: nil}, result)
})
t.Run("should return graphql errors and internal error when report contains them", func(t *testing.T) {
t.Parallel()
internalErr := errors.New("errors occurred")
externalErr := operationreport.ExternalError{
Message: "graphql error",
Path: nil,
Locations: nil,
}
report := operationreport.Report{}
report.AddInternalError(internalErr)
report.AddExternalError(externalErr)
result, err := NormalizationResultFromReport(report)
assert.Error(t, err)
assert.Equal(t, internalErr, err)
assert.False(t, result.Successful)
assert.Equal(t, result.Errors.Count(), 1)
assert.Equal(t, "graphql error", result.Errors.(graphqlerrors.RequestErrors)[0].Message)
})
}
func stringify(any any) []byte {
out, _ := json.Marshal(any)
return out
}