Make GraphQL interface value enum variants named after the type they contain

This commit is contained in:
tyranron 2020-11-09 14:07:33 +01:00
parent 4dbd740fef
commit bcbf44ecbd
No known key found for this signature in database
GPG key ID: 762E144FB230A4F0

View file

@ -1641,11 +1641,15 @@ impl EnumType {
}
}
/// Returns name of a single variant of this [`EnumType`] by the given positional `num` in the
/// enum type definition.
/// Returns name of a single variant of this [`EnumType`] by the given underlying [`syn::Type`]
/// of the variant.
#[must_use]
fn variant_ident(num: usize) -> syn::Ident {
format_ident!("Impl{}", num)
fn variant_ident(ty: &syn::Type) -> &syn::Ident {
if let syn::Type::Path(p) = ty {
&p.path.segments.last().unwrap().ident
} else {
unreachable!("GraphQL object has unexpected type `{}`", quote! { #ty })
}
}
/// Indicates whether this [`EnumType`] has non-exhaustive phantom variant to hold type
@ -1728,8 +1732,8 @@ impl EnumType {
self.trait_ident,
);
let variants = self.variants.iter().enumerate().map(|(n, ty)| {
let variant = Self::variant_ident(n);
let variants = self.variants.iter().map(|ty| {
let variant = Self::variant_ident(ty);
let doc = format!(
"`{}` implementer of this GraphQL interface.",
quote! { #ty },
@ -1783,8 +1787,8 @@ impl EnumType {
let enum_ty = &self.ident;
let (impl_generics, generics, where_clause) = self.trait_generics.split_for_impl();
self.variants.iter().enumerate().map(move |(n, ty)| {
let variant = Self::variant_ident(n);
self.variants.iter().map(move |ty| {
let variant = Self::variant_ident(ty);
quote! {
#[automatically_derived]
@ -1846,8 +1850,8 @@ impl EnumType {
None
};
let match_arms = self.variants.iter().enumerate().map(|(n, ty)| {
let variant = Self::variant_ident(n);
let match_arms = self.variants.iter().map(|ty| {
let variant = Self::variant_ident(ty);
let args = args.clone();
quote! {
@ -1905,8 +1909,8 @@ impl EnumType {
fn method_concrete_type_name_tokens(&self) -> TokenStream {
let scalar = &self.scalar;
let match_arms = self.variants.iter().enumerate().map(|(n, ty)| {
let variant = Self::variant_ident(n);
let match_arms = self.variants.iter().map(|ty| {
let variant = Self::variant_ident(ty);
quote! {
Self::#variant(v) => <
@ -1932,8 +1936,8 @@ impl EnumType {
fn method_resolve_into_type_tokens(&self) -> TokenStream {
let resolving_code = gen::sync_resolving_code();
let match_arms = self.variants.iter().enumerate().map(|(n, _)| {
let variant = Self::variant_ident(n);
let match_arms = self.variants.iter().map(|ty| {
let variant = Self::variant_ident(ty);
quote! {
Self::#variant(res) => #resolving_code,
@ -1957,8 +1961,8 @@ impl EnumType {
fn method_resolve_into_type_async_tokens(&self) -> TokenStream {
let resolving_code = gen::async_resolving_code(None);
let match_arms = self.variants.iter().enumerate().map(|(n, _)| {
let variant = Self::variant_ident(n);
let match_arms = self.variants.iter().map(|ty| {
let variant = Self::variant_ident(ty);
quote! {
Self::#variant(v) => {