use quote::{format_ident, quote, quote_spanned};
use syn::spanned::Spanned as _;
use super::{DatabaseKind, Delegate, EnumModel, EnumVariant, Escape, GraphqlKind, Remote};
#[cfg(test)]
mod tests;
impl EnumModel {
pub(super) fn emit_mirror(&self, remote: &Remote) -> proc_macro2::TokenStream {
let error = self.refuses().then(|| self.refusal());
let total = self.door(remote);
let markers = self.markers();
let conversions = self.conversions(remote);
let face = self.mirror_face(remote);
let parsing = self.mirror_parsing(remote);
let cursor = self.mirror_cursor();
let key = self.key_delegation();
let width = self.width_row();
let read = self.read_row();
let storage = DatabaseKind::ALL
.into_iter()
.map(|database| self.mirror_storage(database));
let wire = self.scalar_wire();
quote! {
#error
const _: () = {
#total
#markers
#conversions
#face
#parsing
#cursor
#key
#width
#read
#(#storage)*
};
#wire
}
}
fn door(&self, remote: &Remote) -> Option<proc_macro2::TokenStream> {
Some(match self.posture()? {
Escape::Total => self.total_from_str(remote),
Escape::Gate(gate) => self.unconditional_gate(remote, gate),
Escape::TryGate(gate) => self.refusable_gate(remote, gate),
})
}
fn total_from_str(&self, remote: &Remote) -> proc_macro2::TokenStream {
let name = &self.name;
let path = &remote.path;
let spend = self.spend();
let message = format!(
"`{{Self}}` is a REFUSAL, so the vocabulary it comes from is closed — and `{name}` declares \
an `#[ingraph(other)]` escape, which is a promise every word parses"
);
let note = "an open vocabulary is one whose `FromStr` cannot fail: a word it does not know \
becomes its own escape rather than a refusal, so `FromStr::Err` is \
`core::convert::Infallible`. Drop the `#[ingraph(other)]` variant and this mirror's \
`From` becomes exhaustive over the upstream's own variants, which is what makes an \
upstream that grows one a compile error here rather than a silent absence";
let outer = format!(
"`{{Self}}` cannot be mirrored as an OPEN vocabulary — `{name}` declares an \
`#[ingraph(other)]` escape, and that is a promise every word parses"
);
quote! {
#[diagnostic::on_unimplemented(
message = #message,
label = "a parse that can produce this is a parse that can refuse",
note = #note,
)]
trait __IngraphNever {
fn never(self) -> ::core::convert::Infallible;
}
impl __IngraphNever for ::core::convert::Infallible {
fn never(self) -> ::core::convert::Infallible {
self
}
}
#[diagnostic::on_unimplemented(message = #outer, note = #note)]
trait __IngraphTotalFromStr: ::core::str::FromStr {
fn total(text: &::core::primitive::str) -> Self;
}
impl<T> __IngraphTotalFromStr for T
where
T: ::core::str::FromStr,
<T as ::core::str::FromStr>::Err: __IngraphNever,
{
fn total(text: &::core::primitive::str) -> Self {
match <Self as ::core::str::FromStr>::from_str(text) {
::core::result::Result::Ok(value) => value,
::core::result::Result::Err(refused) => match __IngraphNever::never(refused) {},
}
}
}
fn #spend(text: &::core::primitive::str) -> #path {
<#path as __IngraphTotalFromStr>::total(text)
}
}
}
fn unconditional_gate(&self, remote: &Remote, gate: &syn::Path) -> proc_macro2::TokenStream {
let name = &self.name;
let path = &remote.path;
let spend = self.spend();
let marker = self.gate_marker();
let message = format!(
"`{{Self}}` is what `{name}`'s `other(from = \"…\")` gate answers with, and an UNCONDITIONAL \
gate answers with the mirrored vocabulary itself"
);
let note = "`other(from = \"…\")` names a gate of the shape `fn(impl AsRef<str>) -> Remote` — \
total, with no failure to report — and that signature is where an open mirror's \
promise that every word parses is kept. A gate answering with a `Result` is a gate \
that can REFUSE: write it as `other(try_from = \"…\")`, which is the same door with \
this vocabulary's own `ParseError` cast around the refusal";
let called = quote_spanned! {gate.span()=> #gate(text)};
quote! {
#[diagnostic::on_unimplemented(
message = #message,
label = "an unconditional gate answers with an upstream value, not with a result",
note = #note,
)]
trait #marker {
fn __ingraph_gated(self) -> #path;
}
impl #marker for #path {
#[inline]
fn __ingraph_gated(self) -> #path {
self
}
}
fn #spend(text: &::core::primitive::str) -> #path {
match <#path as ::core::str::FromStr>::from_str(text) {
::core::result::Result::Ok(__ingraph_value) => __ingraph_value,
::core::result::Result::Err(_) => #marker::__ingraph_gated(#called),
}
}
}
}
fn refusable_gate(&self, remote: &Remote, gate: &syn::Path) -> proc_macro2::TokenStream {
let name = &self.name;
let path = &remote.path;
let spend = self.spend();
let marker = self.gate_marker();
let refusal = self.upstream_refusal(remote);
let message = format!(
"`{{Self}}` is what `{name}`'s `other(try_from = \"…\")` gate answers with, and a REFUSABLE \
gate refuses exactly as the vocabulary it opens does"
);
let note = "`other(try_from = \"…\")` names a gate of the shape `fn(impl AsRef<str>) -> \
Result<Remote, <Remote as FromStr>::Err>`, and the error type is not a choice: the \
door parses first and falls to the gate, so the two refusals become one and there \
is one type for this vocabulary's own `ParseError` to be cast around. A gate that \
refuses with a type of its own has to map it — `Result::map_err` at the gate, or an \
error type the upstream already answers with";
let called = quote_spanned! {gate.span()=> #gate(text)};
quote! {
#[diagnostic::on_unimplemented(
message = #message,
label = "this refusal is not the one the mirrored vocabulary's own parse answers with",
note = #note,
)]
trait #marker {
fn __ingraph_gated(self) -> ::core::result::Result<#path, #refusal>;
}
impl #marker for ::core::result::Result<#path, #refusal> {
#[inline]
fn __ingraph_gated(self) -> Self {
self
}
}
fn #spend(
text: &::core::primitive::str,
) -> ::core::result::Result<#path, #refusal> {
match <#path as ::core::str::FromStr>::from_str(text) {
::core::result::Result::Ok(__ingraph_value) => ::core::result::Result::Ok(__ingraph_value),
::core::result::Result::Err(_) => #marker::__ingraph_gated(#called),
}
}
}
}
fn upstream_refusal(&self, remote: &Remote) -> proc_macro2::TokenStream {
let path = &remote.path;
quote!(<#path as ::core::str::FromStr>::Err)
}
fn gate_marker(&self) -> syn::Ident {
format_ident!("__IngraphGate")
}
fn spend(&self) -> syn::Ident {
format_ident!("__ingraph_upstream")
}
fn conversions(&self, remote: &Remote) -> proc_macro2::TokenStream {
let name = &self.name;
let path = &remote.path;
let arms = self.restated().map(|variant| {
let ident = &variant.ident;
let upstream = variant.upstream();
quote!(#path::#upstream => #name::#ident,)
});
let wild = self.escape().map(|escape| {
let ident = &escape.ident;
quote! {
__ingraph_unknown => #name::#ident(::core::convert::Into::into(
::std::string::String::as_str(&::std::string::ToString::to_string(&__ingraph_unknown)),
)),
}
});
quote! {
#[automatically_derived]
impl ::core::convert::From<#path> for #name {
fn from(__ingraph_value: #path) -> Self {
match __ingraph_value {
#(#arms)*
#wild
}
}
}
#[automatically_derived]
impl ::core::convert::From<#name> for #path {
#[inline]
fn from(__ingraph_value: #name) -> Self {
#name::remote(&__ingraph_value)
}
}
}
}
fn mirror_face(&self, remote: &Remote) -> proc_macro2::TokenStream {
let vis = &self.vis;
let name = &self.name;
let path = &remote.path;
let variants = self.restated().map(|variant| {
let ident = &variant.ident;
quote!(Self::#ident)
});
let arms = self.restated().map(|variant| {
let ident = &variant.ident;
let upstream = variant.upstream();
quote!(Self::#ident => #path::#upstream,)
});
let spend = self.spend();
let escape = self.escape().map(|escape| {
let ident = &escape.ident;
let word = quote! {
::core::convert::AsRef::<::core::primitive::str>::as_ref(__ingraph_word)
};
if self.refuses() {
let stopped = format!(
"`{name}::{ident}` holds a word this vocabulary's own door refuses, so there is no \
upstream value for it to materialize as. `{name}::remote` answers with an upstream \
value rather than with a result — every rendering, every bind and every delegated call \
reads through it — so substituting a variant here would hand back a value nobody wrote. \
Only `other(try_from = \"…\")` builds a door that can refuse, and every word reaching \
this escape through a parse, a decode or a cursor has already passed it: this one was \
built by hand"
);
quote! {
Self::#ident(__ingraph_word) => match #spend(#word) {
::core::result::Result::Ok(__ingraph_value) => __ingraph_value,
::core::result::Result::Err(_) => ::core::panic!(#stopped),
},
}
} else {
quote! {
Self::#ident(__ingraph_word) => #spend(#word),
}
}
});
let predicates = self.variants.iter().map(|variant| self.predicate(variant));
let delegates = remote
.delegates
.iter()
.filter(|delegate| delegate.on_self.is_none())
.map(|delegate| self.delegate(delegate));
let variants_doc = format!(
"Every RESTATED variant of [`{name}`], in declaration order.\n\nThe escape is absent: it \
carries a word rather than a name, so it is no constant. This is the list a per-variant \
round-trip pin walks."
);
let remote_doc = format!(
"The upstream value this mirrors, by value.\n\nEvery word [`{name}`] renders or parses goes \
through here, so the grammar is the upstream's alone. The escape variant PARSES its word \
back, which is what makes a mirror's rendering byte-identical to the upstream's."
);
quote! {
#[automatically_derived]
impl #name {
#[doc = #variants_doc]
#vis const VARIANTS: &'static [Self] = &[#(#variants),*];
#[doc = #remote_doc]
#[must_use]
#vis fn remote(&self) -> #path {
match self {
#(#arms)*
#escape
}
}
#(#predicates)*
#(#delegates)*
}
}
}
pub(super) fn predicate(&self, variant: &EnumVariant) -> proc_macro2::TokenStream {
let vis = &self.vis;
let name = &self.name;
let ident = &variant.ident;
let predicate = variant.predicate();
let pattern = if variant.escape.is_some() {
quote!(Self::#ident(..))
} else {
quote!(Self::#ident)
};
let doc = format!("Whether this is [`{name}::{ident}`].");
quote! {
#[doc = #doc]
#[inline]
#[must_use]
#vis const fn #predicate(&self) -> ::core::primitive::bool {
::core::matches!(self, #pattern)
}
}
}
fn delegate(&self, delegate: &Delegate) -> proc_macro2::TokenStream {
let vis = &self.vis;
let name = &self.name;
let signature = &delegate.signature;
let method = &signature.ident;
let arguments = signature.inputs.iter().skip(1).filter_map(|argument| {
let syn::FnArg::Typed(typed) = argument else {
return None;
};
match typed.pat.as_ref() {
syn::Pat::Ident(bound) => Some(bound.ident.clone()),
_ => None,
}
});
let body = if delegate.exclusive {
quote! {
let mut __ingraph_remote = #name::remote(&*self);
let __ingraph_answer = __ingraph_remote.#method(#(#arguments),*);
*self = <Self as ::core::convert::From<_>>::from(__ingraph_remote);
__ingraph_answer
}
} else {
quote! {
let __ingraph_remote = #name::remote(self);
__ingraph_remote.#method(#(#arguments),*)
}
};
let doc = if delegate.exclusive {
format!(
"Forwarded to the upstream vocabulary, through [`{name}::remote`] — and WRITTEN BACK.\n\nThe \
upstream value is materialized, mutated, and converted home, so a call costs TWO \
conversions where a shared-receiver delegate costs one. A mirror stores a word rather than \
an upstream value; there is nothing here to mutate in place."
)
} else {
format!("Forwarded to the upstream vocabulary, through [`{name}::remote`].")
};
quote! {
#[doc = #doc]
#[inline]
#vis #signature {
#body
}
}
}
fn mirror_parsing(&self, remote: &Remote) -> proc_macro2::TokenStream {
let name = &self.name;
let path = &remote.path;
let reader = self.slug_map();
let spend = self.spend();
let (err, body, from_str) = if self.refuses() {
let parse = if self.escape().is_some() {
quote!(#spend(text))
} else {
quote!(<#path as ::core::str::FromStr>::from_str(text))
};
let error = self.error();
(
quote!(#error),
quote! {
match #parse {
::core::result::Result::Ok(value) => {
::core::option::Option::Some(::core::convert::From::from(value))
}
::core::result::Result::Err(_) => ::core::option::Option::None,
}
},
quote! {
match #reader(text) {
::core::option::Option::Some(value) => ::core::result::Result::Ok(value),
::core::option::Option::None => ::core::result::Result::Err(#error(())),
}
},
)
} else {
(
quote!(::core::convert::Infallible),
quote! {
::core::option::Option::Some(::core::convert::From::from(#spend(text)))
},
quote! {
::core::result::Result::Ok(::core::convert::From::from(#spend(text)))
},
)
};
quote! {
fn #reader(text: &::core::primitive::str) -> ::core::option::Option<#name> {
#body
}
#[automatically_derived]
impl ::core::fmt::Display for #name {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
::core::fmt::Display::fmt(&#name::remote(self), f)
}
}
#[automatically_derived]
impl ::core::str::FromStr for #name {
type Err = #err;
fn from_str(text: &::core::primitive::str) -> ::core::result::Result<Self, Self::Err> {
#from_str
}
}
}
}
fn mirror_cursor(&self) -> proc_macro2::TokenStream {
let reader = self.slug_map();
self.word_cursor(
"e! {
::std::string::String::as_bytes(&::std::string::ToString::to_string(self))
},
"e!(#reader(text)),
)
}
pub(super) fn word_cursor(
&self,
written: &proc_macro2::TokenStream,
read: &proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
let name = &self.name;
let cursor_value = self.framework(&["cursor"], "CursorValue");
quote! {
#[automatically_derived]
impl #cursor_value for #name {
fn write_cursor(&self, out: &mut ::std::vec::Vec<::core::primitive::u8>) {
out.extend_from_slice(#written);
}
fn read_cursor(bytes: &[::core::primitive::u8]) -> ::core::option::Option<Self> {
match ::core::str::from_utf8(bytes) {
::core::result::Result::Ok(text) => #read,
::core::result::Result::Err(_) => ::core::option::Option::None,
}
}
}
}
}
fn slug_map(&self) -> syn::Ident {
format_ident!("__ingraph_of_slug")
}
fn mirror_storage(&self, database: DatabaseKind) -> proc_macro2::TokenStream {
let reader = self.slug_map();
self.text_storage(
database,
"e!(::std::string::ToString::to_string(self)),
"e!(#reader(stored)),
)
}
pub(super) fn text_storage(
&self,
database: DatabaseKind,
bound: &proc_macro2::TokenStream,
read: &proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
if !database.is_sql() {
return quote!();
}
let name = &self.name;
let gate = self.framework(&["gate"], &database.gate().to_string());
let backend = self.backend(database);
let carrier = self.framework(&["comparison", "sql"], "Carrier");
let query_builder = self.framework(&["sqlx"], "QueryBuilder");
let sqlx_type = self.framework(&["sqlx"], "Type");
let encode = self.framework(&["sqlx"], "Encode");
let decode = self.framework(&["sqlx"], "Decode");
let database_trait = self.framework(&["sqlx"], "Database");
let is_null = self.framework(&["sqlx", "encode"], "IsNull");
let error = self.framework(&["sqlx", "error"], "BoxDynError");
let refused = format!("{name}: the stored text names no variant of this vocabulary");
let stored_as = self.framework(&["column"], "StoredAs");
quote! {
#gate! {
#[automatically_derived]
impl #stored_as<#backend> for #name {
const WORDS: &'static [&'static ::core::primitive::str] =
<::std::string::String as #stored_as<#backend>>::WORDS;
}
#[automatically_derived]
impl #sqlx_type<#backend> for #name {
fn type_info() -> <#backend as #database_trait>::TypeInfo {
<::core::primitive::str as #sqlx_type<#backend>>::type_info()
}
fn compatible(ty: &<#backend as #database_trait>::TypeInfo) -> ::core::primitive::bool {
<::core::primitive::str as #sqlx_type<#backend>>::compatible(ty)
}
}
#[automatically_derived]
impl<'__ingraph> #encode<'__ingraph, #backend> for #name {
fn encode_by_ref(
&self,
buf: &mut <#backend as #database_trait>::ArgumentBuffer,
) -> ::core::result::Result<#is_null, #error> {
<::std::string::String as #encode<'__ingraph, #backend>>::encode(#bound, buf)
}
}
#[automatically_derived]
impl<'__ingraph> #decode<'__ingraph, #backend> for #name {
fn decode(
value: <#backend as #database_trait>::ValueRef<'__ingraph>,
) -> ::core::result::Result<Self, #error> {
let stored =
<&'__ingraph ::core::primitive::str as #decode<'__ingraph, #backend>>::decode(value)?;
match #read {
::core::option::Option::Some(value) => ::core::result::Result::Ok(value),
::core::option::Option::None => ::core::result::Result::Err(#error::from(#refused)),
}
}
}
#[automatically_derived]
impl #carrier<#backend> for #name {
fn push_bind(&self, qb: &mut #query_builder<#backend>) {
qb.push_bind(#bound);
}
}
}
}
}
pub(super) fn scalar_wire(&self) -> proc_macro2::TokenStream {
let name = &self.name;
let gate = self.framework(&["gate"], "if_async_graphql");
let input = self.framework(&["graphql"], "ToGraphqlInput");
let output = self.framework(&["graphql"], "ToGraphqlOutput");
let promoted: Vec<&Delegate> = self.promoted(GraphqlKind::AsyncGraphql).collect();
let scalar = self.mirror_scalar(&promoted);
let object = self.mirror_object(&promoted);
quote! {
const _: () = {
#gate! {
#scalar
#object
#[automatically_derived]
impl #input 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 {
::core::clone::Clone::clone(__ingraph_value)
}
}
#[automatically_derived]
impl #output for #name {
type Output<'__ingraph> = &'__ingraph Self;
#[inline]
fn to_graphql_output(__ingraph_value: &Self) -> &Self {
__ingraph_value
}
}
}
};
}
}
fn mirror_scalar(&self, promoted: &[&Delegate]) -> proc_macro2::TokenStream {
let name = &self.name;
let ag = self.framework(&[], "async_graphql");
let shelf = crate::emit::vendored(&self.krate, "async_graphql");
let scalar = quote! {
impl #ag::ScalarType for #name {
fn parse(__ingraph_value: #ag::Value) -> #ag::InputValueResult<Self> {
let __ingraph_text = ::core::result::Result::map_err(
<::std::string::String as #ag::InputType>::parse(
::core::option::Option::Some(__ingraph_value),
),
#ag::InputValueError::propagate,
)?;
::core::result::Result::map_err(
<Self as ::core::str::FromStr>::from_str(&__ingraph_text),
|__ingraph_refused| #ag::InputValueError::custom(__ingraph_refused),
)
}
fn to_value(&self) -> #ag::Value {
#ag::Value::String(::std::string::ToString::to_string(self))
}
}
};
if promoted.is_empty() {
let sdl = self.sdl_name();
return quote! {
#[#ag::Scalar(crate = #shelf, name = #sdl)]
#scalar
};
}
let sdl = crate::union::input_sdl_name(&self.sdl_name());
quote! {
#scalar
#[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 {
#ag::registry::Registry::create_input_type::<Self, _>(
__ingraph_registry,
#ag::registry::MetaTypeId::Scalar,
|_| #ag::registry::MetaType::Scalar {
name: ::std::string::String::from(#sdl),
description: ::core::option::Option::None,
is_valid: ::core::option::Option::Some(::std::sync::Arc::new(
|__ingraph_value| <Self as #ag::ScalarType>::is_valid(__ingraph_value),
)),
visible: ::core::option::Option::None,
inaccessible: false,
tags: ::std::vec::Vec::new(),
specified_by_url: ::core::option::Option::None,
directive_invocations: ::std::vec::Vec::new(),
requires_scopes: ::std::vec::Vec::new(),
},
)
}
#[inline]
fn parse(
__ingraph_value: ::core::option::Option<#ag::Value>,
) -> #ag::InputValueResult<Self> {
<Self as #ag::ScalarType>::parse(
::core::option::Option::unwrap_or_default(__ingraph_value),
)
}
#[inline]
fn to_value(&self) -> #ag::Value {
<Self as #ag::ScalarType>::to_value(self)
}
#[inline]
fn as_raw_value(&self) -> ::core::option::Option<&Self::RawValueType> {
::core::option::Option::Some(self)
}
}
}
}
fn mirror_object(&self, promoted: &[&Delegate]) -> proc_macro2::TokenStream {
if promoted.is_empty() {
return quote!();
}
let name = &self.name;
let ag = self.framework(&[], "async_graphql");
let sdl = self.sdl_name();
let witness = self.wire_witness();
let text = super::TEXT_FIELD;
let text_ty: syn::Type = syn::parse_quote!(::std::string::String);
let text_crossed = self.crossed(&text_ty, "e!(&__ingraph_word));
let fields = core::iter::once(self.published_field(text, &text_ty)).chain(promoted.iter().map(
|delegate| {
let word = delegate
.word(GraphqlKind::AsyncGraphql)
.expect("a promoted delegate publishes at this backend");
self.published_field(&word, &self.returned(delegate))
},
));
let arms = promoted.iter().map(|delegate| {
let word = delegate
.word(GraphqlKind::AsyncGraphql)
.expect("a promoted delegate publishes at this backend");
let method = &delegate.signature.ident;
let crossed = self.crossed(&self.returned(delegate), "e!(&__ingraph_answer));
quote! {
#word => {
let __ingraph_answer = #name::#method(self);
#ag::ServerResult::map(
#ag::OutputType::resolve(
&#crossed,
&#ag::Context::with_selection_set(
__ingraph_ctx,
&__ingraph_ctx.item.node.selection_set,
),
__ingraph_ctx.item,
).await,
::core::option::Option::Some,
)
}
}
});
quote! {
#witness
#[automatically_derived]
impl #ag::resolver_utils::ContainerType for #name {
async fn resolve_field(
&self,
__ingraph_ctx: &#ag::Context<'_>,
) -> #ag::ServerResult<::core::option::Option<#ag::Value>> {
match #ag::Name::as_str(&__ingraph_ctx.item.node.name.node) {
#text => {
let __ingraph_word = ::std::string::ToString::to_string(self);
#ag::ServerResult::map(
#ag::OutputType::resolve(
&#text_crossed,
&#ag::Context::with_selection_set(
__ingraph_ctx,
&__ingraph_ctx.item.node.selection_set,
),
__ingraph_ctx.item,
).await,
::core::option::Option::Some,
)
}
#(#arms)*
_ => ::core::result::Result::Ok(::core::option::Option::None),
}
}
}
#[automatically_derived]
impl #ag::OutputType for #name {
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 {
#ag::registry::Registry::create_output_type::<Self, _>(
__ingraph_registry,
#ag::registry::MetaTypeId::Object,
|__ingraph_registry| #ag::registry::MetaType::Object {
name: ::std::string::String::from(#sdl),
description: ::core::option::Option::None,
fields: {
let mut __ingraph_fields = #ag::indexmap::IndexMap::new();
#(#fields)*
__ingraph_fields
},
cache_control: ::core::default::Default::default(),
extends: false,
shareable: false,
resolvable: true,
keys: ::core::option::Option::None,
visible: ::core::option::Option::None,
inaccessible: false,
interface_object: false,
tags: ::std::vec::Vec::new(),
is_subscription: false,
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<'_>,
_: &#ag::Positioned<#ag::parser::types::Field>,
) -> #ag::ServerResult<#ag::Value> {
#ag::resolver_utils::resolve_container(__ingraph_ctx, self).await
}
}
#[automatically_derived]
impl #ag::ObjectType for #name {}
}
}
fn published_field(&self, word: &str, returned: &syn::Type) -> proc_macro2::TokenStream {
let ag = self.framework(&[], "async_graphql");
let marker = self.wire_marker();
let ty = quote_spanned! {returned.span()=>
<#returned as #marker>::__ingraph_wire_type(__ingraph_registry)
};
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,
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(),
},
);
}
}
fn returned(&self, delegate: &Delegate) -> syn::Type {
match &delegate.signature.output {
syn::ReturnType::Type(_, ty) => (**ty).clone(),
syn::ReturnType::Default => {
syn::parse_quote_spanned!(delegate.signature.ident.span()=> ())
}
}
}
fn crossed(
&self,
returned: &syn::Type,
value: &proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
let marker = self.wire_marker();
quote_spanned! {returned.span()=>
<#returned as #marker>::__ingraph_wire_value(#value)
}
}
fn wire_marker(&self) -> syn::Ident {
format_ident!("__IngraphWire")
}
fn wire_witness(&self) -> proc_macro2::TokenStream {
let name = &self.name;
let ag = self.framework(&[], "async_graphql");
let crossing = self.framework(&["graphql"], "ToGraphqlOutput");
let marker = self.wire_marker();
let message = format!(
"`{{Self}}` is what a method `{name}` PUBLISHES answers with, and it does not cross into an \
output position"
);
let note = format!(
"a published `delegate(fn …)` becomes a field of the `{name}` object, so its return type \
needs an `ingraph::ToGraphqlOutput` row — the map every wire face returns through. Return a \
type that has one, or drop the `#[graphql(…)]` and keep the method as Rust"
);
quote! {
#[diagnostic::on_unimplemented(
message = #message,
label = "this type has no output crossing",
note = #note,
)]
trait #marker: ::core::marker::Sized + 'static {
type Crossed<'__ingraph>: #ag::OutputType
where
Self: '__ingraph;
fn __ingraph_wire_type(
__ingraph_registry: &mut #ag::registry::Registry,
) -> ::std::string::String;
fn __ingraph_wire_value(&self) -> Self::Crossed<'_>;
}
#[diagnostic::do_not_recommend]
impl<__IngraphReturned> #marker for __IngraphReturned
where
__IngraphReturned: #crossing + 'static,
for<'__ingraph> <__IngraphReturned as #crossing>::Output<'__ingraph>: #ag::OutputType,
{
type Crossed<'__ingraph> = <__IngraphReturned as #crossing>::Output<'__ingraph>;
#[inline]
fn __ingraph_wire_type(
__ingraph_registry: &mut #ag::registry::Registry,
) -> ::std::string::String {
<Self::Crossed<'static> as #ag::OutputType>::create_type_info(__ingraph_registry)
}
#[inline]
fn __ingraph_wire_value(&self) -> Self::Crossed<'_> {
<Self as #crossing>::to_graphql_output(self)
}
}
}
}
}