module GraphQL::Schema::Interface::DefinitionMethods

Public Instance Methods

accessible?(context) click to toggle source

The interface is accessible if any of its possible types are accessible

# File lib/graphql/schema/interface.rb, line 34
def accessible?(context)
  context.schema.possible_types(self, context).each do |type|
    if context.schema.accessible?(type, context)
      return true
    end
  end
  false
end
definition_methods(&block) click to toggle source

Methods defined in this block will be:

  • Added as class methods to this interface

  • Added as class methods to all child interfaces

# File lib/graphql/schema/interface.rb, line 24
def definition_methods(&block)
  self::DefinitionMethods.module_eval(&block)
end
included(child_class) click to toggle source

Here's the tricky part. Make sure behavior keeps making its way down the inheritance chain.

Calls superclass method
# File lib/graphql/schema/interface.rb, line 52
def included(child_class)
  if !child_class.is_a?(Class)
    # In this case, it's been included into another interface.
    # This is how interface inheritance is implemented

    # We need this before we can call `own_interfaces`
    child_class.extend(Schema::Interface::DefinitionMethods)

    child_class.type_membership_class(self.type_membership_class)
    child_class.ancestors.reverse_each do |ancestor|
      if ancestor.const_defined?(:DefinitionMethods)
        child_class.extend(ancestor::DefinitionMethods)
      end
    end

    # Use an instance variable to tell whether it's been included previously or not;
    # You can't use constant detection because constants are brought into scope
    # by `include`, which has already happened at this point.
    if !child_class.instance_variable_defined?(:@_definition_methods)
      defn_methods_module = Module.new
      child_class.instance_variable_set(:@_definition_methods, defn_methods_module)
      child_class.const_set(:DefinitionMethods, defn_methods_module)
      child_class.extend(child_class::DefinitionMethods)
    end
    child_class.introspection(introspection)
    child_class.description(description)
    # If interfaces are mixed into each other, only define this class once
    if !child_class.const_defined?(:UnresolvedTypeError, false)
      add_unresolved_type_error(child_class)
    end
  elsif child_class < GraphQL::Schema::Object
    # This is being included into an object type, make sure it's using `implements(...)`
    backtrace_line = caller(0, 10).find { |line| line.include?("schema/member/has_interfaces.rb") && line.include?("in `implements'")}
    if !backtrace_line
      raise "Attach interfaces using `implements(#{self})`, not `include(#{self})`"
    end
  end

  super
end
kind() click to toggle source
# File lib/graphql/schema/interface.rb, line 123
def kind
  GraphQL::TypeKinds::INTERFACE
end
orphan_types(*types) click to toggle source
Calls superclass method
# File lib/graphql/schema/interface.rb, line 93
def orphan_types(*types)
  if types.any?
    @orphan_types = types
  else
    all_orphan_types = @orphan_types || []
    all_orphan_types += super if defined?(super)
    all_orphan_types.uniq
  end
end
to_graphql() click to toggle source
# File lib/graphql/schema/interface.rb, line 105
def to_graphql
  type_defn = GraphQL::InterfaceType.new
  type_defn.name = graphql_name
  type_defn.description = description
  type_defn.orphan_types = orphan_types
  type_defn.type_membership_class = self.type_membership_class
  type_defn.ast_node = ast_node
  fields.each do |field_name, field_inst| # rubocop:disable Development/ContextIsPassedCop -- legacy-related
    field_defn = field_inst.graphql_definition(silence_deprecation_warning: true)
    type_defn.fields[field_defn.name] = field_defn # rubocop:disable Development/ContextIsPassedCop -- legacy-related
  end
  type_defn.metadata[:type_class] = self
  if respond_to?(:resolve_type)
    type_defn.resolve_type = method(:resolve_type)
  end
  type_defn
end
type_membership_class(membership_class = nil) click to toggle source
# File lib/graphql/schema/interface.rb, line 43
def type_membership_class(membership_class = nil)
  if membership_class
    @type_membership_class = membership_class
  else
    @type_membership_class || find_inherited_value(:type_membership_class, GraphQL::Schema::TypeMembership)
  end
end
visible?(context) click to toggle source

@see {Schema::Warden} hides interfaces without visible implementations

# File lib/graphql/schema/interface.rb, line 29
def visible?(context)
  true
end