entity_async_graphql/ent/
mod.rs

1use async_graphql::{Error, Object, Result};
2use derive_more::{From, Into};
3use entity::{Ent, Id};
4
5mod edge;
6mod field;
7mod value;
8
9pub use edge::{GqlEdge, GqlEdgeDeletionPolicy, GqlEdgeValue, GqlEdgeValueType};
10pub use field::GqlField;
11pub use value::GqlValue;
12
13/// Represents a wrapper around a `dyn Ent` to expose as graphql
14#[derive(From, Into)]
15pub struct GqlDynEnt(Box<dyn Ent>);
16
17#[Object]
18impl GqlDynEnt {
19    #[graphql(name = "id")]
20    async fn gql_id(&self) -> Id {
21        self.0.id()
22    }
23
24    #[graphql(name = "type")]
25    async fn gql_type(&self) -> &str {
26        self.0.r#type()
27    }
28
29    #[graphql(name = "created")]
30    async fn gql_created(&self) -> u64 {
31        self.0.created()
32    }
33
34    #[graphql(name = "last_updated")]
35    async fn gql_last_updated(&self) -> u64 {
36        self.0.last_updated()
37    }
38
39    #[graphql(name = "field")]
40    async fn gql_field(&self, name: String) -> Option<GqlValue> {
41        self.0.field(&name).map(GqlValue::from)
42    }
43
44    #[graphql(name = "fields")]
45    async fn gql_fields(&self) -> Vec<GqlField> {
46        self.0.fields().into_iter().map(GqlField::from).collect()
47    }
48
49    #[graphql(name = "edge")]
50    async fn gql_edge(&self, name: String) -> Option<GqlEdgeValue> {
51        self.0.edge(&name).map(GqlEdgeValue::from)
52    }
53
54    #[graphql(name = "edges")]
55    async fn gql_edges(&self) -> Vec<GqlEdge> {
56        self.0.edges().into_iter().map(GqlEdge::from).collect()
57    }
58
59    #[graphql(name = "load_edge")]
60    async fn gql_load_edge(&self, name: String) -> Result<Vec<Self>> {
61        self.0
62            .load_edge(&name)
63            .map(|x| x.into_iter().map(Self::from).collect())
64            .map_err(|x| Error::new(x.to_string()))
65    }
66}