Module: GraphQL::Schema::Member::HasFields Private

Included in:
Interface::DefinitionMethods, GraphQL::Schema::Mutation, Object
Defined in:
lib/graphql/schema/member/has_fields.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Shared code for Object and Interface

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_default_resolve_module(child_class) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a module which will have instance methods for implementing fields. These will be super methods for fields in interfaces, objects and mutations. Use an instance variable on the class instead of a constant so that module namespaces won’t be an issue. (If we used constants, child_class::DefaultResolve might find a constant from an included module.)



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/graphql/schema/member/has_fields.rb', line 20

def add_default_resolve_module(child_class)
  if child_class.instance_variable_get(:@_default_resolve)
    # This can happen when an object implements an interface,
    # since that interface has the `included` hook above.
    return
  end

  default_resolve_module = Module.new
  child_class.instance_variable_set(:@_default_resolve, default_resolve_module)
  child_class.include(default_resolve_module)
end

.extended(child_class) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

When this module is added to a class, add a place for that class’s default behaviors



10
11
12
13
# File 'lib/graphql/schema/member/has_fields.rb', line 10

def self.extended(child_class)
  add_default_resolve_module(child_class)
  super
end

Instance Method Details

#add_field(field_defn) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Register this field with the class, overriding a previous one if needed. Also, add a parent method for resolving this field.

Parameters:



82
83
84
85
86
87
88
89
# File 'lib/graphql/schema/member/has_fields.rb', line 82

def add_field(field_defn)
  own_fields[field_defn.name] = field_defn
  if !method_defined?(field_defn.method_sym)
    # Only add the super method if there isn't one already.
    add_super_method(field_defn.name.inspect, field_defn.method_sym)
  end
  nil
end

#field(*args, **kwargs, &block) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Add a field to this object or interface with the given definition

See Also:

  • for method signature


51
52
53
54
55
# File 'lib/graphql/schema/member/has_fields.rb', line 51

def field(*args, **kwargs, &block)
  field_defn = field_class.from_options(*args, owner: self, **kwargs, &block)
  add_field(field_defn)
  nil
end

#field_class(new_field_class = nil) ⇒ Class

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The class to initialize when adding fields to this kind of schema member.

Returns:

  • (Class)

    The class to initialize when adding fields to this kind of schema member



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/graphql/schema/member/has_fields.rb', line 92

def field_class(new_field_class = nil)
  if new_field_class
    @field_class = new_field_class
  elsif @field_class
    @field_class
  elsif self.is_a?(Class)
    superclass.respond_to?(:field_class) ? superclass.field_class : GraphQL::Schema::Field
  else
    ancestor = ancestors[1..-1].find { |a| a.respond_to?(:field_class) && a.field_class }
    ancestor ? ancestor.field_class : GraphQL::Schema::Field
  end
end

#fieldsHash<String => GraphQL::Schema::Field>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Fields on this object, keyed by name, including inherited fields.

Returns:

  • (Hash<String => GraphQL::Schema::Field>)

    Fields on this object, keyed by name, including inherited fields



58
59
60
61
62
63
64
65
66
67
# File 'lib/graphql/schema/member/has_fields.rb', line 58

def fields
  # Local overrides take precedence over inherited fields
  all_fields = {}
  ancestors.reverse_each do |ancestor|
    if ancestor.respond_to?(:own_fields)
      all_fields.merge!(ancestor.own_fields)
    end
  end
  all_fields
end

#get_field(field_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



69
70
71
72
73
74
75
76
# File 'lib/graphql/schema/member/has_fields.rb', line 69

def get_field(field_name)
  for ancestor in ancestors
    if ancestor.respond_to?(:own_fields) && f = ancestor.own_fields[field_name]
      return f
    end
  end
  nil
end

#global_id_field(field_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



105
106
107
# File 'lib/graphql/schema/member/has_fields.rb', line 105

def global_id_field(field_name)
  field field_name, "ID", null: false, resolve: GraphQL::Relay::GlobalIdResolve.new(type: self)
end

#included(child_class) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

When this is included into interfaces, add a place for default field behaviors



35
36
37
38
39
# File 'lib/graphql/schema/member/has_fields.rb', line 35

def included(child_class)
  HasFields.add_default_resolve_module(child_class)
  # Also, prepare a place for default field implementations
  super
end

#inherited(child_class) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

When a subclass of objects are created, add a place for that subclass’s default field behaviors



43
44
45
46
# File 'lib/graphql/schema/member/has_fields.rb', line 43

def inherited(child_class)
  HasFields.add_default_resolve_module(child_class)
  super
end

#own_fieldsArray<GraphQL::Schema::Field>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Fields defined on this class specifically, not parent classes.

Returns:



110
111
112
# File 'lib/graphql/schema/member/has_fields.rb', line 110

def own_fields
  @own_fields ||= {}
end