-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
get parent object in directive function #289
Comments
Good question ;) |
Hi! // directive implement func
resp, err := next(ctx) resp always contains the type of the field to which the directive is assigned and you receive a compilation error, or runtime error if you use a directive for fields with different types. But if you use a websocket as a transport, you can see that wsConnection ("gqlgen/handler/websocket.go") struct is not change on every request. Exportable WSConnection with new CurrentUser field you can write to context. But this will require changing the package "99designs/gqlgen/handler" (you can copy this to project directory). ...
type WSConnection struct {
ctx context.Context
conn *websocket.Conn
exec graphql.ExecutableSchema
active map[string]context.CancelFunc
mu sync.Mutex
cfg *Config
// new field
CurrentUser *gqlgen_directive_parent.User
}
type key string
const WSConnectionKey key = "WSConnectionKey"
...
func (c *WSConnection) subscribe(message *operationMessage) bool {
...
reqCtx := c.cfg.newRequestContext(doc, reqParams.Query, vars)
ctx := graphql.WithRequestContext(c.ctx, reqCtx)
// write WSConnection into context
ctx = context.WithValue(ctx, WSConnectionKey, c)
...
} ID comparison must be performed at todoResolver func. func (r *todoResolver) Text(ctx context.Context, obj *gqlgen_directive_parent.Todo) (string, error) {
conn, ok := ctx.Value(handler.WSConnectionKey).(*handler.WSConnection)
if !ok {
return nil, gqlerror.Errorf("BOOM! Headshot")
}
currentUser := conn.CurrentUser
...
if todo.User.ID != currentUser.ID {
return nil, nil
}
...
} Perhaps this way is idiomatically incorrect, but for me it works. |
I'm not sure how necessary this is. To me it feels like a directive is not the correct tool for this requirement, as it would explicitly tie your directive to the context of a Todo, unless you wrote your directive in such a way that it handled every possible parent type. After a bit of internal discussion, we thought that a solution might instead be for the parent resolver to set something in the context that the directive could read back out. Something like I am interested to know what other people think though, as I'm interested in what other use-cases people have for directives. |
maybe a object level directive can be helpful ? |
It seems like Resolver has a hierarchy of ParentObject in ResolverContext, just like Path. My other usecase.
|
Another interesting use case is in mutations as @jekaspekas suggests:
you never want to run the update, you kind of want to do the select, then run the directive, and then update. It makes me really wish mutations were in the graph, like:
And then you would have the parent object to check again. |
IMHO is here ... #301 so, it is the implementer's responsibility to which evaluate first about next func or directive. |
hey @vvakame @vektah trying to implement this on my directive, but the
and here the config.Directive
I was look and tried the |
The same situation appears to me. The directive is never called. But if it works on type-system-extesion Does anyone know how to solve it? |
I just saw it, it only works on master branch |
Expected Behaviour
Parent object can be retrieved from ResolverContext.
Actual Behavior
It seems not to be able to get it.
Minimal graphql.schema and models to reproduce
I made small demo project.
Is there a good way to do it?
https://github.com/vvakame/til/blob/ad9d192d297b4e0be358f3273442502497ff50fb/graphql/gqlgen-directive-parent/schema.graphql#L9
https://github.com/vvakame/til/blob/ad9d192d297b4e0be358f3273442502497ff50fb/graphql/gqlgen-directive-parent/server/server.go#L49-L56
The text was updated successfully, but these errors were encountered: