Class: GraphQL::Pagination::Connections
- Inherits:
-
Object
- Object
- GraphQL::Pagination::Connections
- Defined in:
- lib/graphql/pagination/connections.rb
Overview
A schema-level connection wrapper manager.
Attach as a plugin.
Defined Under Namespace
Classes: ImplementationMissingError
Instance Method Summary collapse
-
#add(nodes_class, implementation) ⇒ Object
-
#all_wrappers ⇒ Object
-
#delete(nodes_class) ⇒ Object
-
#edge_class_for_field(field) ⇒ Object
private
use an override if there is one.
-
#initialize(schema:) ⇒ Connections
constructor
A new instance of Connections.
-
#populate_connection(field, object, value, original_arguments, context) ⇒ Object
-
#wrap(field, parent, items, arguments, context) ⇒ Object
Used by the runtime to wrap values in connection wrappers.
-
#wrapper_for(items, wrappers: all_wrappers) ⇒ Object
Constructor Details
#initialize(schema:) ⇒ Connections
Returns a new instance of Connections.
24 25 26 27 28 |
# File 'lib/graphql/pagination/connections.rb', line 24 def initialize(schema:) @schema = schema @wrappers = {} add_default end |
Instance Method Details
#add(nodes_class, implementation) ⇒ Object
30 31 32 |
# File 'lib/graphql/pagination/connections.rb', line 30 def add(nodes_class, implementation) @wrappers[nodes_class] = implementation end |
#all_wrappers ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/graphql/pagination/connections.rb', line 38 def all_wrappers all_wrappers = {} @schema.ancestors.reverse_each do |schema_class| if schema_class.respond_to?(:connections) && (c = schema_class.connections) all_wrappers.merge!(c.wrappers) end end all_wrappers end |
#delete(nodes_class) ⇒ Object
34 35 36 |
# File 'lib/graphql/pagination/connections.rb', line 34 def delete(nodes_class) @wrappers.delete(nodes_class) end |
#edge_class_for_field(field) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
use an override if there is one
120 121 122 123 124 125 126 127 128 |
# File 'lib/graphql/pagination/connections.rb', line 120 def edge_class_for_field(field) conn_type = field.type.unwrap conn_type_edge_type = conn_type.respond_to?(:edge_class) && conn_type.edge_class if conn_type_edge_type && conn_type_edge_type != Pagination::Connection::Edge conn_type_edge_type else nil end end |
#populate_connection(field, object, value, original_arguments, context) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/graphql/pagination/connections.rb', line 86 def populate_connection(field, object, value, original_arguments, context) if value.is_a? GraphQL::ExecutionError # This isn't even going to work because context doesn't have ast_node anymore context.add_error(value) nil elsif value.nil? nil elsif value.is_a?(GraphQL::Pagination::Connection) # update the connection with some things that may not have been provided value.context ||= context value.parent ||= object value.first_value ||= original_arguments[:first] value.after_value ||= original_arguments[:after] value.last_value ||= original_arguments[:last] value.before_value ||= original_arguments[:before] value.arguments ||= original_arguments # rubocop:disable Development/ContextIsPassedCop -- unrelated .arguments method value.field ||= field if field.has_max_page_size? && !value.has_max_page_size_override? value.max_page_size = field.max_page_size end if field.has_default_page_size? && !value.has_default_page_size_override? value.default_page_size = field.default_page_size end if (custom_t = context.schema.connections.edge_class_for_field(field)) value.edge_class = custom_t end value else context.namespace(:connections)[:all_wrappers] ||= context.schema.connections.all_wrappers context.schema.connections.wrap(field, object, value, original_arguments, context) end end |
#wrap(field, parent, items, arguments, context) ⇒ Object
Used by the runtime to wrap values in connection wrappers.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/graphql/pagination/connections.rb', line 61 def wrap(field, parent, items, arguments, context) return items if GraphQL::Execution::Interpreter::RawValue === items wrappers = context ? context.namespace(:connections)[:all_wrappers] : all_wrappers impl = wrapper_for(items, wrappers: wrappers) if impl impl.new( items, context: context, parent: parent, field: field, max_page_size: field.has_max_page_size? ? field.max_page_size : context.schema.default_max_page_size, default_page_size: field.has_default_page_size? ? field.default_page_size : context.schema.default_page_size, first: arguments[:first], after: arguments[:after], last: arguments[:last], before: arguments[:before], arguments: arguments, edge_class: edge_class_for_field(field), ) else raise ImplementationMissingError, "Couldn't find a connection wrapper for #{items.class} during #{field.path} (#{items.inspect})" end end |
#wrapper_for(items, wrappers: all_wrappers) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/graphql/pagination/connections.rb', line 48 def wrapper_for(items, wrappers: all_wrappers) impl = nil items.class.ancestors.each { |cls| impl = wrappers[cls] break if impl } impl end |