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

Included in:
Interface::DefinitionMethods, GraphQL::Schema::Mutation, Object, Subscription
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 Objects, Interfaces, Mutations, Subscriptions

Defined Under Namespace

Modules: InterfaceMethods, ObjectMethods

Constant Summary collapse

RUBY_KEYWORDS =

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

A list of Ruby keywords.

[:class, :module, :def, :undef, :begin, :rescue, :ensure, :end, :if, :unless, :then, :elsif, :else, :case, :when, :while, :until, :for, :break, :next, :redo, :retry, :in, :do, :return, :yield, :super, :self, :nil, :true, :false, :and, :or, :not, :alias, :defined?, :BEGIN, :END, :__LINE__, :__FILE__]
GRAPHQL_RUBY_KEYWORDS =

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

A list of GraphQL-Ruby keywords.

[:context, :object, :raw_value]
CONFLICT_FIELD_NAMES =

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

A list of field names that we should advise users to pick a different resolve method name.

Set.new(GRAPHQL_RUBY_KEYWORDS + RUBY_KEYWORDS + Object.instance_methods)

Instance Method Summary collapse

Instance Method Details

#add_field(field_defn, method_conflict_warning: field_defn.method_conflict_warning?) ⇒ 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.

Parameters:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/graphql/schema/member/has_fields.rb', line 36

def add_field(field_defn, method_conflict_warning: field_defn.method_conflict_warning?)
  # Check that `field_defn.original_name` equals `resolver_method` and `method_sym` --
  # that shows that no override value was given manually.
  if method_conflict_warning &&
      CONFLICT_FIELD_NAMES.include?(field_defn.resolver_method) &&
      field_defn.original_name == field_defn.resolver_method &&
      field_defn.original_name == field_defn.method_sym &&
      field_defn.hash_key == NOT_CONFIGURED &&
      field_defn.dig_keys.nil?
    warn(conflict_field_name_warning(field_defn))
  end
  prev_defn = own_fields[field_defn.name]

  case prev_defn
  when nil
    own_fields[field_defn.name] = field_defn
  when Array
    prev_defn << field_defn
  when GraphQL::Schema::Field
    own_fields[field_defn.name] = [prev_defn, field_defn]
  else
    raise "Invariant: unexpected previous field definition for #{field_defn.name.inspect}: #{prev_defn.inspect}"
  end

  nil
end

#all_field_definitionsObject

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.



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/graphql/schema/member/has_fields.rb', line 87

def all_field_definitions
  all_fields = {}
  ancestors.reverse_each do |ancestor|
    if ancestor.respond_to?(:own_fields)
      all_fields.merge!(ancestor.own_fields)
    end
  end
  all_fields = all_fields.values
  all_fields.flatten!
  all_fields
end

#field(*args, **kwargs, &block) ⇒ 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.

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

Returns:

See Also:

  • for method signature


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

def field(*args, **kwargs, &block)
  field_defn = field_class.from_options(*args, owner: self, **kwargs, &block)
  add_field(field_defn)
  field_defn
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



64
65
66
67
68
69
70
71
72
# File 'lib/graphql/schema/member/has_fields.rb', line 64

def field_class(new_field_class = nil)
  if new_field_class
    @field_class = new_field_class
  elsif defined?(@field_class) && @field_class
    @field_class
  else
    find_inherited_value(:field_class, GraphQL::Schema::Field)
  end
end

#global_id_field(field_name, **kwargs) ⇒ 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.



74
75
76
77
78
79
80
# File 'lib/graphql/schema/member/has_fields.rb', line 74

def global_id_field(field_name, **kwargs)
  type = self
  field field_name, "ID", **kwargs, null: false
  define_method(field_name) do
    context.schema.id_from_object(object, type, context)
  end
end

#own_fieldsHash<String => GraphQL::Schema::Field, Array<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:



83
84
85
# File 'lib/graphql/schema/member/has_fields.rb', line 83

def own_fields
  @own_fields ||= {}
end