Class: ActiveShopifyGraphQL::Testing::TestLoader
- Defined in:
- lib/active_shopify_graphql/testing/test_loader.rb
Overview
Loader subclass that reads from the in-memory Store instead of executing GraphQL queries. Overrides the three main loading methods and raises on any attempt to perform a real GraphQL query.
Instance Method Summary collapse
-
#load_attributes(id) ⇒ Hash?
Load attributes for a single record by ID.
-
#load_connection_records(_query_name, _variables, parent = nil, connection_config = nil) ⇒ Object, ...
Load records for a connection (lazy-loaded or explicit).
-
#load_paginated_collection(conditions:, per_page:, query_scope:, after: nil, before: nil, sort_key: nil, reverse: nil) ⇒ Response::PaginatedResult
Load a paginated collection matching the given conditions.
-
#perform_graphql_query(_query, **_variables) ⇒ Object
Should never be called — all data comes from the Store.
Methods inherited from Loader
#context, #defined_attributes, #execute_query, #has_included_connections?, #initialization_args, #initialize, #map_response_to_attributes
Constructor Details
This class inherits a constructor from ActiveShopifyGraphQL::Loader
Instance Method Details
#load_attributes(id) ⇒ Hash?
Load attributes for a single record by ID.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/active_shopify_graphql/testing/test_loader.rb', line 13 def load_attributes(id) record = Testing.store.find(@model_class, id) return nil unless record attrs = filter_to_model_attributes(record) populate_connection_cache(record, attrs) if @included_connections.any? attrs end |
#load_connection_records(_query_name, _variables, parent = nil, connection_config = nil) ⇒ Object, ...
Load records for a connection (lazy-loaded or explicit).
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/active_shopify_graphql/testing/test_loader.rb', line 61 def load_connection_records(_query_name, _variables, parent = nil, connection_config = nil) return [] unless parent && connection_config connection_name = connection_config[:original_name] connection_data = Testing.store.connections_for(parent.class, parent.id, connection_name) return (connection_config[:type] == :singular ? nil : []) unless connection_data target_class = connection_config[:class_name].constantize singular = connection_config[:type] == :singular if singular data = connection_data.is_a?(Array) ? connection_data.first : connection_data return nil unless data attrs = filter_to_model_attributes(data, target_class) wire_inverse_of(parent, attrs, connection_config) ModelBuilder.build(target_class, attrs) else items = connection_data.is_a?(Array) ? connection_data : [connection_data] items.filter_map do |item| attrs = filter_to_model_attributes(item, target_class) wire_inverse_of(parent, attrs, connection_config) ModelBuilder.build(target_class, attrs) end end end |
#load_paginated_collection(conditions:, per_page:, query_scope:, after: nil, before: nil, sort_key: nil, reverse: nil) ⇒ Response::PaginatedResult
Load a paginated collection matching the given conditions.
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 |
# File 'lib/active_shopify_graphql/testing/test_loader.rb', line 27 def load_paginated_collection(conditions:, per_page:, query_scope:, after: nil, before: nil, sort_key: nil, reverse: nil) # rubocop:disable Lint/UnusedMethodArgument all_records = Testing.store.where(@model_class, conditions) # Simple cursor-based pagination using array indices start_index = after ? after.to_i + 1 : 0 start_index = [all_records.size - per_page, 0].max if before page_records = all_records[start_index, per_page] || [] end_index = start_index + page_records.size - 1 page_info = Response::PageInfo.new( "hasNextPage" => end_index < all_records.size - 1, "hasPreviousPage" => start_index.positive?, "startCursor" => start_index.to_s, "endCursor" => end_index.to_s ) attributes_array = page_records.map do |record| attrs = filter_to_model_attributes(record) populate_connection_cache(record, attrs) if @included_connections.any? attrs end Response::PaginatedResult.new( attributes: attributes_array, model_class: @model_class, page_info: page_info, query_scope: query_scope ) end |
#perform_graphql_query(_query, **_variables) ⇒ Object
Should never be called — all data comes from the Store.
90 91 92 93 |
# File 'lib/active_shopify_graphql/testing/test_loader.rb', line 90 def perform_graphql_query(_query, **_variables) raise "TestLoader should not execute GraphQL queries. " \ "Ensure all test data is registered via ActiveShopifyGraphQL::Testing.register" end |