use quote::quote;
use crate::{emit::leading, interface::InterfaceModel};
#[cfg(test)]
mod tests;
impl InterfaceModel {
pub(crate) fn sdl_face(&self) -> proc_macro2::TokenStream {
if !self.projects() {
return quote!();
}
let interface = &self.name;
let name = super::default_output_name(interface);
let published = interface.to_string();
let gate = self.framework(&["gate"], "if_async_graphql");
let ag = self.framework(&[], "async_graphql");
let crossing = self.framework(&["graphql"], "ToGraphqlOutput");
let transformer = self.framework(&["transform"], "Transformer");
let graphql = self.framework(&["transform"], "Graphql");
let outputs: Vec<proc_macro2::TokenStream> = self
.members
.iter()
.map(|member| {
let ty = &member.ty;
quote!(<<#ty as #transformer>::Graphql as #graphql>::Output)
})
.collect();
let registrations = outputs.iter().map(|output| {
quote! {
<#output as #ag::OutputType>::create_type_info(__ingraph_registry);
#ag::registry::Registry::add_implements(
__ingraph_registry,
&<#output as #ag::OutputType>::type_name(),
#published,
);
}
});
let possible = outputs.iter().map(|output| {
quote! {
#ag::indexmap::IndexSet::insert(
&mut __ingraph_possible,
::std::borrow::Cow::into_owned(<#output as #ag::OutputType>::type_name()),
);
}
});
let fields = self.stored().map(|field| {
let word = crate::emit::wire::wire_member(&field.name);
let ty = &field.ty;
quote! {
#ag::indexmap::IndexMap::insert(
&mut __ingraph_fields,
::std::string::String::from(#word),
#ag::registry::MetaField {
name: ::std::string::String::from(#word),
description: ::core::option::Option::None,
args: #ag::indexmap::IndexMap::new(),
ty: <<#ty as #crossing>::Output<'static> as #ag::OutputType>::create_type_info(
__ingraph_registry,
),
deprecation: #ag::registry::Deprecation::NoDeprecated,
cache_control: ::core::default::Default::default(),
external: false,
requires: ::core::option::Option::None,
provides: ::core::option::Option::None,
visible: ::core::option::Option::None,
shareable: false,
inaccessible: false,
tags: ::std::vec::Vec::new(),
override_from: ::core::option::Option::None,
compute_complexity: ::core::option::Option::None,
directive_invocations: ::std::vec::Vec::new(),
requires_scopes: ::std::vec::Vec::new(),
},
);
}
});
let resolves = self.members.iter().map(|member| {
let variant = &member.ident;
quote! {
Self::#variant(__ingraph_member) => {
#ag::resolver_utils::ContainerType::resolve_field(__ingraph_member, __ingraph_ctx).await
}
}
});
let collects = self.members.iter().map(|member| {
let variant = &member.ident;
quote! {
Self::#variant(__ingraph_member) => {
#ag::resolver_utils::ContainerType::collect_all_fields(
__ingraph_member,
__ingraph_ctx,
__ingraph_fields,
)
}
}
});
let typenames = self.members.iter().map(|member| {
let variant = &member.ident;
quote! {
Self::#variant(__ingraph_member) => {
#ag::OutputType::introspection_type_name(__ingraph_member)
}
}
});
let resolve_doc = leading(
"One field of a selection on this interface, resolved by the MEMBER the row came from.\n\nThe \
delegation is what an interface's container is documented to do, and it is what makes an \
inline fragment on a member reach that member's PRIVATE fields. The price is stated in this \
module's note: the interface's published field name is the name its members publish.",
);
let collect_doc = leading(
"The whole selection set, collected against the member the row came from — `ContainerType`'s \
own instruction to an interface, verbatim.",
);
let typename_doc = leading(
"`__typename` — the PUBLISHED name of the member this row came from, read off that member's \
own output face.\n\nThis is the provenance the design ruled instead of a minted `kind` \
field, and delegating is what pins it per arm: a member renamed through \
`output(rename = \"…\")` moves its own `__typename` and this answer follows. The CONCRETE \
by-id field a caller reaches for next is named from the same published word, so the two \
cannot drift.",
);
quote! {
#gate! {
#[automatically_derived]
impl #ag::resolver_utils::ContainerType for #name {
#[doc = #resolve_doc]
async fn resolve_field(
&self,
__ingraph_ctx: &#ag::Context<'_>,
) -> #ag::ServerResult<::core::option::Option<#ag::Value>> {
match self {
#(#resolves)*
}
}
#[doc = #collect_doc]
fn collect_all_fields<'__ingraph_sel>(
&'__ingraph_sel self,
__ingraph_ctx: &#ag::ContextSelectionSet<'__ingraph_sel>,
__ingraph_fields: &mut #ag::resolver_utils::Fields<'__ingraph_sel>,
) -> #ag::ServerResult<()> {
match self {
#(#collects)*
}
}
}
#[automatically_derived]
impl #ag::OutputType for #name {
#[inline]
fn type_name() -> ::std::borrow::Cow<'static, ::core::primitive::str> {
::std::borrow::Cow::Borrowed(#published)
}
#[doc = #typename_doc]
fn introspection_type_name(
&self,
) -> ::std::borrow::Cow<'static, ::core::primitive::str> {
match self {
#(#typenames)*
}
}
fn create_type_info(
__ingraph_registry: &mut #ag::registry::Registry,
) -> ::std::string::String {
#ag::registry::Registry::create_output_type::<Self, _>(
__ingraph_registry,
#ag::registry::MetaTypeId::Interface,
|__ingraph_registry| {
#(#registrations)*
#ag::registry::MetaType::Interface {
name: ::std::string::String::from(#published),
description: ::core::option::Option::None,
fields: {
let mut __ingraph_fields = #ag::indexmap::IndexMap::new();
#(#fields)*
__ingraph_fields
},
possible_types: {
let mut __ingraph_possible = #ag::indexmap::IndexSet::new();
#(#possible)*
__ingraph_possible
},
extends: false,
keys: ::core::option::Option::None,
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(),
}
},
)
}
async fn resolve(
&self,
__ingraph_ctx: &#ag::ContextSelectionSet<'_>,
__ingraph_field: &#ag::Positioned<#ag::parser::types::Field>,
) -> #ag::ServerResult<#ag::Value> {
let _ = __ingraph_field;
#ag::resolver_utils::resolve_container(__ingraph_ctx, self).await
}
}
#[automatically_derived]
impl #ag::InterfaceType for #name {}
}
}
}
}