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 } }
Public Class Methods
Source
# File lib/graphql/schema/directive/feature.rb, line 77 def self.enabled?(flag_name, object, context) raise GraphQL::RequiredImplementationMissingError, "Implement `.enabled?(flag_name, object, context)` to return true or false for the feature flag (#{flag_name.inspect})" end
Override this method in your appβs subclass of this directive.
Parameters
-
flag_name(String) β The client-provided string of a feature to check -
object(GraphQL::Schema::Objct) β The currently-evaluatedGraphQLobject instance -
context(GraphQL::Query::Context)
Returns
-
Booleanβ If truthy, execution will continue
# File lib/graphql/schema/directive/feature.rb, line 58 def self.include?(object, arguments, context) flag_name = arguments[:flag] self.enabled?(flag_name, object, context) end
Implement the Directive API
Source
# File lib/graphql/schema/directive/feature.rb, line 81 def self.resolve_field(...); end
# File lib/graphql/schema/directive/feature.rb, line 82 def self.resolve_fragment_spread(...); end
# File lib/graphql/schema/directive/feature.rb, line 83 def self.resolve_inline_fragment(...); end