1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

GraphQL schema documentation

This commit is contained in:
Quentin Gliech
2022-11-09 13:39:25 +01:00
parent 4918440c3a
commit 4f01c123c3
8 changed files with 349 additions and 57 deletions

View File

@ -13,9 +13,16 @@
// limitations under the License.
use sqlx::{Database, QueryBuilder};
use thiserror::Error;
use ulid::Ulid;
use uuid::Uuid;
#[derive(Debug, Error)]
#[error("Either 'first' or 'last' must be specified")]
pub struct InvalidPagination;
/// Add cursor-based pagination to a query, as used in paginated GraphQL
/// connections
pub fn generate_pagination<'a, DB>(
query: &mut QueryBuilder<'a, DB>,
id_field: &'static str,
@ -23,7 +30,7 @@ pub fn generate_pagination<'a, DB>(
after: Option<Ulid>,
first: Option<usize>,
last: Option<usize>,
) -> Result<(), anyhow::Error>
) -> Result<(), InvalidPagination>
where
DB: Database,
Uuid: sqlx::Type<DB> + sqlx::Encode<'a, DB>,
@ -69,20 +76,21 @@ where
.push(" DESC LIMIT ")
.push_bind((count + 1) as i64);
} else {
anyhow::bail!("Either 'first' or 'last' must be specified");
return Err(InvalidPagination);
}
Ok(())
}
/// Process a page returned by a paginated query
pub fn process_page<T>(
mut page: Vec<T>,
first: Option<usize>,
last: Option<usize>,
) -> Result<(bool, bool, Vec<T>), anyhow::Error> {
) -> Result<(bool, bool, Vec<T>), InvalidPagination> {
let limit = match (first, last) {
(Some(count), _) | (_, Some(count)) => count,
_ => anyhow::bail!("Either 'first' or 'last' must be specified"),
_ => return Err(InvalidPagination),
};
let is_full = page.len() == (limit + 1);