Module: GraphQL::Schema::Interface::DefinitionMethods

Constant Summary

Constants included from Member::HasFields

Member::HasFields::CONFLICT_FIELD_NAMES, Member::HasFields::GRAPHQL_RUBY_KEYWORDS, Member::HasFields::RUBY_KEYWORDS

Constants included from EmptyObjects

EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH

Instance Attribute Summary

Attributes included from Member::HasAstNode

#ast_node

Attributes included from Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Attributes included from Member::BaseDSLMethods

#default_graphql_name, #graphql_name

Instance Method Summary collapse

Methods included from Member::HasInterfaces

extended, #implements, #interface_type_memberships, #interfaces, #own_interface_type_memberships

Methods included from Member::HasDirectives

add_directive, #directive, #directives, extended, get_directives, #inherited, #remove_directive, remove_directive

Methods included from Member::HasDataloader

#dataload, #dataload_all, #dataload_all_associations, #dataload_all_records, #dataload_association, #dataload_record, #dataloader

Methods included from Member::HasAstNode

extended, #inherited

Methods included from Member::Scoped

#inherited, #reauthorize_scoped_objects, #scope_items

Methods included from Member::HasPath

#path

Methods included from Member::HasFields

#add_field, #all_field_definitions, extended, #field, #field_class, #global_id_field, #has_no_fields, #has_no_fields?, included, #own_fields

Methods included from Member::TypeSystemHelpers

#initialize, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature

Methods included from Member::BaseDSLMethods

#authorized?, #comment, #default_relay?, #description, #introspection, #introspection?, #mutation

Methods included from FindInheritedValue

extended, included

Instance Method Details

#definition_methods(&block) ⇒ Object

Methods defined in this block will be:

  • Added as class methods to this interface
  • Added as class methods to all child interfaces


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/graphql/schema/interface.rb', line 25

def definition_methods(&block)
  # 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 !defined?(@_definition_methods)
    defn_methods_module = Module.new
    @_definition_methods = defn_methods_module
    const_set(:DefinitionMethods, defn_methods_module)
    extend(defn_methods_module)
  end
  const_get(:DefinitionMethods).module_exec(&block)
end

#included(child_class) ⇒ Object

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/graphql/schema/interface.rb', line 76

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) && ancestor != child_class
        child_class.extend(ancestor.const_get(:DefinitionMethods))
      end
    end

    child_class.introspection(introspection)
    child_class.description(description)
    child_class.comment(nil)
    # 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(...)`
    locations = caller_locations(0, 10) #: as !nil
    backtrace_line = locations.find do |location|
      location_path = location.path #: as !nil
      location.base_label == "implements" &&
        location_path.end_with?("schema/member/has_interfaces.rb")
    end

    if !backtrace_line
      raise "Attach interfaces using `implements(#{self})`, not `include(#{self})`"
    end

    child_class.ancestors.reverse_each do |ancestor|
      if ancestor != child_class && ancestor <= GraphQL::Schema::Interface && ancestor.const_defined?(:ResolverMethods, false)
        child_class.extend(ancestor.const_get(:ResolverMethods))
      end
    end
  end

  super
end

#kind ⇒ Object



156
157
158
# File 'lib/graphql/schema/interface.rb', line 156

def kind
  GraphQL::TypeKinds::INTERFACE
end

#orphan_types(*types) ⇒ Object

Register other Interface or Object types as implementers of this Interface.

When those Interfaces or Objects aren't used as the return values of fields, they may have to be registered using this method so that GraphQL-Ruby can find them.

Parameters

  • types (Class, Module)

Returns

  • Array<Module, Class> — Implementers of this interface, if they're registered

:call-seq:

orphan_types(Class | Module *types) -> Array[Module | Class]


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/graphql/schema/interface.rb', line 136

def orphan_types(*types)
  if !types.empty?
    @orphan_types ||= []
    @orphan_types.concat(types)
  else
    if defined?(@orphan_types)
      all_orphan_types = @orphan_types.dup
      if defined?(super)
        all_orphan_types += super
        all_orphan_types.uniq!
      end
      all_orphan_types
    elsif defined?(super)
      super
    else
      EmptyObjects::EMPTY_ARRAY
    end
  end
end

#resolver_methods(&block) ⇒ Object

Instance methods defined in this block will become class methods on objects that implement this interface. Use it to implement resolve_each:, resolve_batch:, and resolve_static: fields.

Examples

Example: field :thing, String, resolve_static: true

resolver_methods do
  def thing
    Somehow.get.thing
  end
end


52
53
54
55
56
57
58
59
60
# File 'lib/graphql/schema/interface.rb', line 52

def resolver_methods(&block)
  if !defined?(@_resolver_methods)
    resolver_methods_module = Module.new
    @_resolver_methods = resolver_methods_module
    const_set(:ResolverMethods, resolver_methods_module)
    extend(resolver_methods_module)
  end
  const_get(:ResolverMethods).module_exec(&block)
end

#type_membership_class(membership_class = nil) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/graphql/schema/interface.rb', line 67

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) ⇒ Boolean

See Schema::Warden hides interfaces without visible implementations

Returns:



63
64
65
# File 'lib/graphql/schema/interface.rb', line 63

def visible?(context)
  true
end