module GraphQL::Schema::Member::HasDirectives

Constants

NO_DIRECTIVES

Public Instance Methods

directive(dir_class, **options) click to toggle source

Create an instance of `dir_class` for `self`, using `options`.

It removes a previously-attached instance of `dir_class`, if there is one.

@return [void]

# File lib/graphql/schema/member/has_directives.rb, line 12
def directive(dir_class, **options)
  @own_directives ||= []
  remove_directive(dir_class)
  @own_directives << dir_class.new(self, **options)
  nil
end
directives() click to toggle source
# File lib/graphql/schema/member/has_directives.rb, line 29
def directives
  case self
  when Class
    inherited_directives = if superclass.respond_to?(:directives)
      superclass.directives
    else
      NO_DIRECTIVES
    end
    if inherited_directives.any? && @own_directives
      dirs = []
      merge_directives(dirs, inherited_directives)
      merge_directives(dirs, @own_directives)
      dirs
    elsif @own_directives
      @own_directives
    elsif inherited_directives.any?
      inherited_directives
    else
      NO_DIRECTIVES
    end
  when Module
    dirs = nil
    self.ancestors.reverse_each do |ancestor|
      if ancestor.respond_to?(:own_directives) &&
          (anc_dirs = ancestor.own_directives).any?
        dirs ||= []
        merge_directives(dirs, anc_dirs)
      end
    end
    if own_directives
      dirs ||= []
      merge_directives(dirs, own_directives)
    end
    dirs || NO_DIRECTIVES
  when HasDirectives
    @own_directives || NO_DIRECTIVES
  else
    raise "Invariant: how could #{self} not be a Class, Module, or instance of HasDirectives?"
  end
end
remove_directive(dir_class) click to toggle source

Remove an attached instance of `dir_class`, if there is one @param dir_class [Class<GraphQL::Schema::Directive>] @return [viod]

# File lib/graphql/schema/member/has_directives.rb, line 22
def remove_directive(dir_class)
  @own_directives && @own_directives.reject! { |d| d.is_a?(dir_class) }
  nil
end

Protected Instance Methods

own_directives() click to toggle source
# File lib/graphql/schema/member/has_directives.rb, line 72
def own_directives
  @own_directives
end

Private Instance Methods

merge_directives(target, dirs) click to toggle source

Modify `target` by adding items from `dirs` such that:

  • Any name conflict is overriden by the incoming member of `dirs`

  • Any other member of `dirs` is appended

@param target [Array<GraphQL::Schema::Directive>] @param dirs [Array<GraphQL::Schema::Directive>] @return [void]

# File lib/graphql/schema/member/has_directives.rb, line 84
def merge_directives(target, dirs)
  dirs.each do |dir|
    if (idx = target.find_index { |d| d.graphql_name == dir.graphql_name })
      target.slice!(idx)
      target.insert(idx, dir)
    else
      target << dir
    end
  end
  nil
end