Gqlgen: How to get the field names of directives?

Created on 18 Oct 2019  路  7Comments  路  Source: 99designs/gqlgen

What happened?

directive @isID on FIELD_DEFINITION
type Salesperson {

    Name  : String!
    Age: Int! 
    UserID: Int! @isID
}

For something like this, with directive IsID how would you get 'UserID' as a string?
I can get the entire field with obj, or the value of UserID but that is not enough to infer 'UserID'

UserID: Int! @isID(FieldName:"UserID")
This seems to be the best I can do.

versions

  • gqlgen version? v0.10.1-dev
  • go version? go version go1.12.7 linux/amd64
  • dep or go modules? mod
question stale

All 7 comments

Thank you @JulienBreux

I guess it was mentioned here in the documentation.
I completely went past it.
This is the directive implementation

import ("github.com/99designs/gqlgen/graphql")
c.Directives.IsID = func(ctx context.Context, obj interface{}, next graphql.Resolver) (interface{}, error) {
        ctxValue := graphql.GetResolverContext(ctx)
        fmt.Printf("context: %#v\n", ctxValue)
}

Unfortunately this seems not to be working.

graphql.GetResolverContext(ctx).Object returns 'Salesperson' not 'UserID' and
graphql.CollectAllFields(ctx) returns an empty array.

Based on docs https://gqlgen.com/reference/directives/

- obj: the object containing the value this was applied to, eg:
for field definition directives, the object/input object that contains the field
for argument directives, a map containing all arguments

but I see in generated code that obj is always set as nil. Looks like it is a bug

@o1egl Thanks. Good to know it's a bug.

@vvakame Thank you for the label, also your MTC2018 blog was amazing (sorry for off topic)

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Hello,
Any upcoming news about this bug ?
Or is there any reason why the field name of the given directive shouldn't be visible from directive resolver ???

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jacksontj picture jacksontj  路  4Comments

coderste picture coderste  路  3Comments

sumanthakannantha picture sumanthakannantha  路  3Comments

JulienBreux picture JulienBreux  路  3Comments

RobertoOrtis picture RobertoOrtis  路  3Comments