use quote::quote;
use syn::ext::IdentExt as _;
use super::{EnumModel, Namespace};
#[cfg(test)]
mod tests;
impl EnumModel {
pub(super) fn graphql_face(&self) -> proc_macro2::TokenStream {
let gate = self.framework(&["gate"], "if_async_graphql");
let table = self.enum_type();
let input = self.input_type();
let output = self.output_type();
let crossing = self.output_crossing();
let operand = self.input_crossing();
quote! {
const _: () = {
#gate! {
#table
#input
#output
#crossing
#operand
}
};
}
}
pub(super) fn sdl_name(&self) -> String {
self
.wire_name
.clone()
.unwrap_or_else(|| self.name.unraw().to_string())
}
fn enum_type(&self) -> proc_macro2::TokenStream {
let name = &self.name;
let ag = self.framework(&[], "async_graphql");
let items = self.variants.iter().map(|variant| {
let word = variant.name(Namespace::Graphql);
let ident = &variant.ident;
quote! {
#ag::resolver_utils::EnumItem {
name: #word,
value: #name::#ident,
}
}
});
quote! {
#[automatically_derived]
impl #ag::resolver_utils::EnumType for #name {
fn items() -> &'static [#ag::resolver_utils::EnumItem<#name>] {
&[#(#items),*]
}
}
}
}
fn input_type(&self) -> proc_macro2::TokenStream {
let name = &self.name;
let ag = self.framework(&[], "async_graphql");
let sdl = self.sdl_name();
let values = self.variants.iter().map(|variant| {
let word = variant.name(Namespace::Graphql);
quote! {
__ingraph_values.insert(
::std::string::String::from(#word),
#ag::registry::MetaEnumValue {
name: ::std::string::String::from(#word),
description: ::core::option::Option::None,
deprecation: #ag::registry::Deprecation::NoDeprecated,
visible: ::core::option::Option::None,
inaccessible: false,
tags: ::std::vec::Vec::new(),
directive_invocations: ::std::vec::Vec::new(),
},
);
}
});
quote! {
#[automatically_derived]
impl #ag::InputType for #name {
type RawValueType = Self;
#[inline]
fn type_name() -> ::std::borrow::Cow<'static, ::core::primitive::str> {
::std::borrow::Cow::Borrowed(#sdl)
}
fn create_type_info(
__ingraph_registry: &mut #ag::registry::Registry,
) -> ::std::string::String {
__ingraph_registry.create_input_type::<Self, _>(
#ag::registry::MetaTypeId::Enum,
|_| #ag::registry::MetaType::Enum {
name: ::std::string::String::from(#sdl),
description: ::core::option::Option::None,
enum_values: {
let mut __ingraph_values = #ag::indexmap::IndexMap::new();
#(#values)*
__ingraph_values
},
visible: ::core::option::Option::None,
inaccessible: false,
tags: ::std::vec::Vec::new(),
rust_typename: ::core::option::Option::Some(::core::any::type_name::<Self>()),
directive_invocations: ::std::vec::Vec::new(),
requires_scopes: ::std::vec::Vec::new(),
},
)
}
fn parse(
value: ::core::option::Option<#ag::Value>,
) -> #ag::InputValueResult<Self> {
#ag::resolver_utils::parse_enum(::core::option::Option::unwrap_or_default(value))
}
fn to_value(&self) -> #ag::Value {
#ag::resolver_utils::enum_value(*self)
}
#[inline]
fn as_raw_value(&self) -> ::core::option::Option<&Self::RawValueType> {
::core::option::Option::Some(self)
}
}
}
}
fn output_type(&self) -> proc_macro2::TokenStream {
let name = &self.name;
let ag = self.framework(&[], "async_graphql");
quote! {
#[automatically_derived]
impl #ag::OutputType for #name {
#[inline]
fn type_name() -> ::std::borrow::Cow<'static, ::core::primitive::str> {
<Self as #ag::InputType>::type_name()
}
#[inline]
fn create_type_info(
__ingraph_registry: &mut #ag::registry::Registry,
) -> ::std::string::String {
<Self as #ag::InputType>::create_type_info(__ingraph_registry)
}
async fn resolve(
&self,
_: &#ag::ContextSelectionSet<'_>,
_: &#ag::Positioned<#ag::parser::types::Field>,
) -> #ag::ServerResult<#ag::Value> {
::core::result::Result::Ok(#ag::resolver_utils::enum_value(*self))
}
}
}
}
fn output_crossing(&self) -> proc_macro2::TokenStream {
let name = &self.name;
let scalar = self.framework(&["graphql"], "ToGraphqlOutput");
quote! {
#[automatically_derived]
impl #scalar for #name {
type Output<'__ingraph> = &'__ingraph Self;
#[inline]
fn to_graphql_output(__ingraph_value: &Self) -> &Self {
__ingraph_value
}
}
}
}
fn input_crossing(&self) -> proc_macro2::TokenStream {
let name = &self.name;
let operand = self.framework(&["graphql"], "ToGraphqlInput");
quote! {
#[automatically_derived]
impl #operand for #name {
type Input = Self;
type Error = ::core::convert::Infallible;
#[inline]
fn cross(__ingraph_value: Self) -> ::core::result::Result<Self, ::core::convert::Infallible> {
::core::result::Result::Ok(__ingraph_value)
}
#[inline]
fn carry(__ingraph_value: &Self) -> Self {
*__ingraph_value
}
}
}
}
}