Skip to content

Commit

Permalink
Merge pull request 99designs#31 from vektah/recover-handler
Browse files Browse the repository at this point in the history
Customizable recover func
  • Loading branch information
vektah authored Feb 26, 2018
2 parents 7d5285b + 9a9cb5d commit 5e6a613
Show file tree
Hide file tree
Showing 17 changed files with 355 additions and 88 deletions.
4 changes: 2 additions & 2 deletions codegen/templates/data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion codegen/templates/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@
{{- template "args.gotpl" $field.Args }}

{{- if $field.IsConcurrent }}
return graphql.Defer(func() graphql.Marshaler {
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.recover(r)
ec.Error(userErr)
ret = graphql.Null
}
}()
{{- end }}

{{- if $field.GoVarName }}
Expand Down
13 changes: 7 additions & 6 deletions codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func (e *executableSchema) Schema() *schema.Schema {
return parsedSchema
}

func (e *executableSchema) Query(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {
func (e *executableSchema) Query(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation, recover graphql.RecoverFunc) *graphql.Response {
{{- if .QueryRoot }}
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx, recover: recover}

data := ec._{{.QueryRoot.GQLType}}(op.Selections)
var buf bytes.Buffer
Expand All @@ -45,9 +45,9 @@ func (e *executableSchema) Query(ctx context.Context, doc *query.Document, varia
{{- end }}
}

func (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {
func (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation, recover graphql.RecoverFunc) *graphql.Response {
{{- if .MutationRoot }}
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx, recover: recover}

data := ec._{{.MutationRoot.GQLType}}(op.Selections)
var buf bytes.Buffer
Expand All @@ -62,9 +62,9 @@ func (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, va
{{- end }}
}

func (e *executableSchema) Subscription(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) func() *graphql.Response {
func (e *executableSchema) Subscription(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation, recover graphql.RecoverFunc) func() *graphql.Response {
{{- if .SubscriptionRoot }}
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx, recover: recover}

next := ec._{{.SubscriptionRoot.GQLType}}(op.Selections)
if ec.Errors != nil {
Expand Down Expand Up @@ -98,6 +98,7 @@ type executionContext struct {
variables map[string]interface{}
doc *query.Document
ctx context.Context
recover graphql.RecoverFunc
}

{{- range $object := .Objects }}
Expand Down
22 changes: 15 additions & 7 deletions example/chat/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (e *executableSchema) Schema() *schema.Schema {
return parsedSchema
}

func (e *executableSchema) Query(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}
func (e *executableSchema) Query(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation, recover graphql.RecoverFunc) *graphql.Response {
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx, recover: recover}

data := ec._Query(op.Selections)
var buf bytes.Buffer
Expand All @@ -46,8 +46,8 @@ func (e *executableSchema) Query(ctx context.Context, doc *query.Document, varia
}
}

func (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}
func (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation, recover graphql.RecoverFunc) *graphql.Response {
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx, recover: recover}

data := ec._Mutation(op.Selections)
var buf bytes.Buffer
Expand All @@ -59,8 +59,8 @@ func (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, va
}
}

func (e *executableSchema) Subscription(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) func() *graphql.Response {
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}
func (e *executableSchema) Subscription(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation, recover graphql.RecoverFunc) func() *graphql.Response {
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx, recover: recover}

next := ec._Subscription(op.Selections)
if ec.Errors != nil {
Expand Down Expand Up @@ -91,6 +91,7 @@ type executionContext struct {
variables map[string]interface{}
doc *query.Document
ctx context.Context
recover graphql.RecoverFunc
}

var chatroomImplementors = []string{"Chatroom"}
Expand Down Expand Up @@ -277,7 +278,14 @@ func (ec *executionContext) _Query_room(field graphql.CollectedField) graphql.Ma
return graphql.Null
}
}
return graphql.Defer(func() graphql.Marshaler {
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.recover(r)
ec.Error(userErr)
ret = graphql.Null
}
}()
res, err := ec.resolvers.Query_room(ec.ctx, arg0)
if err != nil {
ec.Error(err)
Expand Down
Loading

0 comments on commit 5e6a613

Please sign in to comment.