Skip to content

Commit bcbf44e

Browse files
committed
Make GraphQL interface value enum variants named after the type they contain
1 parent 4dbd740 commit bcbf44e

File tree

1 file changed

+20
-16
lines changed
  • juniper_codegen/src/graphql_interface

1 file changed

+20
-16
lines changed

juniper_codegen/src/graphql_interface/mod.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,11 +1641,15 @@ impl EnumType {
16411641
}
16421642
}
16431643

1644-
/// Returns name of a single variant of this [`EnumType`] by the given positional `num` in the
1645-
/// enum type definition.
1644+
/// Returns name of a single variant of this [`EnumType`] by the given underlying [`syn::Type`]
1645+
/// of the variant.
16461646
#[must_use]
1647-
fn variant_ident(num: usize) -> syn::Ident {
1648-
format_ident!("Impl{}", num)
1647+
fn variant_ident(ty: &syn::Type) -> &syn::Ident {
1648+
if let syn::Type::Path(p) = ty {
1649+
&p.path.segments.last().unwrap().ident
1650+
} else {
1651+
unreachable!("GraphQL object has unexpected type `{}`", quote! { #ty })
1652+
}
16491653
}
16501654

16511655
/// Indicates whether this [`EnumType`] has non-exhaustive phantom variant to hold type
@@ -1728,8 +1732,8 @@ impl EnumType {
17281732
self.trait_ident,
17291733
);
17301734

1731-
let variants = self.variants.iter().enumerate().map(|(n, ty)| {
1732-
let variant = Self::variant_ident(n);
1735+
let variants = self.variants.iter().map(|ty| {
1736+
let variant = Self::variant_ident(ty);
17331737
let doc = format!(
17341738
"`{}` implementer of this GraphQL interface.",
17351739
quote! { #ty },
@@ -1783,8 +1787,8 @@ impl EnumType {
17831787
let enum_ty = &self.ident;
17841788
let (impl_generics, generics, where_clause) = self.trait_generics.split_for_impl();
17851789

1786-
self.variants.iter().enumerate().map(move |(n, ty)| {
1787-
let variant = Self::variant_ident(n);
1790+
self.variants.iter().map(move |ty| {
1791+
let variant = Self::variant_ident(ty);
17881792

17891793
quote! {
17901794
#[automatically_derived]
@@ -1846,8 +1850,8 @@ impl EnumType {
18461850
None
18471851
};
18481852

1849-
let match_arms = self.variants.iter().enumerate().map(|(n, ty)| {
1850-
let variant = Self::variant_ident(n);
1853+
let match_arms = self.variants.iter().map(|ty| {
1854+
let variant = Self::variant_ident(ty);
18511855
let args = args.clone();
18521856

18531857
quote! {
@@ -1905,8 +1909,8 @@ impl EnumType {
19051909
fn method_concrete_type_name_tokens(&self) -> TokenStream {
19061910
let scalar = &self.scalar;
19071911

1908-
let match_arms = self.variants.iter().enumerate().map(|(n, ty)| {
1909-
let variant = Self::variant_ident(n);
1912+
let match_arms = self.variants.iter().map(|ty| {
1913+
let variant = Self::variant_ident(ty);
19101914

19111915
quote! {
19121916
Self::#variant(v) => <
@@ -1932,8 +1936,8 @@ impl EnumType {
19321936
fn method_resolve_into_type_tokens(&self) -> TokenStream {
19331937
let resolving_code = gen::sync_resolving_code();
19341938

1935-
let match_arms = self.variants.iter().enumerate().map(|(n, _)| {
1936-
let variant = Self::variant_ident(n);
1939+
let match_arms = self.variants.iter().map(|ty| {
1940+
let variant = Self::variant_ident(ty);
19371941

19381942
quote! {
19391943
Self::#variant(res) => #resolving_code,
@@ -1957,8 +1961,8 @@ impl EnumType {
19571961
fn method_resolve_into_type_async_tokens(&self) -> TokenStream {
19581962
let resolving_code = gen::async_resolving_code(None);
19591963

1960-
let match_arms = self.variants.iter().enumerate().map(|(n, _)| {
1961-
let variant = Self::variant_ident(n);
1964+
let match_arms = self.variants.iter().map(|ty| {
1965+
let variant = Self::variant_ident(ty);
19621966

19631967
quote! {
19641968
Self::#variant(v) => {

0 commit comments

Comments
 (0)