extract ensure_valid_params

This commit is contained in:
simon lehericey 2023-10-17 16:35:24 +02:00 committed by Paul Chavard
parent 0dd68ab73d
commit 311c9215cd

View file

@ -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