File tree Expand file tree Collapse file tree 2 files changed +15
-3
lines changed Expand file tree Collapse file tree 2 files changed +15
-3
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ func Execute(p ExecuteParams) (result *Result) {
36
36
go func (out chan <- * Result , done <- chan struct {}) {
37
37
defer func () {
38
38
if err := recover (); err != nil {
39
- result .Errors = append ( result . Errors , gqlerrors .FormatError (err .(error )))
39
+ result .AppendErrors ( gqlerrors .FormatError (err .(error )))
40
40
}
41
41
select {
42
42
case out <- result :
@@ -54,7 +54,7 @@ func Execute(p ExecuteParams) (result *Result) {
54
54
})
55
55
56
56
if err != nil {
57
- result .Errors = append ( result . Errors , gqlerrors .FormatError (err ))
57
+ result .AppendErrors ( gqlerrors .FormatError (err ))
58
58
return
59
59
}
60
60
@@ -67,7 +67,7 @@ func Execute(p ExecuteParams) (result *Result) {
67
67
68
68
select {
69
69
case <- ctx .Done ():
70
- result .Errors = append ( result . Errors , gqlerrors .FormatError (ctx .Err ()))
70
+ result .AppendErrors ( gqlerrors .FormatError (ctx .Err ()))
71
71
case r := <- resultChannel :
72
72
result = r
73
73
}
Original file line number Diff line number Diff line change 1
1
package graphql
2
2
3
3
import (
4
+ "sync"
5
+
4
6
"github.com/graphql-go/graphql/gqlerrors"
5
7
)
6
8
@@ -9,8 +11,18 @@ import (
9
11
type Result struct {
10
12
Data interface {} `json:"data"`
11
13
Errors []gqlerrors.FormattedError `json:"errors,omitempty"`
14
+
15
+ errorsLock sync.Mutex
12
16
}
13
17
14
18
func (r * Result ) HasErrors () bool {
15
19
return len (r .Errors ) > 0
16
20
}
21
+
22
+ // AppendErrors is the thread-safe way to append error(s) to Result.Errors.
23
+ func (r * Result ) AppendErrors (errs ... gqlerrors.FormattedError ) {
24
+ r .errorsLock .Lock ()
25
+ defer r .errorsLock .Unlock ()
26
+
27
+ r .Errors = append (r .Errors , errs ... )
28
+ }
You can’t perform that action at this time.
0 commit comments