module GraphQL::Types::Relay::ConnectionBehaviors::ClassMethods

Attributes

edge_class[R]

@return [Class]

node_type[R]

@return [Class]

Public Instance Methods

accessible?(ctx) click to toggle source
# File lib/graphql/types/relay/connection_behaviors.rb, line 80
def accessible?(ctx)
  node_type.accessible?(ctx)
end
authorized?(obj, ctx) click to toggle source
# File lib/graphql/types/relay/connection_behaviors.rb, line 76
def authorized?(obj, ctx)
  true # Let nodes be filtered out
end
edge_nullable(new_value = nil) click to toggle source

Set the default ‘edge_nullable` for this class and its child classes. (Defaults to `true`.) Use `edge_nullable(false)` in your base class to make non-null `edge` fields.

# File lib/graphql/types/relay/connection_behaviors.rb, line 111
def edge_nullable(new_value = nil)
  if new_value.nil?
    defined?(@edge_nullable) ? @edge_nullable : superclass.edge_nullable
  else
    @edge_nullable = new_value
  end
end
edge_type(edge_type_class, edge_class: GraphQL::Pagination::Connection::Edge, node_type: edge_type_class.node_type, nodes_field: self.has_nodes_field, node_nullable: self.node_nullable, edges_nullable: self.edges_nullable, edge_nullable: self.edge_nullable, field_options: nil) click to toggle source

Configure this connection to return ‘edges` and `nodes` based on `edge_type_class`.

This method will use the inputs to create:

  • ‘edges` field

  • ‘nodes` field

  • description

It’s called when you subclass this base connection, trying to use the class name to set defaults. You can call it again in the class definition to override the default (or provide a value, if the default lookup failed). @param field_options [Hash] Any extra keyword arguments to pass to the ‘field :edges, …` and `field :nodes, …` configurations

# File lib/graphql/types/relay/connection_behaviors.rb, line 39
def edge_type(edge_type_class, edge_class: GraphQL::Pagination::Connection::Edge, node_type: edge_type_class.node_type, nodes_field: self.has_nodes_field, node_nullable: self.node_nullable, edges_nullable: self.edges_nullable, edge_nullable: self.edge_nullable, field_options: nil)
  # Set this connection's graphql name
  node_type_name = node_type.graphql_name

  @node_type = node_type
  @edge_type = edge_type_class
  @edge_class = edge_class

  base_field_options = {
    name: :edges,
    type: [edge_type_class, null: edge_nullable],
    null: edges_nullable,
    description: "A list of edges.",
    connection: false,
  }

  if field_options
    base_field_options.merge!(field_options)
  end

  field(**base_field_options)

  define_nodes_field(node_nullable, field_options: field_options) if nodes_field

  description("The connection type for #{node_type_name}.")
end
edges_nullable(new_value = nil) click to toggle source

Set the default ‘edges_nullable` for this class and its child classes. (Defaults to `true`.) Use `edges_nullable(false)` in your base class to make non-null `edges` fields.

# File lib/graphql/types/relay/connection_behaviors.rb, line 101
def edges_nullable(new_value = nil)
  if new_value.nil?
    defined?(@edges_nullable) ? @edges_nullable : superclass.edges_nullable
  else
    @edges_nullable = new_value
  end
end
has_nodes_field(new_value = nil) click to toggle source

Set the default ‘nodes_field` for this class and its child classes. (Defaults to `true`.) Use `nodes_field(false)` in your base class to prevent adding of a nodes field.

# File lib/graphql/types/relay/connection_behaviors.rb, line 121
def has_nodes_field(new_value = nil)
  if new_value.nil?
    defined?(@nodes_field) ? @nodes_field : superclass.has_nodes_field
  else
    @nodes_field = new_value
  end
end
node_nullable(new_value = nil) click to toggle source

Set the default ‘node_nullable` for this class and its child classes. (Defaults to `true`.) Use `node_nullable(false)` in your base class to make non-null `node` and `nodes` fields.

# File lib/graphql/types/relay/connection_behaviors.rb, line 91
def node_nullable(new_value = nil)
  if new_value.nil?
    defined?(@node_nullable) ? @node_nullable : superclass.node_nullable
  else
    @node_nullable = new_value
  end
end
nodes_field(node_nullable: self.node_nullable, field_options: nil) click to toggle source

Add the shortcut ‘nodes` field to this connection and its subclasses

# File lib/graphql/types/relay/connection_behaviors.rb, line 72
def nodes_field(node_nullable: self.node_nullable, field_options: nil)
  define_nodes_field(node_nullable, field_options: field_options)
end
scope_items(items, context) click to toggle source

Filter this list according to the way its node type would scope them

# File lib/graphql/types/relay/connection_behaviors.rb, line 67
def scope_items(items, context)
  node_type.scope_items(items, context)
end
visible?(ctx) click to toggle source
Calls superclass method
# File lib/graphql/types/relay/connection_behaviors.rb, line 84
def visible?(ctx)
  # if this is an abstract base class, there may be no `node_type`
  node_type ? node_type.visible?(ctx) : super
end

Private Instance Methods

define_nodes_field(nullable, field_options: nil) click to toggle source
# File lib/graphql/types/relay/connection_behaviors.rb, line 131
def define_nodes_field(nullable, field_options: nil)
  base_field_options = {
    name: :nodes,
    type: [@node_type, null: nullable],
    null: nullable,
    description: "A list of nodes.",
    connection: false,
  }
  if field_options
    base_field_options.merge!(field_options)
  end
  field(**base_field_options)
end