Class: GraphQL::Connection
Overview
Connections wrap collections of objects.
Out of the box, the only field it has is ‘edges`, which provides access to the members of the collection.
You can define a custom Connection to use. This allows you to define fields at the collection level (rather than the item level)
Custom fields can access the collection as Field#items.
Direct Known Subclasses
Class Attribute Summary collapse
-
.default_connection ⇒ Object
Returns the value of attribute default_connection.
Instance Attribute Summary collapse
-
#calls ⇒ Object
readonly
Returns the value of attribute calls.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#syntax_fields ⇒ Object
readonly
Returns the value of attribute syntax_fields.
Attributes inherited from Node
Class Method Summary collapse
-
.default_connection! ⇒ Object
Call this to make a the class the default connection when one isn’t found by name.
- .default_schema_name ⇒ Object
Instance Method Summary collapse
- #edge_fields ⇒ Object
- #edges ⇒ Object
-
#initialize(items, query:, fields: []) ⇒ Connection
constructor
A new instance of Connection.
-
#items ⇒ Object
Returns the members of the collection, after any calls on the corresponding Field have been applied.
Methods inherited from Node
all_fields, #as_result, #context, cursor, desc, description, exposes, field, inherited, #method_missing, own_fields, remove_field, ruby_class_name, schema_name, type
Constructor Details
#initialize(items, query:, fields: []) ⇒ Connection
Returns a new instance of Connection.
48 49 50 51 52 |
# File 'lib/graphql/connection.rb', line 48 def initialize(items, query:, fields: []) @target = items @syntax_fields = fields @query = query end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class GraphQL::Node
Class Attribute Details
.default_connection ⇒ Object
Returns the value of attribute default_connection.
78 79 80 |
# File 'lib/graphql/connection.rb', line 78 def default_connection @default_connection end |
Instance Attribute Details
#calls ⇒ Object (readonly)
Returns the value of attribute calls.
46 47 48 |
# File 'lib/graphql/connection.rb', line 46 def calls @calls end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
46 47 48 |
# File 'lib/graphql/connection.rb', line 46 def query @query end |
#syntax_fields ⇒ Object (readonly)
Returns the value of attribute syntax_fields.
46 47 48 |
# File 'lib/graphql/connection.rb', line 46 def syntax_fields @syntax_fields end |
Class Method Details
.default_connection! ⇒ Object
Call this to make a the class the default connection when one isn’t found by name.
81 82 83 |
# File 'lib/graphql/connection.rb', line 81 def default_connection! GraphQL::Connection.default_connection = self end |
.default_schema_name ⇒ Object
74 75 76 |
# File 'lib/graphql/connection.rb', line 74 def default_schema_name name.split("::").last.sub(/Connection$/, '').underscore end |
Instance Method Details
#edge_fields ⇒ Object
59 60 61 |
# File 'lib/graphql/connection.rb', line 59 def edge_fields @edge_fields ||= syntax_fields.find { |f| f.identifier == "edges" }.fields end |
#edges ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/graphql/connection.rb', line 63 def edges raise "#{self.class} expected a connection, but got `nil`" if items.nil? items.map do |item| node_class = GraphQL::SCHEMA.type_for_object(item) node = node_class.new(item, fields: edge_fields, query: query) res = node.as_result res end end |
#items ⇒ Object
Returns the members of the collection, after any calls on the corresponding Field have been applied
55 56 57 |
# File 'lib/graphql/connection.rb', line 55 def items @target end |