From 18108c308a87677ff6fc216ad2cb16bc310472da Mon Sep 17 00:00:00 2001 From: Anthony Miller Date: Tue, 5 Sep 2023 11:44:35 -0700 Subject: [PATCH] Remove stripping of null values from Selection Set models (apollographql/apollo-ios-dev#25) --- Sources/Apollo/ApolloStore.swift | 1 - Sources/Apollo/GraphQLSelectionSetMapper.swift | 13 +++++++++---- Sources/ApolloAPI/DataDict.swift | 4 +--- ...utor_SelectionSetMapper_FromResponse_Tests.swift | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Sources/Apollo/ApolloStore.swift b/Sources/Apollo/ApolloStore.swift index e2ad5862e2..cc4ea7c3cf 100644 --- a/Sources/Apollo/ApolloStore.swift +++ b/Sources/Apollo/ApolloStore.swift @@ -278,7 +278,6 @@ public class ApolloStore { withKey: key, variables: variables, accumulator: GraphQLSelectionSetMapper( - stripNullValues: false, handleMissingValues: .allowForOptionalFields ) ) diff --git a/Sources/Apollo/GraphQLSelectionSetMapper.swift b/Sources/Apollo/GraphQLSelectionSetMapper.swift index 4583674495..d67907574e 100644 --- a/Sources/Apollo/GraphQLSelectionSetMapper.swift +++ b/Sources/Apollo/GraphQLSelectionSetMapper.swift @@ -7,7 +7,6 @@ final class GraphQLSelectionSetMapper: GraphQLResultAccumulator let requiresCacheKeyComputation: Bool = false - let stripNullValues: Bool let handleMissingValues: HandleMissingValues enum HandleMissingValues { @@ -19,10 +18,8 @@ final class GraphQLSelectionSetMapper: GraphQLResultAccumulator } init( - stripNullValues: Bool = true, handleMissingValues: HandleMissingValues = .disallow ) { - self.stripNullValues = stripNullValues self.handleMissingValues = handleMissingValues } @@ -48,7 +45,7 @@ final class GraphQLSelectionSetMapper: GraphQLResultAccumulator } func acceptNullValue(info: FieldExecutionInfo) -> AnyHashable? { - return stripNullValues ? nil : Optional.none + return DataDict.NullValue } func acceptMissingValue(info: FieldExecutionInfo) throws -> AnyHashable? { @@ -89,3 +86,11 @@ final class GraphQLSelectionSetMapper: GraphQLResultAccumulator return T.init(_dataDict: rootValue) } } + +// MARK: - Null Value Definition +extension DataDict { + /// A common value used to represent a null value in a `DataDict`. + /// + /// This value can be cast to `NSNull` and will bridge automatically. + static let NullValue = AnyHashable(Optional.none) +} diff --git a/Sources/ApolloAPI/DataDict.swift b/Sources/ApolloAPI/DataDict.swift index c8651384b6..d5e458df26 100644 --- a/Sources/ApolloAPI/DataDict.swift +++ b/Sources/ApolloAPI/DataDict.swift @@ -126,9 +126,7 @@ public struct DataDict: Hashable { } } - - -// MARK: Value Conversion Helpers +// MARK: - Value Conversion Helpers public protocol SelectionSetEntityValue { /// - Warning: This function is not supported for external use. diff --git a/Tests/ApolloTests/GraphQLExecutor_SelectionSetMapper_FromResponse_Tests.swift b/Tests/ApolloTests/GraphQLExecutor_SelectionSetMapper_FromResponse_Tests.swift index dbd0331e99..d1222dafe7 100644 --- a/Tests/ApolloTests/GraphQLExecutor_SelectionSetMapper_FromResponse_Tests.swift +++ b/Tests/ApolloTests/GraphQLExecutor_SelectionSetMapper_FromResponse_Tests.swift @@ -23,7 +23,7 @@ class GraphQLExecutor_SelectionSetMapper_FromResponse_Tests: XCTestCase { selectionSet: selectionSet, on: object, variables: variables, - accumulator: GraphQLSelectionSetMapper(stripNullValues: true) + accumulator: GraphQLSelectionSetMapper() ) }