From 6ef10cd9796be3bf60f746a42cd23de867270eba Mon Sep 17 00:00:00 2001 From: Dariusz Kuc <9501705+dariuszkuc@users.noreply.github.com> Date: Thu, 12 Oct 2023 14:39:35 -0500 Subject: [PATCH] fix: simplify branching logic when applying fed directives (#8) --- src/Federation/FederationTypeInterceptor.cs | 77 ++++++++++++--------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/src/Federation/FederationTypeInterceptor.cs b/src/Federation/FederationTypeInterceptor.cs index d6f0f1e..e11c6eb 100644 --- a/src/Federation/FederationTypeInterceptor.cs +++ b/src/Federation/FederationTypeInterceptor.cs @@ -243,7 +243,7 @@ private void AddFederationDirectiveMarkers(DefinitionBase? definition) { descriptor.Inaccessible(); } - if (attribute is ApolloTagAttribute casted) + else if (attribute is ApolloTagAttribute casted) { descriptor.ApolloTag(casted.Name); } @@ -251,16 +251,16 @@ private void AddFederationDirectiveMarkers(DefinitionBase? definition) foreach (EnumValueDefinition enumValueDefinition in enumTypeDefinition.Values) { - var enumValueDescriptor = EnumValueDescriptor.From(_context, enumValueDefinition); if (enumValueDefinition.Member != null) { + var enumValueDescriptor = EnumValueDescriptor.From(_context, enumValueDefinition); foreach (var attribute in enumValueDefinition.Member.GetCustomAttributes(true)) { if (attribute is InaccessibleAttribute) { enumValueDescriptor.Inaccessible(); } - if (attribute is ApolloTagAttribute casted) + else if (attribute is ApolloTagAttribute casted) { enumValueDescriptor.ApolloTag(casted.Name); } @@ -272,14 +272,13 @@ private void AddFederationDirectiveMarkers(DefinitionBase? definition) case InterfaceTypeDefinition interfaceTypeDefinition: { var descriptor = InterfaceTypeDescriptor.From(_context, interfaceTypeDefinition); - foreach (var attribute in interfaceTypeDefinition.RuntimeType.GetCustomAttributes(true)) { if (attribute is InaccessibleAttribute) { descriptor.Inaccessible(); } - if (attribute is ApolloTagAttribute casted) + else if (attribute is ApolloTagAttribute casted) { descriptor.ApolloTag(casted.Name); } @@ -295,7 +294,7 @@ private void AddFederationDirectiveMarkers(DefinitionBase? definition) { fieldDescriptor.Inaccessible(); } - if (attribute is ApolloTagAttribute casted) + else if (attribute is ApolloTagAttribute casted) { fieldDescriptor.ApolloTag(casted.Name); } @@ -313,7 +312,7 @@ private void AddFederationDirectiveMarkers(DefinitionBase? definition) { descriptor.Inaccessible(); } - if (attribute is ApolloTagAttribute casted) + else if (attribute is ApolloTagAttribute casted) { descriptor.ApolloTag(casted.Name); } @@ -321,16 +320,16 @@ private void AddFederationDirectiveMarkers(DefinitionBase? definition) } foreach (InputFieldDefinition fieldDefinition in inputObjectTypeDefinition.Fields) { - var fieldDescriptor = InputFieldDescriptor.From(_context, fieldDefinition); if (fieldDefinition.RuntimeType != null) { + var fieldDescriptor = InputFieldDescriptor.From(_context, fieldDefinition); foreach (var attribute in fieldDefinition.RuntimeType.GetCustomAttributes(true)) { if (attribute is InaccessibleAttribute) { fieldDescriptor.Inaccessible(); } - if (attribute is ApolloTagAttribute casted) + else if (attribute is ApolloTagAttribute casted) { fieldDescriptor.ApolloTag(casted.Name); } @@ -342,40 +341,53 @@ private void AddFederationDirectiveMarkers(DefinitionBase? definition) case ObjectTypeDefinition objectTypeDefinition: { var descriptor = ObjectTypeDescriptor.From(_context, objectTypeDefinition); - foreach (var attribute in objectTypeDefinition.RuntimeType.GetCustomAttributes(true)) { - if (attribute is InaccessibleAttribute) - { - descriptor.Inaccessible(); - } - if (attribute is ShareableAttribute) + switch (attribute) { - descriptor.Shareable(); - } - if (attribute is ApolloTagAttribute casted) - { - descriptor.ApolloTag(casted.Name); + case ApolloTagAttribute tag: + { + descriptor.ApolloTag(tag.Name); + break; + } + case InaccessibleAttribute: + { + descriptor.Inaccessible(); + break; + } + case ShareableAttribute: + { + descriptor.Shareable(); + break; + } + default: break; } } foreach (ObjectFieldDefinition fieldDefinition in objectTypeDefinition.Fields) { - var fieldDescriptor = ObjectFieldDescriptor.From(_context, fieldDefinition); if (fieldDefinition.Member != null) { + var fieldDescriptor = ObjectFieldDescriptor.From(_context, fieldDefinition); foreach (var attribute in fieldDefinition.Member.GetCustomAttributes(true)) { - if (attribute is InaccessibleAttribute) + switch (attribute) { - fieldDescriptor.Inaccessible(); - } - if (attribute is ShareableAttribute) - { - fieldDescriptor.Shareable(); - } - if (attribute is ApolloTagAttribute casted) - { - fieldDescriptor.ApolloTag(casted.Name); + case ApolloTagAttribute tag: + { + fieldDescriptor.ApolloTag(tag.Name); + break; + } + case InaccessibleAttribute: + { + fieldDescriptor.Inaccessible(); + break; + } + case ShareableAttribute: + { + fieldDescriptor.Shareable(); + break; + } + default: break; } } } @@ -385,14 +397,13 @@ private void AddFederationDirectiveMarkers(DefinitionBase? definition) case UnionTypeDefinition unionTypeDefinition: { var descriptor = UnionTypeDescriptor.From(_context, unionTypeDefinition); - foreach (var attribute in unionTypeDefinition.RuntimeType.GetCustomAttributes(true)) { if (attribute is InaccessibleAttribute) { descriptor.Inaccessible(); } - if (attribute is ApolloTagAttribute casted) + else if (attribute is ApolloTagAttribute casted) { descriptor.ApolloTag(casted.Name); }