class GraphQL::Schema::Directive
Subclasses of this can influence how {GraphQL::Execution::Interpreter} runs queries.
-
{.include?}: if it returns `false`, the field or fragment will be skipped altogether, as if it were absent
-
{.resolve}: Wraps field resolution (so it should call `yield` to continue)
Constants
- DEFAULT_DEPRECATION_REASON
- LOCATIONS
- LOCATION_DESCRIPTIONS
Attributes
arguments[R]
@return [GraphQL::Interpreter::Arguments]
owner[R]
@return [GraphQL::Schema::Field, GraphQL::Schema::Argument
, Class, Module]
Public Class Methods
default_directive(new_default_directive = nil)
click to toggle source
# File lib/graphql/schema/directive.rb, line 44 def default_directive(new_default_directive = nil) if new_default_directive != nil @default_directive = new_default_directive elsif @default_directive.nil? @default_directive = (superclass.respond_to?(:default_directive) ? superclass.default_directive : false) else !!@default_directive end end
default_directive?()
click to toggle source
# File lib/graphql/schema/directive.rb, line 54 def default_directive? default_directive end
default_graphql_name()
click to toggle source
Return a name based on the class name, but downcase the first letter.
Calls superclass method
# File lib/graphql/schema/directive.rb, line 23 def default_graphql_name @default_graphql_name ||= begin camelized_name = super.dup camelized_name[0] = camelized_name[0].downcase camelized_name end end
include?(_object, arguments, context)
click to toggle source
If false, this part of the query won't be evaluated
# File lib/graphql/schema/directive.rb, line 79 def include?(_object, arguments, context) static_include?(arguments, context) end
locations(*new_locations)
click to toggle source
# File lib/graphql/schema/directive.rb, line 31 def locations(*new_locations) if new_locations.any? new_locations.each do |new_loc| if !LOCATIONS.include?(new_loc.to_sym) raise ArgumentError, "#{self} (#{self.graphql_name}) has an invalid directive location: `locations #{new_loc}` " end end @locations = new_locations else @locations ||= (superclass.respond_to?(:locations) ? superclass.locations : []) end end
new(owner, **arguments)
click to toggle source
# File lib/graphql/schema/directive.rb, line 125 def initialize(owner, **arguments) @owner = owner assert_valid_owner # It's be nice if we had the real context here, but we don't. What we _would_ get is: # - error handling # - lazy resolution # Probably, those won't be needed here, since these are configuration arguments, # not runtime arguments. @arguments = self.class.coerce_arguments(nil, arguments, Query::NullContext) end
on_field?()
click to toggle source
# File lib/graphql/schema/directive.rb, line 98 def on_field? locations.include?(FIELD) end
on_fragment?()
click to toggle source
# File lib/graphql/schema/directive.rb, line 102 def on_fragment? locations.include?(FRAGMENT_SPREAD) && locations.include?(INLINE_FRAGMENT) end
on_operation?()
click to toggle source
# File lib/graphql/schema/directive.rb, line 106 def on_operation? locations.include?(QUERY) && locations.include?(MUTATION) && locations.include?(SUBSCRIPTION) end
path()
click to toggle source
# File lib/graphql/schema/directive.rb, line 17 def path "@#{super}" end
repeatable(new_value)
click to toggle source
# File lib/graphql/schema/directive.rb, line 114 def repeatable(new_value) @repeatable = new_value end
repeatable?()
click to toggle source
# File lib/graphql/schema/directive.rb, line 110 def repeatable? !!@repeatable end
resolve(object, arguments, context) { || ... }
click to toggle source
Continuing is passed as a block; `yield` to continue
# File lib/graphql/schema/directive.rb, line 89 def resolve(object, arguments, context) yield end
resolve_each(object, arguments, context) { || ... }
click to toggle source
Continuing is passed as a block, yield to continue.
# File lib/graphql/schema/directive.rb, line 94 def resolve_each(object, arguments, context) yield end
static_include?(_arguments, _context)
click to toggle source
Determines whether {Execution::Lookahead} considers the field to be selected
# File lib/graphql/schema/directive.rb, line 84 def static_include?(_arguments, _context) true end
to_graphql()
click to toggle source
# File lib/graphql/schema/directive.rb, line 60 def to_graphql defn = GraphQL::Directive.new defn.name = self.graphql_name defn.description = self.description defn.locations = self.locations defn.default_directive = self.default_directive defn.ast_node = ast_node defn.metadata[:type_class] = self all_argument_definitions.each do |arg_defn| arg_graphql = arg_defn.to_graphql(silence_deprecation_warning: true) defn.arguments[arg_graphql.name] = arg_graphql # rubocop:disable Development/ContextIsPassedCop -- legacy-related end # Make a reference to a classic-style Arguments class defn.arguments_class = GraphQL::Query::Arguments.construct_arguments_class(defn) defn end
Public Instance Methods
graphql_name()
click to toggle source
# File lib/graphql/schema/directive.rb, line 136 def graphql_name self.class.graphql_name end
Private Instance Methods
assert_has_location(location)
click to toggle source
# File lib/graphql/schema/directive.rb, line 222 def assert_has_location(location) if !self.class.locations.include?(location) raise ArgumentError, <<-MD Directive `@#{self.class.graphql_name}` can't be attached to #{@owner.graphql_name} because #{location} isn't included in its locations (#{self.class.locations.join(", ")}). Use `locations(#{location})` to update this directive's definition, or remove it from #{@owner.graphql_name}. MD end end
assert_valid_owner()
click to toggle source
# File lib/graphql/schema/directive.rb, line 187 def assert_valid_owner case @owner when Class if @owner < GraphQL::Schema::Object assert_has_location(OBJECT) elsif @owner < GraphQL::Schema::Union assert_has_location(UNION) elsif @owner < GraphQL::Schema::Enum assert_has_location(ENUM) elsif @owner < GraphQL::Schema::InputObject assert_has_location(INPUT_OBJECT) elsif @owner < GraphQL::Schema::Scalar assert_has_location(SCALAR) elsif @owner < GraphQL::Schema assert_has_location(SCHEMA) else raise "Unexpected directive owner class: #{@owner}" end when Module assert_has_location(INTERFACE) when GraphQL::Schema::Argument if @owner.owner.is_a?(GraphQL::Schema::Field) assert_has_location(ARGUMENT_DEFINITION) else assert_has_location(INPUT_FIELD_DEFINITION) end when GraphQL::Schema::Field assert_has_location(FIELD_DEFINITION) when GraphQL::Schema::EnumValue assert_has_location(ENUM_VALUE) else raise "Unexpected directive owner: #{@owner.inspect}" end end