From 311c9215cd4481b6c86ed4225a7148ea6f3813d7 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 17 Oct 2023 16:35:24 +0200 Subject: [PATCH] extract ensure_valid_params --- app/graphql/connections/cursor_connection.rb | 35 +++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/app/graphql/connections/cursor_connection.rb b/app/graphql/connections/cursor_connection.rb index 5235e7786..8d07b5534 100644 --- a/app/graphql/connections/cursor_connection.rb +++ b/app/graphql/connections/cursor_connection.rb @@ -27,6 +27,7 @@ module Connections def load_nodes @nodes ||= begin + ensure_valid_params page_info = compute_page_info(before:, after:, first:, last:) nodes = resolve_nodes(**page_info.slice(:before, :after, :limit, :inverted)) result_size = nodes.size @@ -39,6 +40,24 @@ module Connections end end + def ensure_valid_params + if first.present? && last.present? + raise GraphQL::ExecutionError.new('Arguments "first" and "last" are exclusive', extensions: { code: :bad_request }) + end + + if before.present? && after.present? + raise GraphQL::ExecutionError.new('Arguments "before" and "after" are exclusive', extensions: { code: :bad_request }) + end + + if first.present? && first < 0 + raise GraphQL::ExecutionError.new('Argument "first" must be a non-negative integer', extensions: { code: :bad_request }) + end + + if last.present? && last < 0 + raise GraphQL::ExecutionError.new('Argument "last" must be a non-negative integer', extensions: { code: :bad_request }) + end + end + def timestamp_and_id_from_cursor(cursor) timestamp, id = decode(cursor).split(';') [Time.zone.parse(timestamp), id.to_i] @@ -76,22 +95,6 @@ module Connections # first is a number (n) and mean take n element in order ascendant # last : n element in order descendant def compute_page_info(before: nil, after: nil, first: nil, last: nil) - if first.present? && last.present? - raise GraphQL::ExecutionError.new('Arguments "first" and "last" are exclusive', extensions: { code: :bad_request }) - end - - if before.present? && after.present? - raise GraphQL::ExecutionError.new('Arguments "before" and "after" are exclusive', extensions: { code: :bad_request }) - end - - if first.present? && first < 0 - raise GraphQL::ExecutionError.new('Argument "first" must be a non-negative integer', extensions: { code: :bad_request }) - end - - if last.present? && last < 0 - raise GraphQL::ExecutionError.new('Argument "last" must be a non-negative integer', extensions: { code: :bad_request }) - end - if @deprecated_order == :desc if last.present? first = [last, max_page_size].min