[−][src]Trait async_graphql::connection::DataSource
Data source of GraphQL Cursor Connections type
Edge
is an extension object type that extends the edge fields, If you don't need it, you can use EmptyEdgeFields
.
References
(GraphQL Cursor Connections Specification)[https://facebook.github.io/relay/graphql/connections.htm]
Examples
use async_graphql::*; use async_graphql::connection::*; struct QueryRoot; struct Numbers; #[SimpleObject] struct Diff { diff: i32, } #[DataSource] impl DataSource for Numbers { type CursorType = usize; type NodeType = i32; type ConnectionFieldsType = EmptyFields; type EdgeFieldsType = Diff; async fn execute_query(&self, ctx: &Context<'_>, after: Option<usize>, before: Option<usize>, first: Option<usize>, last: Option<usize>, ) -> FieldResult<Connection<Self::CursorType, Self::NodeType, Self::ConnectionFieldsType, Self::EdgeFieldsType>> { let mut start = after.map(|after| after + 1).unwrap_or(0); let mut end = before.unwrap_or(10000); if let Some(first) = first { end = (start + first).min(end); } if let Some(last) = last { start = if last > end - start { end } else { end - last }; } let mut connection = Connection::new(start > 0, end < 10000); connection.append( (start..end).into_iter().map(|n| Edge::with_additional_fields(n, n as i32, Diff{ diff: (10000 - n) as i32 })), ); Ok(connection) } } #[Object] impl QueryRoot { async fn numbers(&self, ctx: &Context<'_>, after: Option<String>, before: Option<String>, first: Option<i32>, last: Option<i32> ) -> FieldResult<Connection<usize, i32, EmptyFields, Diff>> { Numbers.query(ctx, after, before, first, last).await } } #[async_std::main] async fn main() { let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription); assert_eq!(schema.execute("{ numbers(first: 2) { edges { node diff } } }").await.unwrap().data, serde_json::json!({ "numbers": { "edges": [ {"node": 0, "diff": 10000}, {"node": 1, "diff": 9999}, ] }, })); assert_eq!(schema.execute("{ numbers(last: 2) { edges { node diff } } }").await.unwrap().data, serde_json::json!({ "numbers": { "edges": [ {"node": 9998, "diff": 2}, {"node": 9999, "diff": 1}, ] }, })); }
Associated Types
type CursorType: CursorType + Send + Sync
Cursor type
type NodeType
Record type
type ConnectionFieldsType
Additional fields for connection
Is a type that implements ObjectType
and can be defined by the procedure macro #[Object]
or #[SimpleObject]
.
type EdgeFieldsType
Additional fields for edge
Is a type that implements ObjectType
and can be defined by the procedure macro #[Object]
or #[SimpleObject]
.
Required methods
#[must_use]fn execute_query<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 Context<'life2>,
after: Option<Self::CursorType>,
before: Option<Self::CursorType>,
first: Option<usize>,
last: Option<usize>
) -> Pin<Box<dyn Future<Output = FieldResult<Connection<Self::CursorType, Self::NodeType, Self::ConnectionFieldsType, Self::EdgeFieldsType>>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
&'life0 self,
ctx: &'life1 Context<'life2>,
after: Option<Self::CursorType>,
before: Option<Self::CursorType>,
first: Option<usize>,
last: Option<usize>
) -> Pin<Box<dyn Future<Output = FieldResult<Connection<Self::CursorType, Self::NodeType, Self::ConnectionFieldsType, Self::EdgeFieldsType>>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
Execute query
Provided methods
#[must_use]fn query<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 Context<'life2>,
after: Option<String>,
before: Option<String>,
first: Option<i32>,
last: Option<i32>
) -> Pin<Box<dyn Future<Output = FieldResult<Connection<Self::CursorType, Self::NodeType, Self::ConnectionFieldsType, Self::EdgeFieldsType>>> + Send + 'async_trait>> where
<Self::CursorType as CursorType>::Error: Display + Send + Sync + 'static,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: Sync + 'async_trait,
&'life0 self,
ctx: &'life1 Context<'life2>,
after: Option<String>,
before: Option<String>,
first: Option<i32>,
last: Option<i32>
) -> Pin<Box<dyn Future<Output = FieldResult<Connection<Self::CursorType, Self::NodeType, Self::ConnectionFieldsType, Self::EdgeFieldsType>>> + Send + 'async_trait>> where
<Self::CursorType as CursorType>::Error: Display + Send + Sync + 'static,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: Sync + 'async_trait,
Parses the parameters and executes the query.
Implementations on Foreign Types
impl<'a, T: Send + Sync> DataSource for &'a [T]
[src]
type CursorType = usize
type NodeType = &'a T
type ConnectionFieldsType = EmptyFields
type EdgeFieldsType = EmptyFields
fn execute_query<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_ctx: &'life1 Context<'life2>,
after: Option<usize>,
before: Option<usize>,
first: Option<usize>,
last: Option<usize>
) -> Pin<Box<dyn Future<Output = FieldResult<Connection<Self::CursorType, Self::NodeType, Self::ConnectionFieldsType, Self::EdgeFieldsType>>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
[src]
&'life0 self,
_ctx: &'life1 Context<'life2>,
after: Option<usize>,
before: Option<usize>,
first: Option<usize>,
last: Option<usize>
) -> Pin<Box<dyn Future<Output = FieldResult<Connection<Self::CursorType, Self::NodeType, Self::ConnectionFieldsType, Self::EdgeFieldsType>>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,