Module: GraphQL::Client::Schema::ObjectType
- Defined in:
- lib/graphql/client/schema/object_type.rb
Class Method Summary collapse
Instance Method Summary collapse
- #cast(value, errors) ⇒ Object
- #define_class(definition, irep_node) ⇒ Object
- #define_field(name, type) ⇒ Object
- #define_fields(fields) ⇒ Object
Class Method Details
.new(type, fields = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/graphql/client/schema/object_type.rb', line 13 def self.new(type, fields = {}) Class.new(ObjectClass) do extend BaseType extend ObjectType define_singleton_method(:type) { type } define_singleton_method(:fields) { fields } end end |
Instance Method Details
#cast(value, errors) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/graphql/client/schema/object_type.rb', line 71 def cast(value, errors) case value when Hash new(value, errors) when NilClass nil else raise InvariantError, "expected value to be a Hash, but was #{value.class}" end end |
#define_class(definition, irep_node) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/graphql/client/schema/object_type.rb', line 23 def define_class(definition, irep_node) fields = irep_node.typed_children[type].inject({}) { |h, (field_name, field_irep_node)| definition_for_field = definition.indexes[:definitions][field_irep_node.ast_node] # Ignore fields defined in other documents. if definition.source_document.definitions.include?(definition_for_field) h[field_name.to_sym] = schema_module.define_class(definition, field_irep_node, field_irep_node.definition.type) end h } Class.new(self) do define_fields(fields) if definition.client.enforce_collocated_callers keys = fields.keys.map { |key| ActiveSupport::Inflector.underscore(key) } Client.enforce_collocated_callers(self, keys, definition.source_location[0]) end class << self attr_reader :source_definition attr_reader :_spreads end @source_definition = definition @_spreads = definition.indexes[:spreads][irep_node.ast_node] end end |
#define_field(name, type) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/graphql/client/schema/object_type.rb', line 56 def define_field(name, type) name = name.to_s method_name = ActiveSupport::Inflector.underscore(name) define_method(method_name) do @casted_data.fetch(name) do @casted_data[name] = type.cast(@data[name], @errors.filter_by_path(name)) end end define_method("#{method_name}?") do @data[name] ? true : false end end |
#define_fields(fields) ⇒ Object
52 53 54 |
# File 'lib/graphql/client/schema/object_type.rb', line 52 def define_fields(fields) fields.each { |name, type| define_field(name, type) } end |