diff --git a/.circleci/config.yml b/.circleci/config.yml index 608cb893..2dbc68e8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -17,25 +17,17 @@ defaults: &defaults version: 2 jobs: - golang:1.8.7: - <<: *test_without_go_modules - docker: - - image: circleci/golang:1.8.7 - golang:1.9.7: - <<: *test_without_go_modules - docker: - - image: circleci/golang:1.9.7 - golang:1.11: + golang:1.23: <<: *defaults docker: - - image: circleci/golang:1.11 - golang:latest: + - image: cimg/go:1.23 + golang:1.24.1: <<: *defaults docker: - - image: circleci/golang:latest + - image: cimg/go:1.24.1 coveralls: docker: - - image: circleci/golang:latest + - image: circleci/golang:1.23 steps: - checkout - run: go get github.com/mattn/goveralls @@ -46,8 +38,6 @@ workflows: version: 2 build: jobs: - - golang:1.8.7 - - golang:1.9.7 - - golang:1.11 - - golang:latest + - golang:1.23 + - golang:1.24.1 - coveralls diff --git a/graphql_test.go b/graphql_test.go index 8b06a7b1..248acfc9 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -2,10 +2,12 @@ package graphql_test import ( "context" + "errors" "reflect" "testing" "github.com/graphql-go/graphql" + "github.com/graphql-go/graphql/gqlerrors" "github.com/graphql-go/graphql/testutil" ) @@ -268,3 +270,32 @@ func TestEmptyStringIsNotNull(t *testing.T) { t.Errorf("wrong result, query: %v, graphql result diff: %v", query, testutil.Diff(expected, result)) } } + +func TestResultErrorsJoinedFailure(t *testing.T) { + r := graphql.Result{} + + if err := r.ErrorsJoined(); err != nil { + t.Fatalf("wrong result, want: nil, got: %v", err) + } +} + +func TestResultErrorsJoinedSuccess(t *testing.T) { + r := graphql.Result{ + Errors: []gqlerrors.FormattedError{ + {Message: "first error"}, + {Message: "second error"}, + }, + } + + expected := errors.New("second error: first error") + + if err := r.ErrorsJoined(); err != nil { + if errors.Is(err, expected) { + t.Fatalf("wrong result, want: %v, got: %v", expected, err) + } + + return + } + + t.Fatalf("wrong result, got: nil, want: %v", expected) +} diff --git a/types.go b/types.go index 5b991d8c..176dfa67 100644 --- a/types.go +++ b/types.go @@ -1,6 +1,8 @@ package graphql import ( + "fmt" + "github.com/graphql-go/graphql/gqlerrors" ) @@ -17,3 +19,23 @@ type Result struct { func (r *Result) HasErrors() bool { return len(r.Errors) > 0 } + +// ErrorsJoined joins and returns the errors of the result with `:` as character separator. +func (r *Result) ErrorsJoined() error { + if r.Errors == nil { + return nil + } + + var result error + for _, err := range r.Errors { + if result == nil { + result = fmt.Errorf("%w", err) + + continue + } + + result = fmt.Errorf("%w: %w", err, result) + } + + return result +}