Skip to content

Commit 202b989

Browse files
committed
handler_test: adds FormatErrorFn tests
1 parent 2f5bd43 commit 202b989

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

handler_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler_test
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io/ioutil"
78
"net/http"
@@ -13,6 +14,8 @@ import (
1314
"context"
1415

1516
"github.com/graphql-go/graphql"
17+
"github.com/graphql-go/graphql/gqlerrors"
18+
"github.com/graphql-go/graphql/language/location"
1619
"github.com/graphql-go/graphql/testutil"
1720
"github.com/graphql-go/handler"
1821
)
@@ -210,3 +213,90 @@ func TestHandler_BasicQuery_WithRootObjFn(t *testing.T) {
210213
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
211214
}
212215
}
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

Comments
 (0)