Merge pull request #53 from phated/datasource-context

Datasource context
This commit is contained in:
Sunli 2020-05-07 10:04:25 +08:00 committed by GitHub
commit 78096e0931
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 11 deletions

View File

@ -22,25 +22,37 @@ impl DataSource for Integers {
// 我们不需要扩展边的字段所以传EmptyEdgeFields // 我们不需要扩展边的字段所以传EmptyEdgeFields
type EdgeFieldsObj = EmptyEdgeFields; type EdgeFieldsObj = EmptyEdgeFields;
async fn query_operation(&self, operation: &QueryOperation<'_>) -> FieldResult<Connection<Self::Element, Self::EdgeFieldsObj>> { async fn query_operation(&self, _ctx: &Context<'_>, operation: &QueryOperation<'_>) -> FieldResult<Connection<Self::Element, Self::EdgeFieldsObj>> {
let (start, end) = match operation { let (start, end) = match operation {
// 向前查找 // 向前查找
QueryOperation::Forward {after, limit} => { QueryOperation::First {limit} => {
let start = 0;
let end = start + *limit as i32;
(start, end)
}
QueryOperation::FirstAfter {after, limit} => {
// 起始数字从after+1开始如果没有after参数则从0开始 // 起始数字从after+1开始如果没有after参数则从0开始
let start = after let start = after.parse::<i32>()
.and_then(|after| after.parse::<i32>().ok()) .ok()
.map(|after| after + 1) .map(|after| after + 1)
.unwrap_or(0); .unwrap_or(0);
(start, end + start + *limit) (start, end + start + *limit)
} }
// 向后查找 // 向后查找
QueryOperation::Backward {before, limit} => { QueryOperation::Last {limit} => {
let end = 0;
let start = end - *limit as i32;
(start, end)
}
QueryOperation::LastBefore {before, limit} => {
// 结束数字如果没有before参数则为0 // 结束数字如果没有before参数则为0
let end = before let end = before.parse::<i32>()
.and_then(|before| before.parse::<i32>().ok()) .ok()
.unwrap_or(0); .unwrap_or(0);
(end - *limit, end) (end - *limit, end)
} }
// TODO: Advise to handle all cases
_ => (0, 10)
}; };
// 创建节点,每个节点都是一个包含三个值的元组,依次是游标,扩展边对象,节点值 // 创建节点,每个节点都是一个包含三个值的元组,依次是游标,扩展边对象,节点值

View File

@ -130,7 +130,7 @@ struct Pagination {
/// type Element = i32; /// type Element = i32;
/// type EdgeFieldsObj = DiffFields; /// type EdgeFieldsObj = DiffFields;
/// ///
/// async fn query_operation(&self, operation: &QueryOperation) -> FieldResult<Connection<Self::Element, Self::EdgeFieldsObj>> { /// async fn query_operation(&self, ctx: &Context<'_>, operation: &QueryOperation) -> FieldResult<Connection<Self::Element, Self::EdgeFieldsObj>> {
/// let (start, end) = match operation { /// let (start, end) = match operation {
/// QueryOperation::First {limit} => { /// QueryOperation::First {limit} => {
/// let start = 0; /// let start = 0;
@ -216,7 +216,7 @@ pub trait DataSource: Sync + Send {
/// Execute the query. /// Execute the query.
async fn query( async fn query(
&self, &self,
_ctx: &Context<'_>, ctx: &Context<'_>,
after: Option<Cursor>, after: Option<Cursor>,
before: Option<Cursor>, before: Option<Cursor>,
first: Option<i32>, first: Option<i32>,
@ -335,12 +335,13 @@ pub trait DataSource: Sync + Send {
}, },
}; };
self.query_operation(&operation).await self.query_operation(ctx, &operation).await
} }
/// Parses the parameters and executes the queryUsually you just need to implement this method. /// Parses the parameters and executes the queryUsually you just need to implement this method.
async fn query_operation( async fn query_operation(
&self, &self,
ctx: &Context<'_>,
operation: &QueryOperation, operation: &QueryOperation,
) -> FieldResult<Connection<Self::Element, Self::EdgeFieldsObj>>; ) -> FieldResult<Connection<Self::Element, Self::EdgeFieldsObj>>;
} }

View File

@ -1,5 +1,5 @@
use crate::types::connection::{EmptyEdgeFields, QueryOperation}; use crate::types::connection::{EmptyEdgeFields, QueryOperation};
use crate::{Connection, DataSource, FieldResult}; use crate::{Connection, Context, DataSource, FieldResult};
use byteorder::{ReadBytesExt, BE}; use byteorder::{ReadBytesExt, BE};
#[async_trait::async_trait] #[async_trait::async_trait]
@ -9,6 +9,7 @@ impl<'a, T: Sync> DataSource for &'a [T] {
async fn query_operation( async fn query_operation(
&self, &self,
_ctx: &Context<'_>,
operation: &QueryOperation, operation: &QueryOperation,
) -> FieldResult<Connection<Self::Element, Self::EdgeFieldsObj>> { ) -> FieldResult<Connection<Self::Element, Self::EdgeFieldsObj>> {
let (start, end) = match operation { let (start, end) = match operation {