Module: GraphqlPagination::CollectionField
- Defined in:
- lib/graphql_pagination/collection_field.rb
Instance Method Summary collapse
-
#calculate_complexity(query:, nodes:, child_complexity:) ⇒ Object
Calculate complexity for collection fields This is based on graphql-ruby’s connection complexity calculation See: github.com/rmosolgo/graphql-ruby/blob/master/lib/graphql/schema/field.rb#L472.
-
#collection_type? ⇒ Boolean
Check if the field returns a collection type.
Instance Method Details
#calculate_complexity(query:, nodes:, child_complexity:) ⇒ Object
Calculate complexity for collection fields This is based on graphql-ruby’s connection complexity calculation See: github.com/rmosolgo/graphql-ruby/blob/master/lib/graphql/schema/field.rb#L472
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/graphql_pagination/collection_field.rb', line 15 def calculate_complexity(query:, nodes:, child_complexity:) if collection_type? arguments = query.arguments_for(nodes.first, self) # Get the page size from the `limit` or `per` argument # Default to a reasonable value if not provided max_possible_page_size = arguments[:limit] || arguments[:per] if max_possible_page_size.nil? # Use schema defaults or a reasonable default # Note: default_page_size, max_page_size are GraphQL::Schema::Field methods # query.schema.default_page_size and query.schema.default_max_page_size are schema-level configs max_possible_page_size = default_page_size || query.schema.default_page_size || max_page_size || query.schema.default_max_page_size || 25 # Reasonable default for kaminari end = 0 lookahead = GraphQL::Execution::Lookahead.new(query:, field: self, ast_nodes: nodes, owner_type: owner) # Calculate metadata complexity if ( = lookahead.selection(:metadata)).selected? += 1 # metadata field itself += .selections.size # metadata subfields end # Check for total_pages and total_count (they are part of metadata) # Note: These are already counted above if metadata is selected nodes_edges_complexity = 0 nodes_edges_complexity += 1 if lookahead.selects?(:collection) # Calculate items complexity # Subtract metadata and collection field complexity from child complexity items_complexity = child_complexity - - nodes_edges_complexity # Apply complexity: 1 (this field) + (page_size * items) + metadata + collection 1 + (max_possible_page_size * items_complexity) + + nodes_edges_complexity else super end end |
#collection_type? ⇒ Boolean
Check if the field returns a collection type
4 5 6 7 8 9 10 |
# File 'lib/graphql_pagination/collection_field.rb', line 4 def collection_type? if @return_type_expr.respond_to?(:graphql_name) @return_type_expr.graphql_name.end_with?("Collection") else false end end |