1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use proc_macro::TokenStream;
use syn::{parse_macro_input, Data, DeriveInput, Ident};
#[macro_use]
extern crate quote;
extern crate proc_macro;
#[proc_macro_derive(RelayGlobalID)]
pub fn derive_relay_global_id(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;
let m = quote! {
impl From<&#name> for String {
fn from(id: &#name) -> Self {
let node_type = id.1.clone() as u32;
let mut uuid = id.0.clone();
if uuid.len() < 36 {
panic!("ID type must only contain a UUIDv4");
}
uuid.remove(8);
uuid.remove(12);
uuid.remove(16);
uuid.remove(20);
format!("{}{}", uuid, node_type)
}
}
#[async_graphql::Scalar]
impl async_graphql::ScalarType for #name {
fn parse(_value: async_graphql::Value) -> async_graphql::InputValueResult<Self> {
unimplemented!();
}
fn to_value(&self) -> async_graphql::Value {
async_graphql::Value::String(String::from(self))
}
}
};
TokenStream::from(m)
}
#[proc_macro_derive(RelayNodeEnum)]
pub fn derive_relay_node(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;
let variants = match input.data {
Data::Enum(e) => e
.variants
.into_iter()
.map(|v| v.ident)
.collect::<Vec<Ident>>(),
_ => {
panic!("The RelayNode macro must be used on an enum type");
}
};
let variant_node_type = (0..variants.len()).map(|v| (v + 1).to_string());
let m = quote! {
#[derive(Clone)]
pub enum SchemaNodeTypes {
Unknown = 0,
#(
#variants,
)*
}
impl #name {
pub async fn get(ctx: async_graphql_relay::RelayContext, relay_id: String) -> Option<Node> {
if relay_id.len() < 32 {
None?
}
let (id, node_type) = relay_id.split_at(32);
let mut id = id.to_string();
id.insert(8, '-');
id.insert(13, '-');
id.insert(18, '-');
id.insert(23, '-');
match node_type {
#(
#variant_node_type => <#variants>::get(ctx, id.to_string()).await,
)*
_ => None
}
}
}
};
TokenStream::from(m)
}