class GraphQL::Schema::Directive::Feature
}
}
rating
name
recommendations @feature(flag: “recommendationEngine”) {
# returns true. Otherwise, it’s treated as if it didn’t exist.
# This field only runs if ‘.enabled?(“recommendationEngine”, obj, context)`
viewer {
@example Flagging a part of the query
end
end
MyFeatureFlags.enabled?(current_user, flag_key)
# Check the feature flag however your app does it:
current_user = context[:viewer]
flag_key = flag_name.underscore
# Translate some GraphQL data for Ruby:
def self.enabled?(flag_name, _obj, context)
class Directives::Feature < GraphQL::Schema::Directive::Feature
# app/graphql/directives/feature.rb
@example Implementing the Feature directive
To use it, you have to implement `.enabled?`, for example:
(So, this flag would be for internal clients, like your iOS app, not third-party API clients.)
if the current viewer doesn’t have certain flags enabled.
With that system, you could use this directive to exclude parts of a query
This directive might be used along with a server-side feature flag system like Flipper.
An example directive to show how you might interact with the runtime.
def self.enabled?(flag_name, object, context)
-
(Boolean)
- If truthy, execution will continue
Parameters:
-
context
(GraphQL::Query::Context
) -- -
object
(GraphQL::Schema::Objct
) -- The currently-evaluated GraphQL object instance -
flag_name
(String
) -- The client-provided string of a feature to check
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
def self.include?(object, arguments, context)
def self.include?(object, arguments, context) flag_name = arguments[:flag] self.enabled?(flag_name, object, context) end