Skip to content

Commit

Permalink
add nodes exposure on ConectionType so nesting through edges isn't al…
Browse files Browse the repository at this point in the history
…ways needed. (#952)
  • Loading branch information
urkle committed Jun 16, 2022
1 parent a210f64 commit 8ccd27e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 8 deletions.
12 changes: 7 additions & 5 deletions integrations/actix-web/tests/graphql.rs
Expand Up @@ -137,11 +137,13 @@ async fn test_hello_header() {
async fn test_count() {
let srv = test::init_service(
App::new()
.app_data(Data::new(
Schema::build(CountQueryRoot, CountMutation, EmptySubscription)
.data(Count::default())
.finish(),
))
.app_data(
Data::new(
Schema::build(CountQueryRoot, CountMutation, EmptySubscription)
.data(Count::default())
.finish(),
),
)
.service(
web::resource("/")
.guard(guard::Post())
Expand Down
7 changes: 4 additions & 3 deletions src/extensions/opentelemetry.rs
Expand Up @@ -83,13 +83,14 @@ where
next: NextSubscribe<'_>,
) -> BoxStream<'s, Response> {
Box::pin(
next.run(ctx, stream)
.with_context(OpenTelemetryContext::current_with_span(
next.run(ctx, stream).with_context(
OpenTelemetryContext::current_with_span(
self.tracer
.span_builder("subscribe")
.with_kind(SpanKind::Server)
.start(&*self.tracer),
)),
),
),
)
}

Expand Down
5 changes: 5 additions & 0 deletions src/types/connection/connection_type.rs
Expand Up @@ -117,6 +117,11 @@ where
&self.edges
}

/// A list of nodes.
async fn nodes(&self) -> Vec<&Node> {
self.edges.iter().map(|e| &e.node).collect()
}

#[graphql(flatten)]
#[inline]
async fn additional_fields(&self) -> &ConnectionFields {
Expand Down
73 changes: 73 additions & 0 deletions tests/connection.rs
Expand Up @@ -94,3 +94,76 @@ pub async fn test_connection_additional_fields() {
})
);
}

#[tokio::test]
pub async fn test_connection_nodes() {
struct Query;

#[Object]
impl Query {
async fn numbers(
&self,
after: Option<String>,
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> Result<Connection<usize, i32>> {
connection::query(
after,
before,
first,
last,
|after, before, first, last| async move {
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
.edges
.extend((start..end).map(|n| Edge::new(n, n as i32)));
Ok::<_, Error>(connection)
},
)
.await
}
}

let schema = Schema::new(Query, EmptyMutation, EmptySubscription);

assert_eq!(
schema
.execute("{ numbers(first: 2) { __typename edges { __typename node } nodes } }")
.await
.data,
value!({
"numbers": {
"__typename": "IntConnection",
"edges": [
{"__typename": "IntEdge", "node": 0},
{"__typename": "IntEdge", "node": 1},
],
"nodes": [
0,
1,
],
},
})
);

assert_eq!(
schema.execute("{ numbers(last: 2) { nodes } }").await.data,
value!({
"numbers": {
"nodes": [
9998,
9999,
],
},
})
);
}

0 comments on commit 8ccd27e

Please sign in to comment.