extract ensure_valid_params
This commit is contained in:
parent
0dd68ab73d
commit
311c9215cd
1 changed files with 19 additions and 16 deletions
|
@ -27,6 +27,7 @@ module Connections
|
||||||
|
|
||||||
def load_nodes
|
def load_nodes
|
||||||
@nodes ||= begin
|
@nodes ||= begin
|
||||||
|
ensure_valid_params
|
||||||
page_info = compute_page_info(before:, after:, first:, last:)
|
page_info = compute_page_info(before:, after:, first:, last:)
|
||||||
nodes = resolve_nodes(**page_info.slice(:before, :after, :limit, :inverted))
|
nodes = resolve_nodes(**page_info.slice(:before, :after, :limit, :inverted))
|
||||||
result_size = nodes.size
|
result_size = nodes.size
|
||||||
|
@ -39,6 +40,24 @@ module Connections
|
||||||
end
|
end
|
||||||
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)
|
def timestamp_and_id_from_cursor(cursor)
|
||||||
timestamp, id = decode(cursor).split(';')
|
timestamp, id = decode(cursor).split(';')
|
||||||
[Time.zone.parse(timestamp), id.to_i]
|
[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
|
# first is a number (n) and mean take n element in order ascendant
|
||||||
# last : n element in order descendant
|
# last : n element in order descendant
|
||||||
def compute_page_info(before: nil, after: nil, first: nil, last: nil)
|
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 @deprecated_order == :desc
|
||||||
if last.present?
|
if last.present?
|
||||||
first = [last, max_page_size].min
|
first = [last, max_page_size].min
|
||||||
|
|
Loading…
Reference in a new issue