Class: GraphQL::CollectionEdge
- Inherits:
-
Object
- Object
- GraphQL::CollectionEdge
- Defined in:
- lib/graphql/collection_edge.rb
Instance Attribute Summary collapse
-
#calls ⇒ Object
Returns the value of attribute calls.
-
#edge_class ⇒ Object
Returns the value of attribute edge_class.
-
#fields ⇒ Object
Returns the value of attribute fields.
Instance Method Summary collapse
- #apply_calls(unfiltered_items, call_hash) ⇒ Object
- #count ⇒ Object
- #edges(fields:) ⇒ Object
-
#initialize(items:, edge_class:) ⇒ CollectionEdge
constructor
A new instance of CollectionEdge.
- #safe_send(identifier) ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(items:, edge_class:) ⇒ CollectionEdge
4 5 6 7 |
# File 'lib/graphql/collection_edge.rb', line 4 def initialize(items:, edge_class:) @items = items @edge_class = edge_class end |
Instance Attribute Details
#calls ⇒ Object
Returns the value of attribute calls.
2 3 4 |
# File 'lib/graphql/collection_edge.rb', line 2 def calls @calls end |
#edge_class ⇒ Object
Returns the value of attribute edge_class.
2 3 4 |
# File 'lib/graphql/collection_edge.rb', line 2 def edge_class @edge_class end |
#fields ⇒ Object
Returns the value of attribute fields.
2 3 4 |
# File 'lib/graphql/collection_edge.rb', line 2 def fields @fields end |
Instance Method Details
#apply_calls(unfiltered_items, call_hash) ⇒ Object
26 27 28 29 |
# File 'lib/graphql/collection_edge.rb', line 26 def apply_calls(unfiltered_items, call_hash) # override this to apply calls to your items unfiltered_items end |
#count ⇒ Object
22 23 24 |
# File 'lib/graphql/collection_edge.rb', line 22 def count @items.count end |
#edges(fields:) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/graphql/collection_edge.rb', line 31 def edges(fields:) filtered_items = apply_calls(items, calls) filtered_items.map do |item| node = edge_class.new(item) json = {} fields.each do |field| name = field.identifier if name == "node" # it's magic node.fields = field.fields json[name] = node.to_json else json[name] = node.safe_send(name) end end json end end |
#safe_send(identifier) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/graphql/collection_edge.rb', line 49 def safe_send(identifier) if respond_to?(identifier) public_send(identifier) else raise GraphQL::FieldNotDefinedError, "#{self.class.name}##{identifier} was requested, but it isn't defined." end end |
#to_json ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/graphql/collection_edge.rb', line 9 def to_json json = {} fields.each do |field| name = field.identifier if name == "edges" json["edges"] = edges(fields: field.fields) else json[name] = safe_send(name) end end json end |