Module: GraphQL::Client::Schema::ObjectType
- Defined in:
- lib/graphql/client/schema/object_type.rb
Constant Summary collapse
- PREDICATE_CACHE =
Hash.new { |h, name| h[name] = -> { @data[name] ? true : false } }
- METHOD_CACHE =
Hash.new { |h, key| h[key] = -> { name = key.to_s type = self.class::FIELDS[key] @casted_data.fetch(name) do @casted_data[name] = type.cast(@data[name], @errors.filter_by_path(name)) end } }
- MODULE_CACHE =
Hash.new do |h, fields| h[fields] = Module.new do fields.each do |name| GraphQL::Client::Schema::ObjectType.define_cached_field(name, self) end end end
- FIELDS_CACHE =
Hash.new { |h, k| h[k] = k }
Class Method Summary collapse
Instance Method Summary collapse
- #cast(value, errors) ⇒ Object
- #define_class(definition, ast_nodes) ⇒ Object
- #define_field(name, type) ⇒ Object
- #define_fields(fields) ⇒ Object
Class Method Details
.define_cached_field(name, ctx) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/graphql/client/schema/object_type.rb', line 92 def self.define_cached_field(name, ctx) key = name name = -name.to_s method_name = ActiveSupport::Inflector.underscore(name) ctx.send(:define_method, method_name, &METHOD_CACHE[key]) ctx.send(:define_method, "#{method_name}?", &PREDICATE_CACHE[name]) end |
.new(type, fields = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/graphql/client/schema/object_type.rb', line 12 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
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/graphql/client/schema/object_type.rb', line 116 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, ast_nodes) ⇒ Object
22 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 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/graphql/client/schema/object_type.rb', line 22 def define_class(definition, ast_nodes) # First, gather all the ast nodes representing a certain selection, by name. # We gather AST nodes into arrays so that multiple selections can be grouped, for example: # # { # f1 { a b } # f1 { b c } # } # # should be treated like `f1 { a b c }` field_nodes = {} ast_nodes.each do |ast_node| ast_node.selections.each do |selected_ast_node| gather_selections(field_nodes, definition, selected_ast_node) end end # After gathering all the nodes by name, prepare to create methods and classes for them. field_classes = {} field_nodes.each do |result_name, field_ast_nodes| # `result_name` might be an alias, so make sure to get the proper name field_name = field_ast_nodes.first.name field_definition = definition.client.schema.get_field(type.name, field_name) field_return_type = field_definition.type field_classes[result_name.to_sym] = schema_module.define_class(definition, field_ast_nodes, field_return_type) end klass = Class.new(self) klass.define_fields(field_classes) klass.instance_variable_set(:@source_definition, definition) klass.instance_variable_set(:@_spreads, definition.indexes[:spreads][ast_nodes.first]) if definition.client.enforce_collocated_callers keys = field_classes.keys.map { |key| ActiveSupport::Inflector.underscore(key) } Client.enforce_collocated_callers(klass, keys, definition.source_location[0]) end klass end |
#define_field(name, type) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/graphql/client/schema/object_type.rb', line 101 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
86 87 88 89 90 |
# File 'lib/graphql/client/schema/object_type.rb', line 86 def define_fields(fields) const_set :FIELDS, FIELDS_CACHE[fields] mod = MODULE_CACHE[fields.keys.sort] include mod end |