Skip to content

Commit f127033

Browse files
racerxdlchris-ramon
authored andcommitted
Added Support for CustomErrorFormatter
1 parent 28550b4 commit f127033

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ _testmain.go
2323
*.test
2424
*.prof
2525
*.swp
26+
.idea

handler.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/graphql-go/graphql"
1111

1212
"context"
13+
"github.com/graphql-go/graphql/gqlerrors"
1314
)
1415

1516
const (
@@ -27,6 +28,7 @@ type Handler struct {
2728
playground bool
2829
rootObjectFn RootObjectFn
2930
resultCallbackFn ResultCallbackFn
31+
customErrorFormatter func(err error) gqlerrors.FormattedError
3032
}
3133
type RequestOptions struct {
3234
Query string `json:"query" url:"query" schema:"query"`
@@ -126,11 +128,12 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
126128

127129
// execute graphql query
128130
params := graphql.Params{
129-
Schema: *h.Schema,
130-
RequestString: opts.Query,
131-
VariableValues: opts.Variables,
132-
OperationName: opts.OperationName,
133-
Context: ctx,
131+
Schema: *h.Schema,
132+
RequestString: opts.Query,
133+
VariableValues: opts.Variables,
134+
OperationName: opts.OperationName,
135+
Context: ctx,
136+
CustomErrorFomatter: h.customErrorFormatter,
134137
}
135138
if h.rootObjectFn != nil {
136139
params.RootObject = h.rootObjectFn(ctx, r)
@@ -191,6 +194,7 @@ type Config struct {
191194
Playground bool
192195
RootObjectFn RootObjectFn
193196
ResultCallbackFn ResultCallbackFn
197+
CustomErrorFormatter func(err error) gqlerrors.FormattedError
194198
}
195199

196200
func NewConfig() *Config {
@@ -206,6 +210,7 @@ func New(p *Config) *Handler {
206210
if p == nil {
207211
p = NewConfig()
208212
}
213+
209214
if p.Schema == nil {
210215
panic("undefined GraphQL schema")
211216
}
@@ -217,5 +222,6 @@ func New(p *Config) *Handler {
217222
playground: p.Playground,
218223
rootObjectFn: p.RootObjectFn,
219224
resultCallbackFn: p.ResultCallbackFn,
225+
customErrorFormatter: p.CustomErrorFormatter,
220226
}
221227
}

0 commit comments

Comments
 (0)