Skip to content

Commit

Permalink
Fix the ability of websockets to get errors (#2097)
Browse files Browse the repository at this point in the history
Because DispatchOperation creates tempResponseContext,
which is passed into Exec, which is then used in _Subscription to
generate the next function. Inside the various subscription functions
when generating next the context was captured there.

Which means later when the returned function from DispatchOperation is
called. The responseContext which accumulates the errors is the
tempResponseContext which we no longer have access to to read the errors
out of it.

Instead add a context to next() so that it can be passed through and
accumulated the errors as expected.

Added a unit test for this as well.

Co-authored-by: Chris Pride <cpride@observeinc.com>
  • Loading branch information
telemenar and Chris Pride authored Apr 18, 2022
1 parent e3f04b4 commit ec0dea8
Show file tree
Hide file tree
Showing 17 changed files with 333 additions and 51 deletions.
18 changes: 9 additions & 9 deletions _examples/chat/generated.go

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

10 changes: 5 additions & 5 deletions codegen/directives.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (ec *executionContext) _mutationMiddleware(ctx context.Context, obj *ast.Op
{{ end }}

{{ if .Directives.LocationDirectives "SUBSCRIPTION" }}
func (ec *executionContext) _subscriptionMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) func() graphql.Marshaler {
func (ec *executionContext) _subscriptionMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) func(ctx context.Context) graphql.Marshaler {
for _, d := range obj.Directives {
switch d.Name {
{{- range $directive := .Directives.LocationDirectives "SUBSCRIPTION" }}
Expand All @@ -80,7 +80,7 @@ func (ec *executionContext) _subscriptionMiddleware(ctx context.Context, obj *as
args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs)
if err != nil {
ec.Error(ctx, err)
return func() graphql.Marshaler {
return func(ctx context.Context) graphql.Marshaler {
return graphql.Null
}
}
Expand All @@ -98,15 +98,15 @@ func (ec *executionContext) _subscriptionMiddleware(ctx context.Context, obj *as
tmp, err := next(ctx)
if err != nil {
ec.Error(ctx, err)
return func() graphql.Marshaler {
return func(ctx context.Context) graphql.Marshaler {
return graphql.Null
}
}
if data, ok := tmp.(func() graphql.Marshaler); ok {
if data, ok := tmp.(func(ctx context.Context) graphql.Marshaler); ok {
return data
}
ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp)
return func() graphql.Marshaler {
return func(ctx context.Context) graphql.Marshaler {
return graphql.Null
}
}
Expand Down
4 changes: 2 additions & 2 deletions codegen/field.gotpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{- range $object := .Objects }}{{- range $field := $object.Fields }}

func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Context, field graphql.CollectedField{{ if not $object.Root }}, obj {{$object.Reference | ref}}{{end}}) (ret {{ if $object.Stream }}func(){{ end }}graphql.Marshaler) {
func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Context, field graphql.CollectedField{{ if not $object.Root }}, obj {{$object.Reference | ref}}{{end}}) (ret {{ if $object.Stream }}func(ctx context.Context){{ end }}graphql.Marshaler) {
{{- $null := "graphql.Null" }}
{{- if $object.Stream }}
{{- $null = "nil" }}
Expand Down Expand Up @@ -38,7 +38,7 @@ func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Contex
return {{ $null }}
}
{{- if $object.Stream }}
return func() graphql.Marshaler {
return func(ctx context.Context) graphql.Marshaler {
res, ok := <-resTmp.(<-chan {{$field.TypeReference.GO | ref}})
if !ok {
return nil
Expand Down
2 changes: 1 addition & 1 deletion codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
var buf bytes.Buffer
return func(ctx context.Context) *graphql.Response {
buf.Reset()
data := next()
data := next(ctx)

if data == nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion codegen/object.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var {{ $object.Name|lcFirst}}Implementors = {{$object.Implementors}}

{{- if .Stream }}
func (ec *executionContext) _{{$object.Name}}(ctx context.Context, sel ast.SelectionSet) func() graphql.Marshaler {
func (ec *executionContext) _{{$object.Name}}(ctx context.Context, sel ast.SelectionSet) func(ctx context.Context) graphql.Marshaler {
fields := graphql.CollectFields(ec.OperationContext, sel, {{$object.Name|lcFirst}}Implementors)
ctx = graphql.WithFieldContext(ctx, &graphql.FieldContext{
Object: {{$object.Name|quote}},
Expand Down
2 changes: 1 addition & 1 deletion codegen/root_.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
var buf bytes.Buffer
return func(ctx context.Context) *graphql.Response {
buf.Reset()
data := next()
data := next(ctx)

if data == nil {
return nil
Expand Down
4 changes: 4 additions & 0 deletions codegen/testserver/followschema/nulls.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ extend type Query {
valid: String!
}

extend type Subscription {
errorRequired: Error!
}

type Errors {
a: Error!
b: Error!
Expand Down
4 changes: 4 additions & 0 deletions codegen/testserver/followschema/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ func (r *subscriptionResolver) Issue896b(ctx context.Context) (<-chan []*CheckIs
panic("not implemented")
}

func (r *subscriptionResolver) ErrorRequired(ctx context.Context) (<-chan *Error, error) {
panic("not implemented")
}

func (r *userResolver) Friends(ctx context.Context, obj *User) ([]*User, error) {
panic("not implemented")
}
Expand Down
14 changes: 13 additions & 1 deletion codegen/testserver/followschema/root_.generated.go

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

Loading

0 comments on commit ec0dea8

Please sign in to comment.