@@ -2,6 +2,7 @@ package handler_test
2
2
3
3
import (
4
4
"encoding/json"
5
+ "errors"
5
6
"fmt"
6
7
"io/ioutil"
7
8
"net/http"
@@ -13,6 +14,8 @@ import (
13
14
"context"
14
15
15
16
"github.com/graphql-go/graphql"
17
+ "github.com/graphql-go/graphql/gqlerrors"
18
+ "github.com/graphql-go/graphql/language/location"
16
19
"github.com/graphql-go/graphql/testutil"
17
20
"github.com/graphql-go/handler"
18
21
)
@@ -210,3 +213,90 @@ func TestHandler_BasicQuery_WithRootObjFn(t *testing.T) {
210
213
t .Fatalf ("wrong result, graphql result diff: %v" , testutil .Diff (expected , result ))
211
214
}
212
215
}
216
+
217
+ type customError struct {
218
+ error
219
+ }
220
+
221
+ func (e customError ) Error () string {
222
+ return e .error .Error ()
223
+ }
224
+
225
+ func TestHandler_BasicQuery_WithFormatErrorFn (t * testing.T ) {
226
+ resolverError := customError {error : errors .New ("resolver error" )}
227
+ myNameQuery := graphql .NewObject (graphql.ObjectConfig {
228
+ Name : "Query" ,
229
+ Fields : graphql.Fields {
230
+ "name" : & graphql.Field {
231
+ Name : "name" ,
232
+ Type : graphql .String ,
233
+ Resolve : func (p graphql.ResolveParams ) (interface {}, error ) {
234
+ return nil , resolverError
235
+ },
236
+ },
237
+ },
238
+ })
239
+ myNameSchema , err := graphql .NewSchema (graphql.SchemaConfig {
240
+ Query : myNameQuery ,
241
+ })
242
+ if err != nil {
243
+ t .Fatal (err )
244
+ }
245
+
246
+ customFormattedError := gqlerrors.FormattedError {
247
+ Message : resolverError .Error (),
248
+ Locations : []location.SourceLocation {
249
+ location.SourceLocation {
250
+ Line : 1 ,
251
+ Column : 2 ,
252
+ },
253
+ },
254
+ Path : []interface {}{"name" },
255
+ Extensions : map [string ]interface {}{
256
+ "fromFormatFn" : "FROM_FORMAT_FN" ,
257
+ },
258
+ }
259
+
260
+ expected := & graphql.Result {
261
+ Data : map [string ]interface {}{
262
+ "name" : nil ,
263
+ },
264
+ Errors : []gqlerrors.FormattedError {customFormattedError },
265
+ }
266
+
267
+ queryString := `query={name}`
268
+ req , _ := http .NewRequest ("GET" , fmt .Sprintf ("/graphql?%v" , queryString ), nil )
269
+
270
+ formatErrorFnCalled := false
271
+ h := handler .New (& handler.Config {
272
+ Schema : & myNameSchema ,
273
+ Pretty : true ,
274
+ FormatErrorFn : func (err gqlerrors.FormattedError ) gqlerrors.FormattedError {
275
+ formatErrorFnCalled = true
276
+ originalError := err .OriginalError ()
277
+ switch errType := originalError .(type ) {
278
+ case customError :
279
+ default :
280
+ t .Fatalf ("unexpected error type: %v" , reflect .TypeOf (errType ))
281
+ }
282
+ return gqlerrors.FormattedError {
283
+ Message : err .Message ,
284
+ Locations : err .Locations ,
285
+ Path : err .Path ,
286
+ Extensions : map [string ]interface {}{
287
+ "fromFormatFn" : "FROM_FORMAT_FN" ,
288
+ },
289
+ }
290
+ },
291
+ })
292
+ result , resp := executeTest (t , h , req )
293
+ if resp .Code != http .StatusOK {
294
+ t .Fatalf ("unexpected server response %v" , resp .Code )
295
+ }
296
+ if ! formatErrorFnCalled {
297
+ t .Fatalf ("FormatErrorFn was not called when it should have been" )
298
+ }
299
+ if ! reflect .DeepEqual (result , expected ) {
300
+ t .Fatalf ("wrong result, graphql result diff: %v" , testutil .Diff (expected , result ))
301
+ }
302
+ }
0 commit comments