class GraphQL::Schema::Directive::Feature

An example directive to show how you might interact with the runtime.

This directive might be used along with a server-side feature flag system like Flipper.

With that system, you could use this directive to exclude parts of a query if the current viewer doesn’t have certain flags enabled. (So, this flag would be for internal clients, like your iOS app, not third-party API clients.)

To use it, you have to implement .enabled?, for example:

Examples

Example: Implementing the Feature directive

# app/graphql/directives/feature.rb
class Directives::Feature < GraphQL::Schema::Directive::Feature
  def self.enabled?(flag_name, _obj, context)
    # Translate some GraphQL data for Ruby:
    flag_key = flag_name.underscore
    current_user = context[:viewer]
    # Check the feature flag however your app does it:
    MyFeatureFlags.enabled?(current_user, flag_key)
  end
end

Example: Flagging a part of the query

viewer {
  # This field only runs if `.enabled?("recommendationEngine", obj, context)`
  # returns true. Otherwise, it's treated as if it didn't exist.
  recommendations @feature(flag: "recommendationEngine") {
    name
    rating
  }
}