entity_async_graphql/ent/
edge.rs

1use async_graphql::{Enum, Object};
2use derive_more::{From, Into};
3use entity::{Edge, EdgeDeletionPolicy, EdgeValue, EdgeValueType, Id};
4
5/// Represents a wrapper around an `Edge` to expose as graphql
6#[derive(From, Into)]
7pub struct GqlEdge(Edge);
8
9#[Object]
10impl GqlEdge {
11    #[graphql(name = "name")]
12    async fn gql_name(&self) -> &str {
13        self.0.name()
14    }
15
16    #[graphql(name = "type")]
17    async fn gql_type(&self) -> GqlEdgeValueType {
18        GqlEdgeValueType::from(self.0.to_type())
19    }
20
21    #[graphql(name = "value")]
22    async fn gql_value(&self) -> GqlEdgeValue {
23        GqlEdgeValue::from(self.0.value().clone())
24    }
25
26    #[graphql(name = "ids")]
27    async fn gql_ids(&self) -> Vec<Id> {
28        self.0.to_ids()
29    }
30
31    #[graphql(name = "deletion_policy")]
32    async fn gql_deletion_policy(&self) -> GqlEdgeDeletionPolicy {
33        GqlEdgeDeletionPolicy::from(self.0.deletion_policy())
34    }
35}
36
37#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Enum)]
38#[graphql(remote = "EdgeDeletionPolicy")]
39pub enum GqlEdgeDeletionPolicy {
40    /// When this ent instance is deleted, nothing else will be done
41    Nothing,
42
43    /// When this ent instance is deleted, delete the reverse edge connections
44    /// of all ents connected by this edge
45    ShallowDelete,
46
47    /// When this ent instance is deleted, fully delete all ents connected
48    /// by this edge
49    DeepDelete,
50}
51
52/// Represents a wrapper around an `EdgeValue` to expose as graphql
53#[derive(From, Into)]
54pub struct GqlEdgeValue(EdgeValue);
55
56#[Object]
57impl GqlEdgeValue {
58    #[graphql(name = "ids")]
59    async fn gql_ids(&self) -> Vec<Id> {
60        self.0.to_ids()
61    }
62
63    #[graphql(name = "type")]
64    async fn gql_type(&self) -> GqlEdgeValueType {
65        GqlEdgeValueType::from(self.0.to_type())
66    }
67}
68
69#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Enum)]
70#[graphql(remote = "EdgeValueType")]
71pub enum GqlEdgeValueType {
72    /// Edge can potentially have one outward connection
73    MaybeOne,
74    /// Edge can have exactly one outward connection
75    One,
76    /// Edge can have many outward connections
77    Many,
78}