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.



84
85
86
87
88
# File 'lib/graphql/client/schema/object_type.rb', line 84

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



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

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.



100
101
102
# File 'lib/graphql/client/schema/object_type.rb', line 100

def errors
  @errors
end

Instance Method Details

#inspectObject



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

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



93
94
95
# File 'lib/graphql/client/schema/object_type.rb', line 93

def to_h
  @data
end