Module: GraphQL::Schema::Member::HasInterfaces

Included in:
Interface::DefinitionMethods, Object
Defined in:
lib/graphql/schema/member/has_interfaces.rb

Overview

-- @requires_ancestor: Kernel @requires_ancestor: Module

Defined Under Namespace

Modules: ClassConfigured

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(child_class) ⇒ Object



137
138
139
# File 'lib/graphql/schema/member/has_interfaces.rb', line 137

def self.extended(child_class)
  child_class.extend(ClassConfigured)
end

Instance Method Details

#implements(*new_interfaces, **options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/graphql/schema/member/has_interfaces.rb', line 12

def implements(*new_interfaces, **options)
  new_memberships = []
  new_interfaces.each do |int|
    if int.is_a?(Module)
      unless int.include?(GraphQL::Schema::Interface) && !int.is_a?(Class)
        non_interface = int #: untyped
        raise "#{non_interface.respond_to?(:graphql_name) ? "#{non_interface.graphql_name} (#{non_interface})" : non_interface.inspect} cannot be implemented since it's not a GraphQL Interface. Use `include` for plain Ruby modules."
      end
      int = int #: as GraphQL::Schema::Member::interface_definition

      new_memberships << int.type_membership_class.new(int, self, **options)

      # Include the methods here,
      # `.fields` will use the inheritance chain
      # to find inherited fields
      include(int)

      # If this interface has interfaces of its own, add those, too
      int.interfaces.each do |next_interface|
        implements(next_interface)
      end
    elsif int.is_a?(String) || int.is_a?(GraphQL::Schema::LateBoundType)
      if !options.empty?
        raise ArgumentError, "`implements(...)` doesn't support options with late-loaded types yet. Remove #{options} and open an issue to request this feature."
      end
      new_memberships << int
    else
      raise ArgumentError, "Unexpected interface definition (expected module): #{int} (#{int.class})"
    end
  end

  # Remove any String or late-bound interfaces which are being replaced
  own_interface_type_memberships.reject! { |old_i_m|
    if !(old_i_m.respond_to?(:abstract_type) && old_i_m.abstract_type.is_a?(Module))
      old_int_type = old_i_m.respond_to?(:abstract_type) ? old_i_m.abstract_type : old_i_m
      old_name = Schema::Member::BuildType.to_type_name(old_int_type)

      new_memberships.any? { |new_i_m|
        new_int_type = new_i_m.respond_to?(:abstract_type) ? new_i_m.abstract_type : new_i_m
        new_name = Schema::Member::BuildType.to_type_name(new_int_type)

        new_name == old_name
      }
    end
  }
  own_interface_type_memberships.concat(new_memberships)
end

#interface_type_memberships ⇒ Object



64
65
66
# File 'lib/graphql/schema/member/has_interfaces.rb', line 64

def interface_type_memberships
  own_interface_type_memberships
end

#interfaces(context = GraphQL::Query::NullContext.instance) ⇒ Object

param context [Query::Context] If omitted, skip filtering.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/graphql/schema/member/has_interfaces.rb', line 109

def interfaces(context = GraphQL::Query::NullContext.instance)
  warden = Warden.from_context(context)
  visible_interfaces = nil #: Array[untyped]?
  own_interface_type_memberships.each do |type_membership|
    case type_membership
    when Schema::TypeMembership
      if warden.visible_type_membership?(type_membership, context)
        visible_interfaces ||= []
        visible_interfaces << type_membership.abstract_type
      end
    when String, Schema::LateBoundType
      # During initialization, `type_memberships` can hold late-bound types
      visible_interfaces ||= []
      visible_interfaces << type_membership
    else
      raise "Invariant: Unexpected type_membership #{type_membership.class}: #{type_membership.inspect}"
    end
  end
  if visible_interfaces
    visible_interfaces.uniq!
    visible_interfaces
  else
    EmptyObjects::EMPTY_ARRAY
  end
end

#own_interface_type_memberships ⇒ Object



60
61
62
# File 'lib/graphql/schema/member/has_interfaces.rb', line 60

def own_interface_type_memberships
  @own_interface_type_memberships ||= []
end