Skip to content

Commit 4c07228

Browse files
authored
Merge pull request #595 from kafaichoi/support-max-rows-in-table-and-view
Support max_rows directive on table and view
2 parents 0aa8dfd + 50ad0f5 commit 4c07228

File tree

5 files changed

+129
-3
lines changed

5 files changed

+129
-3
lines changed

sql/load_sql_context.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ select
265265
)
266266
),
267267
'primary_key_columns', d.directive -> 'primary_key_columns',
268-
'foreign_keys', d.directive -> 'foreign_keys'
268+
'foreign_keys', d.directive -> 'foreign_keys',
269+
'max_rows', (d.directive ->> 'max_rows')::int
269270
)
270271
from
271272
directives d

src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,13 +1423,13 @@ where
14231423
}
14241424
};
14251425

1426-
let max_rows: u64 = xtype
1426+
let max_rows = xtype
14271427
.schema
14281428
.context
14291429
.schemas
14301430
.values()
14311431
.find(|s| s.oid == xtype.table.schema_oid)
1432-
.map(|x| x.directives.max_rows)
1432+
.map(|schema| xtype.table.max_rows(schema))
14331433
.unwrap_or(30);
14341434

14351435
let before: Option<Cursor> = read_argument_cursor(

src/sql_types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ pub struct TableDirectives {
482482
// @graphql({"primary_key_columns": ["id"]})
483483
pub primary_key_columns: Option<Vec<String>>,
484484

485+
// @graphql({"max_rows": 20})
486+
pub max_rows: Option<u64>,
487+
485488
/*
486489
@graphql(
487490
{
@@ -576,13 +579,22 @@ impl Table {
576579
pub fn is_any_column_selectable(&self) -> bool {
577580
self.columns.iter().any(|x| x.permissions.is_selectable)
578581
}
582+
579583
pub fn is_any_column_insertable(&self) -> bool {
580584
self.columns.iter().any(|x| x.permissions.is_insertable)
581585
}
582586

583587
pub fn is_any_column_updatable(&self) -> bool {
584588
self.columns.iter().any(|x| x.permissions.is_updatable)
585589
}
590+
591+
/// Get the effective max_rows value for this table.
592+
/// If table-specific max_rows is set, use that.
593+
/// Otherwise, fall back to schema-level max_rows.
594+
/// If neither is set, use the global default(set in load_sql_context.sql)
595+
pub fn max_rows(&self, schema: &Schema) -> u64 {
596+
self.directives.max_rows.unwrap_or(schema.directives.max_rows)
597+
}
586598
}
587599

588600
#[derive(Deserialize, Clone, Debug, Eq, PartialEq, Hash)]

test/expected/max_rows_directive.out

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,65 @@ begin;
6060
{"data": {"accountCollection": {"edges": [{"node": {"id": 1}}, {"node": {"id": 2}}, {"node": {"id": 3}}, {"node": {"id": 4}}, {"node": {"id": 5}}, {"node": {"id": 6}}, {"node": {"id": 7}}, {"node": {"id": 8}}, {"node": {"id": 9}}, {"node": {"id": 10}}, {"node": {"id": 11}}, {"node": {"id": 12}}, {"node": {"id": 13}}, {"node": {"id": 14}}, {"node": {"id": 15}}, {"node": {"id": 16}}, {"node": {"id": 17}}, {"node": {"id": 18}}, {"node": {"id": 19}}, {"node": {"id": 20}}, {"node": {"id": 21}}, {"node": {"id": 22}}, {"node": {"id": 23}}, {"node": {"id": 24}}, {"node": {"id": 25}}, {"node": {"id": 26}}, {"node": {"id": 27}}, {"node": {"id": 28}}, {"node": {"id": 29}}, {"node": {"id": 30}}, {"node": {"id": 31}}, {"node": {"id": 32}}, {"node": {"id": 33}}, {"node": {"id": 34}}, {"node": {"id": 35}}, {"node": {"id": 36}}, {"node": {"id": 37}}, {"node": {"id": 38}}, {"node": {"id": 39}}, {"node": {"id": 40}}]}}}
6161
(1 row)
6262

63+
-- table-specific max_rows
64+
comment on table account is e'@graphql({"max_rows": 5})';
65+
-- expect 5 rows on first page
66+
select graphql.resolve($$
67+
{
68+
accountCollection {
69+
edges {
70+
node {
71+
id
72+
}
73+
}
74+
}
75+
}
76+
$$);
77+
resolve
78+
-------------------------------------------------------------------------------------------------------------------------------------------------------
79+
{"data": {"accountCollection": {"edges": [{"node": {"id": 1}}, {"node": {"id": 2}}, {"node": {"id": 3}}, {"node": {"id": 4}}, {"node": {"id": 5}}]}}}
80+
(1 row)
81+
82+
-- view-specific max_rows
83+
create view person as
84+
select * from account;
85+
comment on view person is e'@graphql({"primary_key_columns": ["id"], "max_rows": 3})';
86+
-- expect 3 rows on first page
87+
select graphql.resolve($$
88+
{
89+
personCollection {
90+
edges {
91+
node {
92+
id
93+
}
94+
}
95+
}
96+
}
97+
$$);
98+
resolve
99+
------------------------------------------------------------------------------------------------------------
100+
{"data": {"personCollection": {"edges": [{"node": {"id": 1}}, {"node": {"id": 2}}, {"node": {"id": 3}}]}}}
101+
(1 row)
102+
103+
-- nested view with max_rows
104+
create view parent as
105+
select * from person;
106+
comment on view parent is e'@graphql({"primary_key_columns": ["id"], "max_rows": 2})';
107+
-- expect 2 rows on first page
108+
select graphql.resolve($$
109+
{
110+
parentCollection {
111+
edges {
112+
node {
113+
id
114+
}
115+
}
116+
}
117+
}
118+
$$);
119+
resolve
120+
---------------------------------------------------------------------------------------
121+
{"data": {"parentCollection": {"edges": [{"node": {"id": 1}}, {"node": {"id": 2}}]}}}
122+
(1 row)
123+
63124
rollback;

test/sql/max_rows_directive.sql

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,56 @@ begin;
5252
}
5353
$$);
5454

55+
-- table-specific max_rows
56+
comment on table account is e'@graphql({"max_rows": 5})';
57+
58+
-- expect 5 rows on first page
59+
select graphql.resolve($$
60+
{
61+
accountCollection {
62+
edges {
63+
node {
64+
id
65+
}
66+
}
67+
}
68+
}
69+
$$);
70+
71+
-- view-specific max_rows
72+
create view person as
73+
select * from account;
74+
comment on view person is e'@graphql({"primary_key_columns": ["id"], "max_rows": 3})';
75+
76+
-- expect 3 rows on first page
77+
select graphql.resolve($$
78+
{
79+
personCollection {
80+
edges {
81+
node {
82+
id
83+
}
84+
}
85+
}
86+
}
87+
$$);
88+
89+
-- nested view with max_rows
90+
create view parent as
91+
select * from person;
92+
comment on view parent is e'@graphql({"primary_key_columns": ["id"], "max_rows": 2})';
93+
94+
-- expect 2 rows on first page
95+
select graphql.resolve($$
96+
{
97+
parentCollection {
98+
edges {
99+
node {
100+
id
101+
}
102+
}
103+
}
104+
}
105+
$$);
106+
55107
rollback;

0 commit comments

Comments
 (0)