Class: GraphQL::Client::Schema::ObjectClass

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/client/schema/object_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, errors = Errors.new) ⇒ ObjectClass

Returns a new instance of ObjectClass.



81
82
83
84
85
# File 'lib/graphql/client/schema/object_type.rb', line 81

def initialize(data = {}, errors = Errors.new)
  @data = data
  @casted_data = {}
  @errors = errors
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/graphql/client/schema/object_type.rb', line 99

def method_missing(*args)
  super
rescue NoMethodError => e
  type = self.class.type

  if ActiveSupport::Inflector.underscore(e.name.to_s) != e.name.to_s
    raise e
  end

  field = type.all_fields.find do |f|
    f.name == e.name.to_s || ActiveSupport::Inflector.underscore(f.name) == e.name.to_s
  end

  unless field
    raise UnimplementedFieldError, "undefined field `#{e.name}' on #{type} type. https://git.io/v1y3m"
  end

  if @data.key?(field.name)
    error_class = ImplicitlyFetchedFieldError
    message = "implicitly fetched field `#{field.name}' on #{type} type. https://git.io/v1yGL"
  else
    error_class = UnfetchedFieldError
    message = "unfetched field `#{field.name}' on #{type} type. https://git.io/v1y3U"
  end

  raise error_class, message
end

Instance Attribute Details

#errorsObject (readonly)

Public: Return errors associated with data.

Returns Errors collection.



97
98
99
# File 'lib/graphql/client/schema/object_type.rb', line 97

def errors
  @errors
end

Instance Method Details

#inspectObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/graphql/client/schema/object_type.rb', line 127

def inspect
  parent = self.class.ancestors.select { |m| m.is_a?(ObjectType) }.last

  ivars = @data.map { |key, value|
    if value.is_a?(Hash) || value.is_a?(Array)
      "#{key}=..."
    else
      "#{key}=#{value.inspect}"
    end
  }

  buf = "#<#{parent.name}".dup
  buf << " " << ivars.join(" ") if ivars.any?
  buf << ">"
  buf
end

#to_hObject

Public: Returns the raw response data

Returns Hash



90
91
92
# File 'lib/graphql/client/schema/object_type.rb', line 90

def to_h
  @data
end