Module: GraphQL::Types::Relay::ConnectionBehaviors::ClassMethods

Defined in:
lib/graphql/types/relay/connection_behaviors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#edge_class ⇒ Object

Returns

  • Class


63
64
65
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 63

def edge_class
  @edge_class
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) ⇒ Object

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).

Parameters

  • field_options (Hash) — Any extra keyword arguments to pass to the field :edges, ... and field :nodes, ... configurations

:call-seq:

edge_type(edge_type_class, edge_class:, node_type:, nodes_field:, node_nullable:, edges_nullable:, edge_nullable:, Hash field_options:)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 82

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.",
    scope: false, # Assume that the connection was already scoped.
    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

#node_type ⇒ Object

Returns

  • Class


58
59
60
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 58

def node_type
  @node_type
end

Instance Method Details

#authorized?(obj, ctx) ⇒ Boolean

Returns:



133
134
135
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 133

def authorized?(obj, ctx)
  true # Let nodes be filtered out
end

#default_broadcastable(new_value) ⇒ Object



51
52
53
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 51

def default_broadcastable(new_value)
  @default_broadcastable = new_value
end

#default_broadcastable? ⇒ Boolean

Returns:



47
48
49
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 47

def default_broadcastable?
  @default_broadcastable
end

#default_relay? ⇒ Boolean

Returns:



43
44
45
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 43

def default_relay?
  true
end

#edge_nullable(new_value = nil) ⇒ Object

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.



164
165
166
167
168
169
170
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 164

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

#edges_nullable(new_value = nil) ⇒ Object

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.



154
155
156
157
158
159
160
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 154

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) ⇒ Object

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.



174
175
176
177
178
179
180
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 174

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

#inherited(child_class) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 31

def inherited(child_class)
  super
  child_class.has_nodes_field(has_nodes_field)
  child_class.node_nullable(node_nullable)
  child_class.edges_nullable(edges_nullable)
  child_class.edge_nullable(edge_nullable)
  child_class.edge_type = nil
  child_class.node_type = nil
  child_class.edge_class = nil
  child_class.default_broadcastable(default_broadcastable?)
end

#node_nullable(new_value = nil) ⇒ Object

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.



144
145
146
147
148
149
150
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 144

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) ⇒ Object

Add the shortcut nodes field to this connection and its subclasses



129
130
131
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 129

def nodes_field(node_nullable: self.node_nullable, field_options: nil)
  define_nodes_field(node_nullable, field_options: field_options)
end

#reauthorize_scoped_objects(new_value = nil) ⇒ Object

The connection will skip auth on its nodes if the node_type is configured for that



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 116

def reauthorize_scoped_objects(new_value = nil)
  if new_value.nil?
    if @reauthorize_scoped_objects != nil
      @reauthorize_scoped_objects
    else
      node_type.reauthorize_scoped_objects
    end
  else
    @reauthorize_scoped_objects = new_value
  end
end

#scope_items(items, context) ⇒ Object

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



111
112
113
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 111

def scope_items(items, context)
  node_type.scope_items(items, context)
end

#visible?(ctx) ⇒ Boolean

Returns:



137
138
139
140
# File 'lib/graphql/types/relay/connection_behaviors.rb', line 137

def visible?(ctx)
  # if this is an abstract base class, there may be no `node_type`
  node_type ? node_type.visible?(ctx) : super
end