-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for multi-object graphql queries (#436)
- Loading branch information
Showing
10 changed files
with
311 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pub const _DEFAULT_LIMIT: usize = 10; | ||
pub const DEFAULT_LIMIT: u64 = 10; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use sqlx::pool::PoolConnection; | ||
use sqlx::sqlite::SqliteRow; | ||
use sqlx::{FromRow, QueryBuilder, Result, Sqlite}; | ||
|
||
pub enum ID { | ||
Str(String), | ||
I64(i64), | ||
} | ||
|
||
pub async fn query_by_id<T>( | ||
conn: &mut PoolConnection<Sqlite>, | ||
table_name: &str, | ||
id: ID, | ||
) -> Result<T> | ||
where | ||
T: Send + Unpin + for<'a> FromRow<'a, SqliteRow>, | ||
{ | ||
let query = format!("SELECT * FROM {} WHERE id = ?", table_name); | ||
let result = match id { | ||
ID::Str(id) => sqlx::query_as::<_, T>(&query).bind(id).fetch_one(conn).await?, | ||
ID::I64(id) => sqlx::query_as::<_, T>(&query).bind(id).fetch_one(conn).await?, | ||
}; | ||
Ok(result) | ||
} | ||
|
||
pub async fn query_all<T>( | ||
conn: &mut PoolConnection<Sqlite>, | ||
table_name: &str, | ||
limit: u64, | ||
) -> Result<Vec<T>> | ||
where | ||
T: Send + Unpin + for<'a> FromRow<'a, SqliteRow>, | ||
{ | ||
let mut builder: QueryBuilder<'_, Sqlite> = QueryBuilder::new("SELECT * FROM "); | ||
builder.push(table_name).push(" ORDER BY created_at DESC LIMIT ").push(limit); | ||
let results: Vec<T> = builder.build_query_as().fetch_all(conn).await?; | ||
Ok(results) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.