-
Notifications
You must be signed in to change notification settings - Fork 493
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
Added ability to use your own directives usage #446
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ type Request struct { | |
Logger log.Logger | ||
PanicHandler errors.PanicHandler | ||
SubscribeResolverTimeout time.Duration | ||
Visitors map[string]types.DirectiveVisitor | ||
} | ||
|
||
func (r *Request) handlePanic(ctx context.Context) { | ||
|
@@ -208,8 +209,47 @@ func execFieldSelection(ctx context.Context, r *Request, s *resolvable.Schema, f | |
if f.field.ArgsPacker != nil { | ||
in = append(in, f.field.PackedArgs) | ||
} | ||
|
||
// Before hook directive visitor | ||
if len(f.field.Directives) > 0 { | ||
for _, directive := range f.field.Directives { | ||
if visitor, ok := r.Visitors[directive.Name.Name]; ok { | ||
var values = make([]interface{}, 0) | ||
for _, inValue := range in { | ||
values = append(values, inValue.Interface()) | ||
} | ||
|
||
if visitorErr := visitor.Before(ctx, directive, values); err != nil { | ||
err := errors.Errorf("%s", visitorErr) | ||
err.Path = path.toSlice() | ||
err.ResolverError = visitorErr | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These visitors will only be invoked if we use method resolvers. How about when we use struct fields as resolvers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm talking about lines 263 to 269 |
||
|
||
// Call method | ||
callOut := res.Method(f.field.MethodIndex).Call(in) | ||
result = callOut[0] | ||
|
||
// After hook directive visitor (when no error is returned from resolver) | ||
if !f.field.HasError && len(f.field.Directives) > 0 { | ||
for _, directive := range f.field.Directives { | ||
if visitor, ok := r.Visitors[directive.Name.Name]; ok { | ||
returned, visitorErr := visitor.After(ctx, directive, result.Interface()) | ||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be |
||
err := errors.Errorf("%s", visitorErr) | ||
err.Path = path.toSlice() | ||
err.ResolverError = visitorErr | ||
return err | ||
} else { | ||
result = reflect.ValueOf(returned) | ||
} | ||
} | ||
} | ||
} | ||
|
||
if f.field.HasError && !callOut[1].IsNil() { | ||
resolverErr := callOut[1].Interface().(error) | ||
err := errors.Errorf("%s", resolverErr) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be
if visitorErr := visitor.Before(ctx, directive, values); visitorErr != nil {