From 81adc14e0ea3394eca787698dbd4ae578a12f825 Mon Sep 17 00:00:00 2001 From: gh-action-runner Date: Fri, 7 Jun 2024 19:35:07 +0000 Subject: [PATCH] Squashed 'apollo-ios/' changes from 3188d6a6..b52a8610 b52a8610 JSONConverter helper (apollographql/apollo-ios-dev#380) git-subtree-dir: apollo-ios git-subtree-split: b52a86100fd041481a9f191f4a1ab007069a41a1 --- Sources/Apollo/GraphQLError.swift | 7 +--- Sources/Apollo/GraphQLResult.swift | 17 +--------- Sources/Apollo/JSONConverter.swift | 52 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 Sources/Apollo/JSONConverter.swift diff --git a/Sources/Apollo/GraphQLError.swift b/Sources/Apollo/GraphQLError.swift index 6c99dea41..976e67e95 100644 --- a/Sources/Apollo/GraphQLError.swift +++ b/Sources/Apollo/GraphQLError.swift @@ -89,11 +89,6 @@ extension GraphQLError: LocalizedError { extension GraphQLError { func asJSONDictionary() -> [String: Any] { - var dict: [String: Any] = [:] - if let message = self["message"] { dict["message"] = message } - if let locations = self["locations"] { dict["locations"] = locations } - if let path = self["path"] { dict["path"] = path } - if let extensions = self["extensions"] { dict["extensions"] = extensions } - return dict + JSONConverter.convert(self) } } diff --git a/Sources/Apollo/GraphQLResult.swift b/Sources/Apollo/GraphQLResult.swift index d357f8891..695e3c674 100644 --- a/Sources/Apollo/GraphQLResult.swift +++ b/Sources/Apollo/GraphQLResult.swift @@ -55,24 +55,9 @@ extension GraphQLResult { /// - Returns: A `[String: Any]` JSON dictionary representing the ``GraphQLResult``. public func asJSONDictionary() -> [String: Any] { var dict: [String: Any] = [:] - if let data { dict["data"] = convert(value: data.__data) } + if let data { dict["data"] = JSONConverter.convert(data) } if let errors { dict["errors"] = errors.map { $0.asJSONDictionary() } } if let extensions { dict["extensions"] = extensions } return dict } - - private func convert(value: Any) -> Any { - var val: Any = value - if let value = value as? DataDict { - val = value._data - } else if let value = value as? (any CustomScalarType) { - val = value._jsonValue - } - if let dict = val as? [String: Any] { - return dict.mapValues(convert) - } else if let arr = val as? [Any] { - return arr.map(convert) - } - return val - } } diff --git a/Sources/Apollo/JSONConverter.swift b/Sources/Apollo/JSONConverter.swift new file mode 100644 index 000000000..6012eaa11 --- /dev/null +++ b/Sources/Apollo/JSONConverter.swift @@ -0,0 +1,52 @@ +import Foundation +#if !COCOAPODS +import ApolloAPI +#endif + +public enum JSONConverter { + + /// Converts a ``SelectionSet`` into a basic JSON dictionary for use. + /// + /// - Returns: A `[String: Any]` JSON dictionary representing the ``SelectionSet``. + public static func convert(_ selectionSet: some SelectionSet) -> [String: Any] { + selectionSet.__data._data.mapValues(convert(value:)) + } + + static func convert(_ dataDict: DataDict) -> [String: Any] { + dataDict._data.mapValues(convert(value:)) + } + + /// Converts a ``GraphQLResult`` into a basic JSON dictionary for use. + /// + /// - Returns: A `[String: Any]` JSON dictionary representing the ``GraphQLResult``. + public static func convert(_ result: GraphQLResult) -> [String: Any] { + result.asJSONDictionary() + } + + /// Converts a ``GraphQLError`` into a basic JSON dictionary for use. + /// + /// - Returns: A `[String: Any]` JSON dictionary representing the ``GraphQLError``. + public static func convert(_ error: GraphQLError) -> [String: Any] { + var dict: [String: Any] = [:] + if let message = error["message"] { dict["message"] = message } + if let locations = error["locations"] { dict["locations"] = locations } + if let path = error["path"] { dict["path"] = path } + if let extensions = error["extensions"] { dict["extensions"] = extensions } + return dict + } + + private static func convert(value: Any) -> Any { + var val: Any = value + if let value = value as? DataDict { + val = value._data + } else if let value = value as? any CustomScalarType { + val = value._jsonValue + } + if let dict = val as? [String: Any] { + return dict.mapValues(convert) + } else if let arr = val as? [Any] { + return arr.map(convert) + } + return val + } +}