From c678dde4be5dfa6db97de193fa877dabd1e3f3ae Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 6 May 2020 17:04:26 -0700 Subject: [PATCH 1/2] Allow datasource to use context --- src/types/connection/mod.rs | 7 ++++--- src/types/connection/slice.rs | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/types/connection/mod.rs b/src/types/connection/mod.rs index fef4b283..75613240 100644 --- a/src/types/connection/mod.rs +++ b/src/types/connection/mod.rs @@ -130,7 +130,7 @@ struct Pagination { /// type Element = i32; /// type EdgeFieldsObj = DiffFields; /// -/// async fn query_operation(&self, operation: &QueryOperation) -> FieldResult> { +/// async fn query_operation(&self, ctx: &Context<'_>, operation: &QueryOperation) -> FieldResult> { /// 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, before: Option, first: Option, @@ -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 query,Usually you just need to implement this method. async fn query_operation( &self, + ctx: &Context<'_>, operation: &QueryOperation, ) -> FieldResult>; } diff --git a/src/types/connection/slice.rs b/src/types/connection/slice.rs index 974bc706..92999de6 100644 --- a/src/types/connection/slice.rs +++ b/src/types/connection/slice.rs @@ -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> { let (start, end) = match operation { From aed8f33ee594fcc8e035e95776d48b554fa5e38b Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 6 May 2020 17:04:59 -0700 Subject: [PATCH 2/2] Update book --- docs/zh-CN/src/cursor_connections.md | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/docs/zh-CN/src/cursor_connections.md b/docs/zh-CN/src/cursor_connections.md index 7b62a699..29664682 100644 --- a/docs/zh-CN/src/cursor_connections.md +++ b/docs/zh-CN/src/cursor_connections.md @@ -22,25 +22,37 @@ impl DataSource for Integers { // 我们不需要扩展边的字段,所以传EmptyEdgeFields type EdgeFieldsObj = EmptyEdgeFields; - async fn query_operation(&self, operation: &QueryOperation<'_>) -> FieldResult> { + async fn query_operation(&self, _ctx: &Context<'_>, operation: &QueryOperation<'_>) -> FieldResult> { 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::().ok()) + let start = after.parse::() + .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::().ok()) + let end = before.parse::() + .ok() .unwrap_or(0); (end - *limit, end) } + // TODO: Advise to handle all cases + _ => (0, 10) }; // 创建节点,每个节点都是一个包含三个值的元组,依次是游标,扩展边对象,节点值