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 Object and Interface

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, :method]
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)

Instance Method Summary collapse

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.

Parameters:



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

def add_field(field_defn)
  if CONFLICT_FIELD_NAMES.include?(field_defn.resolver_method) && field_defn.original_name == field_defn.resolver_method && field_defn.method_conflict_warning?
    warn "#{self.graphql_name}'s `field :#{field_defn.name}` conflicts with a built-in method, use `resolver_method:` to pick a different resolver method for this field (for example, `resolver_method: :resolve_#{field_defn.resolver_method}` and `def resolve_#{field_defn.resolver_method}`). Or use `method_conflict_warning: false` to suppress this warning."
  end
  own_fields[field_defn.name] = field_defn
  nil
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



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

def field_class(new_field_class = nil)
  if new_field_class
    @field_class = new_field_class
  elsif defined?(@field_class) && @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



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

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.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/graphql/schema/member/has_fields.rb', line 29

def get_field(field_name)
  if (f = own_fields[field_name])
    f
  else
    for ancestor in ancestors
      if ancestor.respond_to?(:own_fields) && f = ancestor.own_fields[field_name]
        return f
      end
    end
    nil
  end
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.



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

def global_id_field(field_name)
  id_resolver = GraphQL::Relay::GlobalIdResolve.new(type: self)
  field field_name, "ID", null: false
  define_method(field_name) do
    id_resolver.call(object, {}, context)
  end
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:



92
93
94
# File 'lib/graphql/schema/member/has_fields.rb', line 92

def own_fields
  @own_fields ||= {}
end