Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable recover func #31

Merged
merged 1 commit into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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