I have a schema where I want to log the name and value for an input field, but I am unable to get the name and value of the input field associated with the directive
I would expect the name and value to be printed as mentioned in the type extension examples: https://github.com/99designs/gqlgen/blob/master/example/type-system-extension/directive.go
Schema:
directive @inputLog on INPUT_FIELD_DEFINITION
input NewTodo {
text: String! @inputLog
userId: String!
}
type Mutation {
createTodo(input: NewTodo!): Todo!
}
Directive definition:
func InputLog(
ctx context.Context,
obj interface{},
next graphql.Resolver,
) (res interface{}, err error) {
rc := graphql.GetResolverContext(ctx)
strToBeLogged := fmt.Sprintf("field: %v has value %+v", rc.Field.Name, obj)
fmt.Println(strToBeLogged)
return next(ctx)
}
prints: field: createTodo has value {Text: UserID:}
I would expect field: text has value foo
gqlgen version: 0.8.0go version: 1.13.4next() gives you the value, it also allows authentication directives to prevent calling it.
obj is the thing surrounding the thing the directive was attached do.
Thanks for the information @vektah. The value part makes sense. How do you propose I get the name for the field?
is graphql.GetFieldContext(ctx).Field.Name what you're looking for?
@vektah : Thanks for the response. I am not on the latest version of gqlgen, but based on the code for GetFieldContext(ctx), it seems that it was mostly a rename of GetResolverContext. As shown by the code snippet above, graphql.GetResolverContext(ctx).Field.Name results in createTodo which is the wrapper and not text which is the name of the field that the value is attached to. I am basically looking to generate a log that for field text the input value sent by the caller was foo
I ended up working around this to do some parsing at the level when the input is created. So, my new schema looks as follows:
directive @inputLog(blacklist:String) on INPUT_FIELD_DEFINITION
input NewTodo {
text: String!
userId: String!
}
type Mutation {
createTodo(input: NewTodo! @inputLog(blacklist: "userId")): Todo!
}
I get a handle to the NewTodo object which is of the type map[string]interface{} and proceed to log the fields from there using JSON