class GraphQL::Schema::Directive
-
{.resolve}: Wraps field resolution (so it should call ‘yield` to continue)
- {.include?}: if it returns `false`, the field or fragment will be skipped altogether, as if it were absent
Subclasses of this can influence how {GraphQL::Execution::Interpreter} runs queries.
def assert_has_location(location)
def assert_has_location(location) if !self.class.locations.include?(location) raise ArgumentError, <<-MD ive `@#{self.class.graphql_name}` can't be attached to #{@owner.graphql_name} because #{location} isn't included in its locations (#{self.class.locations.join(", ")}). ocations(#{location})` to update this directive's definition, or remove it from #{@owner.graphql_name}. end end
def assert_valid_owner
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) elsif @owner < GraphQL::Schema::Resolver assert_has_location(FIELD_DEFINITION) 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
def default_directive(new_default_directive = nil)
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
def default_directive?
def default_directive? default_directive end
def default_graphql_name
Return a name based on the class name,
def default_graphql_name @default_graphql_name ||= begin camelized_name = super.dup camelized_name[0] = camelized_name[0].downcase -camelized_name end end
def graphql_name
def graphql_name self.class.graphql_name end
def include?(_object, arguments, context)
def include?(_object, arguments, context) static_include?(arguments, context) end
def inherited(subclass)
def inherited(subclass) super subclass.class_exec do @default_graphql_name ||= nil end end
def initialize(owner, **arguments)
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.instance) end
def locations(*new_locations)
def locations(*new_locations) if !new_locations.empty? 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
def on_field?
def on_field? locations.include?(FIELD) end
def on_fragment?
def on_fragment? locations.include?(FRAGMENT_SPREAD) && locations.include?(INLINE_FRAGMENT) end
def on_operation?
def on_operation? locations.include?(QUERY) && locations.include?(MUTATION) && locations.include?(SUBSCRIPTION) end
def path
def path "@#{super}" end
def repeatable(new_value)
def repeatable(new_value) @repeatable = new_value end
def repeatable?
def repeatable? !!@repeatable end
def resolve(object, arguments, context)
def resolve(object, arguments, context) yield end
def resolve_each(object, arguments, context)
def resolve_each(object, arguments, context) yield end
def static_include?(_arguments, _context)
def static_include?(_arguments, _context) true end