Add field metadata with custom schema directive

Viewed 9

I want to add custom metadata to fields in GraphQL objects, similar to how the @deprecated directive adds a deprecationReason to a field. I found out graphql-ruby supports creating custom schema directives but I cannot figure out how to add my own metadata to the field the directive is applied to.

If I have a directive:

module Directives
  class Permission < GraphQL::Schema::Directive
    argument :level, String
    locations FIELD_DEFINITION
  end
end

and a type

module Types
  class NoteType < GraphQL::Schema::Object
    field :id, ID, null: false
    field :title, String, null: false, directives: { Directives::Permission => { level: "Manager" }}
  end
end

my goal is to be able to do something like:

{
  __type(name: "Essential") {
    name
    fields(includeDeprecated: true) {
      name
      level      
    }
  }
}

and get a return value like:

{
  "data": {
    "__type": {
      "name": "Note",
      "fields": [
        {
          "name": "id",
          "level": null
        },
        {
          "name": "title",
          "level": "Manager"
        },
      ]
    }
  }
}
0 Answers
Related