Class: GraphQL::Schema::Directive::Feature
- Inherits:
-
GraphQL::Schema::Directive
- Object
- Member
- GraphQL::Schema::Directive
- GraphQL::Schema::Directive::Feature
- Defined in:
- lib/graphql/schema/directive/feature.rb
Overview
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
}
}
Constant Summary
Constants inherited from GraphQL::Schema::Directive
DEFAULT_DEPRECATION_REASON, LOCATIONS, LOCATION_DESCRIPTIONS
Constants included from Member::HasArguments
Member::HasArguments::NO_ARGUMENTS
Constants included from EmptyObjects
EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH
Constants included from Member::GraphQLTypeNames
Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int
Instance Attribute Summary
Attributes inherited from GraphQL::Schema::Directive
Attributes included from Member::BaseDSLMethods
#default_graphql_name, #graphql_name
Attributes included from Member::RelayShortcuts
#connection_type, #connection_type_class, #edge_type, #edge_type_class
Attributes included from Member::HasAstNode
Class Method Summary collapse
-
.enabled?(flag_name, object, context) ⇒ Boolean
Override this method in your app's subclass of this directive.
-
.include?(object, arguments, context) ⇒ Boolean
Implement the Directive API.
- .resolve_field ⇒ Object
- .resolve_fragment_spread ⇒ Object
- .resolve_inline_fragment ⇒ Object
Methods inherited from GraphQL::Schema::Directive
default_directive, default_directive?, default_graphql_name, #graphql_name, #initialize, locations, on_field?, on_fragment?, on_operation?, path, repeatable, repeatable?, resolve, resolve_each, resolve_operation, runtime?, static_include?, validate!
Methods included from Member::HasArguments
#add_argument, #all_argument_definitions, #any_arguments?, #argument, #argument_class, #arguments, #arguments_statically_coercible?, #coerce_arguments, extended, #get_argument, included, #own_arguments, #remove_argument, #validate_directive_argument
Methods included from Member::HasArguments::HasDirectiveArguments
Methods included from Member::HasValidators
extended, #validates, #validators
Methods inherited from Member
Methods included from Member::BaseDSLMethods
#authorized?, #comment, #default_relay?, #description, #introspection, #introspection?, #mutation, #visible?
Methods included from FindInheritedValue
Methods included from Member::BaseDSLMethods::ConfigurationExtension
Methods included from Member::TypeSystemHelpers
#initialize, #kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature
Methods included from Member::Scoped
#inherited, #reauthorize_scoped_objects, #scope_items
Methods included from Member::HasPath
Methods included from Member::HasAstNode
Methods included from Member::HasDirectives
add_directive, #directive, #directives, extended, get_directives, #inherited, #remove_directive, remove_directive
Constructor Details
This class inherits a constructor from GraphQL::Schema::Directive
Class Method Details
.enabled?(flag_name, object, context) ⇒ Boolean
Override this method in your app's subclass of this directive.
Parameters
flag_name(String) — The client-provided string of a feature to checkobject(GraphQL::Schema::Objct) — The currently-evaluated GraphQL object instancecontext(GraphQL::Query::Context)
Returns
Boolean— If truthy, execution will continue
:call-seq:
enabled?(String flag_name, GraphQL::Schema::Objct object, GraphQL::Query::Context context) -> bool
78 79 80 |
# File 'lib/graphql/schema/directive/feature.rb', line 78 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 |
.include?(object, arguments, context) ⇒ Boolean
Implement the Directive API
59 60 61 62 |
# File 'lib/graphql/schema/directive/feature.rb', line 59 def self.include?(object, arguments, context) flag_name = arguments[:flag] self.enabled?(flag_name, object, context) end |
.resolve_field ⇒ Object
82 |
# File 'lib/graphql/schema/directive/feature.rb', line 82 def self.resolve_field(...); end |
.resolve_fragment_spread ⇒ Object
83 |
# File 'lib/graphql/schema/directive/feature.rb', line 83 def self.resolve_fragment_spread(...); end |
.resolve_inline_fragment ⇒ Object
84 |
# File 'lib/graphql/schema/directive/feature.rb', line 84 def self.resolve_inline_fragment(...); end |