Skip to content

Commit c087f5b

Browse files
committed
fix: expand multiple errors for nonnull fields
1 parent d0e93ef commit c087f5b

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Execute(p ExecuteParams) (result *Result) {
5454

5555
defer func() {
5656
if err := recover(); err != nil {
57-
result.Errors = append(result.Errors, gqlerrors.FormatError(err.(error)))
57+
result.Errors = append(result.Errors, gqlerrors.FormatErrorsFromError(err.(error))...)
5858
}
5959
resultChannel <- result
6060
}()

executor_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,6 +2471,49 @@ func TestQuery_NestedJoinedErrors(t *testing.T) {
24712471
}
24722472
}
24732473

2474+
func TestQuery_MultipleErrorsFromResolverNonNull(t *testing.T) {
2475+
queryType := graphql.NewObject(graphql.ObjectConfig{
2476+
Name: "Query",
2477+
Fields: graphql.Fields{
2478+
"value": &graphql.Field{
2479+
Type: graphql.NewNonNull(graphql.Int), // NonNull at top level
2480+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2481+
return nil, errors.Join(
2482+
errors.New("first error"),
2483+
errors.New("second error"),
2484+
)
2485+
},
2486+
},
2487+
},
2488+
})
2489+
2490+
schema, err := graphql.NewSchema(graphql.SchemaConfig{
2491+
Query: queryType,
2492+
})
2493+
if err != nil {
2494+
t.Fatalf("failed to create schema: %v", err)
2495+
}
2496+
2497+
result := graphql.Do(graphql.Params{
2498+
Schema: schema,
2499+
RequestString: `{ value }`,
2500+
})
2501+
2502+
if len(result.Errors) != 2 {
2503+
t.Fatalf("expected 2 errors, got %d: %+v", len(result.Errors), result.Errors)
2504+
}
2505+
2506+
expectedMessages := []string{
2507+
"first error",
2508+
"second error",
2509+
}
2510+
for i, err := range result.Errors {
2511+
if err.Message != expectedMessages[i] {
2512+
t.Errorf("error[%d]: expected message %q, got %q", i, expectedMessages[i], err.Message)
2513+
}
2514+
}
2515+
}
2516+
24742517
type pathError struct {
24752518
msg string
24762519
path []interface{}

0 commit comments

Comments
 (0)