entity_async_graphql_macros/
lib.rs

1#![forbid(unsafe_code)]
2
3mod attribute;
4mod data;
5mod derive;
6mod utils;
7
8use syn::{parse_macro_input, AttributeArgs, DeriveInput};
9
10/// Special wrapper to derive an async-graphql object based on the ent
11#[proc_macro_derive(EntObject, attributes(ent))]
12pub fn derive_ent_object(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
13    utils::do_derive(derive::do_derive_ent_object)(input)
14}
15
16/// Special wrapper to derive an async-graphql filter based on the ent
17#[proc_macro_derive(EntFilter, attributes(ent))]
18pub fn derive_ent_filter(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
19    utils::do_derive(derive::do_derive_ent_filter)(input)
20}
21
22/// Injects elements needed for an ent to be derived as a GraphQL entity.
23///
24/// ```
25/// use entity_async_graphql::gql_ent;
26///
27/// #[gql_ent]
28/// pub struct MyEnt {
29///     name: String,
30///     value: u32,
31///
32///     #[ent(field(computed = "123"))]
33///     computed_value: u8,
34///
35///     #[ent(edge)]
36///     other: MyEnt,
37/// }
38/// ```
39#[proc_macro_attribute]
40pub fn gql_ent(
41    args: proc_macro::TokenStream,
42    input: proc_macro::TokenStream,
43) -> proc_macro::TokenStream {
44    let args = parse_macro_input!(args as AttributeArgs);
45    let input = parse_macro_input!(input as DeriveInput);
46
47    let expanded = utils::entity_crate()
48        .and_then(|root| attribute::do_gql_ent(root, args, input))
49        .unwrap_or_else(|x| x.write_errors());
50
51    proc_macro::TokenStream::from(expanded)
52}