From c21c6838105acae842ca1fc28082dc5475d9c837 Mon Sep 17 00:00:00 2001 From: Calvin Cestari <146856+calvincestari@users.noreply.github.com> Date: Tue, 28 Jan 2025 13:48:20 -0800 Subject: [PATCH] Refactor to reduce duplicated code --- .../IR/GraphQLCompositeType+Identifiable.swift | 16 ++++++++++++++++ .../Sources/IR/IR+ComputedSelectionSet.swift | 13 ++----------- .../Sources/IR/IR+NamedFragment.swift | 9 +-------- 3 files changed, 19 insertions(+), 19 deletions(-) create mode 100644 apollo-ios-codegen/Sources/IR/GraphQLCompositeType+Identifiable.swift diff --git a/apollo-ios-codegen/Sources/IR/GraphQLCompositeType+Identifiable.swift b/apollo-ios-codegen/Sources/IR/GraphQLCompositeType+Identifiable.swift new file mode 100644 index 000000000..8f6ea03c2 --- /dev/null +++ b/apollo-ios-codegen/Sources/IR/GraphQLCompositeType+Identifiable.swift @@ -0,0 +1,16 @@ +import GraphQLCompiler + +extension GraphQLCompositeType { + var isIdentifiable: Bool { + switch(self) { + case let interface as GraphQLInterfaceType: + return interface.keyFields == ["id"] + + case let object as GraphQLObjectType: + return object.keyFields == ["id"] + + default: + return false + } + } +} diff --git a/apollo-ios-codegen/Sources/IR/IR+ComputedSelectionSet.swift b/apollo-ios-codegen/Sources/IR/IR+ComputedSelectionSet.swift index dec4127fc..b1388a7a2 100644 --- a/apollo-ios-codegen/Sources/IR/IR+ComputedSelectionSet.swift +++ b/apollo-ios-codegen/Sources/IR/IR+ComputedSelectionSet.swift @@ -25,17 +25,8 @@ public struct ComputedSelectionSet { guard direct?.fields["id"] != nil || merged.fields["id"] != nil else { return false } - if let type = typeInfo.parentType as? GraphQLObjectType, - type.keyFields == ["id"] { - return true - } - - if let type = typeInfo.parentType as? GraphQLInterfaceType, - type.keyFields == ["id"] { - return true - } - - return false + + return typeInfo.parentType.isIdentifiable } // MARK: Dynamic Member Subscript diff --git a/apollo-ios-codegen/Sources/IR/IR+NamedFragment.swift b/apollo-ios-codegen/Sources/IR/IR+NamedFragment.swift index c70125ad2..db125c329 100644 --- a/apollo-ios-codegen/Sources/IR/IR+NamedFragment.swift +++ b/apollo-ios-codegen/Sources/IR/IR+NamedFragment.swift @@ -30,14 +30,7 @@ public class NamedFragment: Definition, Hashable, CustomDebugStringConvertible { return field.name == "id" }) else { return false } - switch(type) { - case let interface as GraphQLInterfaceType: - return interface.keyFields == ["id"] - case let object as GraphQLObjectType: - return object.keyFields == ["id"] - default: - return false - } + return type.isIdentifiable } init(