use quote::{ToTokens as _, format_ident};
use super::wire_method;
const PREFIX: &str = "__async_graphql_";
#[test]
fn the_door_mints_the_backend_s_own_namespace() {
assert_eq!(
wire_method(&format_ident!("title")).to_string(),
"__async_graphql_title"
);
assert_eq!(
wire_method(&format_ident!("where")).to_string(),
"__async_graphql_where"
);
}
struct Minted {
ident: String,
named: bool,
context: bool,
}
struct Face {
owner: String,
minted: Vec<Minted>,
arguments: usize,
}
fn is_object(attribute: &syn::Attribute) -> bool {
let path = attribute.path();
path
.segments
.last()
.is_some_and(|segment| segment.ident == "Object")
&& path
.segments
.iter()
.any(|segment| segment.ident == "async_graphql")
}
fn names_itself(attributes: &[syn::Attribute]) -> bool {
attributes.iter().any(|attribute| {
attribute.path().is_ident("graphql")
&& attribute
.to_token_stream()
.to_string()
.replace(' ', "")
.contains("name=")
})
}
fn faces(items: &[syn::Item]) -> Vec<Face> {
let mut found = Vec::new();
for item in items {
match item {
syn::Item::Const(anonymous) => {
if let syn::Expr::Block(block) = &*anonymous.expr {
let held: Vec<syn::Item> = block
.block
.stmts
.iter()
.filter_map(|statement| match statement {
syn::Stmt::Item(item) => Some(item.clone()),
_ => None,
})
.collect();
found.extend(faces(&held));
}
}
syn::Item::Impl(block) if block.attrs.iter().any(is_object) => {
let mut face = Face {
owner: block.self_ty.to_token_stream().to_string().replace(' ', ""),
minted: Vec::new(),
arguments: 0,
};
for item in &block.items {
let syn::ImplItem::Fn(method) = item else {
continue;
};
face.minted.push(Minted {
ident: method.sig.ident.to_string(),
named: names_itself(&method.attrs),
context: false,
});
for argument in &method.sig.inputs {
let syn::FnArg::Typed(typed) = argument else {
continue;
};
let syn::Pat::Ident(named) = &*typed.pat else {
continue;
};
face.arguments += 1;
face.minted.push(Minted {
ident: named.ident.to_string(),
named: names_itself(&typed.attrs),
context: typed.ty.to_token_stream().to_string().contains("Context"),
});
}
}
found.push(face);
}
_ => {}
}
}
found
}
#[test]
fn every_ident_an_emitted_face_mints_wears_the_backend_prefix() {
let mut arguments_seen = 0usize;
for (label, model) in [
("wired", crate::tests::wired()),
("queried", crate::tests::queried()),
] {
let file = crate::tests::emission(&model);
let found = faces(&file.items);
assert!(
!found.is_empty(),
"{label}: the walk found no `#[Object]` face, so the rules below would hold vacuously"
);
for face in found {
assert!(
!face.minted.is_empty(),
"{label}: `{}` is an empty face — an object with no field is not a GraphQL type",
face.owner
);
arguments_seen += face.arguments;
for minted in &face.minted {
assert!(
minted.ident.starts_with(PREFIX),
"{label}: `{}::{}` is minted inside an async-graphql face and is not in that backend's \
namespace — every such ident goes through `method::wire_method`, and a second GraphQL \
binding on this type is what an unprefixed one would collide with",
face.owner,
minted.ident
);
assert!(
minted.named || minted.context,
"{label}: `{}::{}` publishes a name derived from its IDENT, and the ident carries the \
backend's prefix — async-graphql would render it `asyncGraphql…`. Every schema item \
writes `#[graphql(name = \"…\")]`",
face.owner,
minted.ident
);
}
}
}
assert!(
arguments_seen > 0,
"the ARGUMENT half of the law was never exercised — the query unit's legs are what carry it"
);
}