Gqlgen: Add field info to directive context

Created on 3 Mar 2020  路  2Comments  路  Source: 99designs/gqlgen

What happened?

I was writing a directive to validate fields. GQLgen did not give me the functionality I need.
I have the following directive that applies on INPUT_FIELD_DEFINITION:

func (r resolver) directiveValidate(
    ctx context.Context, obj interface{}, next graphql.Resolver, rules string,
) (res interface{}, err error) {
    v := validator.New()
    err = v.VarCtx(ctx, obj, rules)
    if err != nil {
        return
    }

    return next(ctx)
}

Unfortunately there is no way for me to check which field is this directive running on right now. obj is pointing to the parent object instead of the field the directive is on.

What did you expect?

I expect to receive in the directive information regarding the field related perhaps in the function context.
What I need is the field's name as in the schema and the field's value either provided in ctx or obj.

Minimal graphql.schema and models to reproduce

directive @validate(rules: String!) on INPUT_FIELD_DEFINITION
input Login {
  # unless I explicitly pass to the directive the field name, there is no way to know what field is being "validated" when the directive runs. 
  username: String! @validate(rules: "min=6")
}

versions

  • gqlgen version? 0.11.1
  • go version? 1.13.8
  • dep or go modules? go modules
accepted

Most helpful comment

Hi @seaskyways @vektah,
Is there an ETA for this?

All 2 comments

@vektah , @seaskyways, We encountered this issue as well: https://github.com/99designs/gqlgen/issues/990#issuecomment-585550923

Adding it here for completeness

Hi @seaskyways @vektah,
Is there an ETA for this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sumanthakannantha picture sumanthakannantha  路  3Comments

msmedes picture msmedes  路  4Comments

steebchen picture steebchen  路  3Comments

bieber picture bieber  路  4Comments

huanghantao picture huanghantao  路  3Comments