juniper_codegen/graphql_subscription/
attr.rs

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
//! Code generation for `#[graphql_subscription]` macro.

use proc_macro2::{Span, TokenStream};

use crate::{
    common::parse,
    graphql_object::{attr::expand_on_impl, Attr},
};

use super::Subscription;

/// Expands `#[graphql_subscription]` macro into generated code.
pub fn expand(attr_args: TokenStream, body: TokenStream) -> syn::Result<TokenStream> {
    if let Ok(mut ast) = syn::parse2::<syn::ItemImpl>(body) {
        if ast.trait_.is_none() {
            let impl_attrs = parse::attr::unite(("graphql_subscription", &attr_args), &ast.attrs);
            ast.attrs = parse::attr::strip(["graphql_subscription", "graphql"], ast.attrs);
            return expand_on_impl::<Subscription>(
                Attr::from_attrs(["graphql_subscription", "graphql"], &impl_attrs)?,
                ast,
            );
        }
    }

    Err(syn::Error::new(
        Span::call_site(),
        "#[graphql_subscription] attribute is applicable to non-trait `impl` blocks only",
    ))
}