Skip to content

Commit

Permalink
mvp offset support. requires pagination tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olirice committed Jan 31, 2024
1 parent 1a628a4 commit 63d3ed5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ pub struct ConnectionBuilder {
pub last: Option<u64>,
pub before: Option<Cursor>,
pub after: Option<Cursor>,
pub offset: Option<u64>,
pub filter: FilterBuilder,
pub order_by: OrderByBuilder,

Expand Down Expand Up @@ -1318,7 +1319,9 @@ where
match &type_ {
__Type::Connection(xtype) => {
// Raise for disallowed arguments
let mut allowed_args = vec!["first", "last", "before", "after", "filter", "orderBy"];
let mut allowed_args = vec![
"first", "last", "before", "after", "offset", "filter", "orderBy",
];
allowed_args.extend(extra_allowed_args);
restrict_allowed_arguments(&allowed_args, query_field)?;

Expand Down Expand Up @@ -1349,6 +1352,24 @@ where
}
};

let offset: gson::Value = read_argument(
"offset",
field,
query_field,
variables,
variable_definitions,
)?;
let offset: Option<u64> = match offset {
gson::Value::Absent | gson::Value::Null => None,
gson::Value::Number(gson::Number::Integer(n)) if n < 0 => {
return Err("`offset` must be an unsigned integer".to_string())
}
gson::Value::Number(gson::Number::Integer(n)) => Some(n as u64),
_ => {
return Err("Internal Error: failed to parse validated offset".to_string());
}
};

let max_rows: u64 = xtype
.schema
.context
Expand Down Expand Up @@ -1434,6 +1455,7 @@ where
first,
last,
before,
offset,
after,
filter,
order_by,
Expand Down
9 changes: 9 additions & 0 deletions src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,15 @@ impl ConnectionType {
default_value: None,
sql_type: None,
},
__InputValue {
name_: "offset".to_string(),
type_: __Type::Scalar(Scalar::Int),
description: Some(
"Query values in the collection after the provided cursor".to_string(),
),
default_value: None,
sql_type: None,
},
__InputValue {
name_: "filter".to_string(),
type_: __Type::FilterEntity(FilterEntityType {
Expand Down
7 changes: 6 additions & 1 deletion src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ impl MutationEntrypoint<'_> for UpdateBuilder {
select
case
when total.total_count > {at_most} then graphql.exception($a$update impacts too many records$a$)::jsonb
else req.res
end
from
total
Expand Down Expand Up @@ -971,6 +972,7 @@ impl ConnectionBuilder {
};

let limit = self.limit_clause();
let offset = self.offset.unwrap_or(0);

// initialized assuming forwards pagination
let mut has_next_page_query = format!(
Expand All @@ -987,6 +989,7 @@ impl ConnectionBuilder {
order by
{order_by_clause}
limit ({limit} + 1)
offset ({offset})
)
select count(*) > {limit} from page_plus_1
"
Expand Down Expand Up @@ -1038,6 +1041,8 @@ impl ConnectionBuilder {
{order_by_clause_records}
limit
{limit}
offset
{offset}
),
__total_count(___total_count) as (
select
Expand Down

0 comments on commit 63d3ed5

Please sign in to comment.