Skip to content

Obtaining field directives at execution time #65

@Hellzed

Description

@Hellzed

Given the following SDL example:

directive @example(
  arg: String
) on FIELD_DEFINITION

type Query {
  field: String @example(arg: "some value")
}

At execution time (I'm assuming from within a resolver or middleware), when resolving field, what would be the proper way to know the example directive is applied to it, and to get "some value"?

I've checked the doc and found graphql.get_directive_values() but can't figure how to use it.
GraphQLResolveInfo.field_nodes is a list with only one field node, which has an empty directives property.

I know providing a framework to define directives is not a goal of this lib, but I just need a bare bones way to access this data.

Edit:
After checking the internals, I get it better: GraphQLResolveInfo.field_nodes gives access to the selection nodes, and these only carry client query directives.

To access the field definition within the server schema, I have to do the following:

field_def = info.parent_type.fields[info.field_name]
directive = info.schema.get_directive("example")
graphql.get_directive_values(directive, field_def)  # {"arg": "some value"}

Let me know if I'm doing something wrong! (and feel free to close this issue. I feel like having some doc about it might make sense.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions