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 0fa42491b6
3 changed files with 25 additions and 11 deletions

View File

@ -22,25 +22,37 @@ impl DataSource for Integers {
// 我们不需要扩展边的字段所以传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 {
// 向前查找
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开始
let start = after
.and_then(|after| after.parse::<i32>().ok())
let start = after.parse::<i32>()
.ok()
.map(|after| after + 1)
.unwrap_or(0);
(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
let end = before
.and_then(|before| before.parse::<i32>().ok())
let end = before.parse::<i32>()
.ok()
.unwrap_or(0);
(end - *limit, end)
}
// TODO: Advise to handle all cases
_ => (0, 10)
};
// 创建节点,每个节点都是一个包含三个值的元组,依次是游标,扩展边对象,节点值

View File

@ -130,7 +130,7 @@ struct Pagination {
/// type Element = i32;
/// 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 {
/// QueryOperation::First {limit} => {
/// let start = 0;
@ -216,7 +216,7 @@ pub trait DataSource: Sync + Send {
/// Execute the query.
async fn query(
&self,
_ctx: &Context<'_>,
ctx: &Context<'_>,
after: Option<Cursor>,
before: Option<Cursor>,
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.
async fn query_operation(
&self,
ctx: &Context<'_>,
operation: &QueryOperation,
) -> FieldResult<Connection<Self::Element, Self::EdgeFieldsObj>>;
}

View File

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