Class: GraphQL::Connection

Inherits:
Node
  • Object
show all
Defined in:
lib/graphql/connection.rb

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.

Examples:

class UpvotesConnection < GraphQL::Collection
  field.number(:count)
  field.boolean(:any)

  def count
    items.count
  end

  def any
    items.any?
  end
end

# Then, this connection will be used for connections whose names match:
class PostNode < GraphQL::Node
  field.connection(:upvotes)
  # ^^ uses `UpvotesConnection` based on naming convention
end

# And you can use the fields in a query:
<<QUERY
find_post(10) {
  title,
  upvotes {
    count,
    any,
    edges {
      node { created_at }
    }
  }
}
QUERY

Direct Known Subclasses

Introspection::Connection

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Node

#target

Class Method Summary collapse

Instance Method Summary collapse

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_connectionObject

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

#callsObject (readonly)

Returns the value of attribute calls.



46
47
48
# File 'lib/graphql/connection.rb', line 46

def calls
  @calls
end

#queryObject (readonly)

Returns the value of attribute query.



46
47
48
# File 'lib/graphql/connection.rb', line 46

def query
  @query
end

#syntax_fieldsObject (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_nameObject



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_fieldsObject



59
60
61
# File 'lib/graphql/connection.rb', line 59

def edge_fields
  @edge_fields ||= syntax_fields.find { |f| f.identifier == "edges" }.fields
end

#edgesObject



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

#itemsObject

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