From b44d0b4fff4192f67b4ab5c5c737620674c39421 Mon Sep 17 00:00:00 2001 From: Anthony Miller Date: Thu, 31 Aug 2023 13:10:46 -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 +--- 3 files changed, 10 insertions(+), 8 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.