From 2cf5f282a6fee1a0af6f7098918a85b5a5edf470 Mon Sep 17 00:00:00 2001 From: TizianoCoroneo Date: Mon, 3 Jun 2024 14:13:08 +0200 Subject: [PATCH] refactor: Added new JSONConvert enum to contain JSON-related operations. --- Tests/ApolloTests/JSONTests.swift | 51 +++++++++++++++++++ apollo-ios/Sources/Apollo/GraphQLResult.swift | 5 +- apollo-ios/Sources/Apollo/JSONConvert.swift | 21 ++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 apollo-ios/Sources/Apollo/JSONConvert.swift diff --git a/Tests/ApolloTests/JSONTests.swift b/Tests/ApolloTests/JSONTests.swift index 65713e115..59810338c 100644 --- a/Tests/ApolloTests/JSONTests.swift +++ b/Tests/ApolloTests/JSONTests.swift @@ -2,6 +2,7 @@ import XCTest @testable import Apollo import ApolloAPI import ApolloInternalTestHelpers +import StarWarsAPI class JSONTests: XCTestCase { func testMissingValueMatchable() { @@ -99,4 +100,54 @@ class JSONTests: XCTestCase { XCTAssertEqual(stringFromSerialized, #"{"aWeirdNull":null}"#) } + + func testJSONConvertSelectionSetEncoding() throws { + class Hero: MockSelectionSet { + typealias Schema = MockSchemaMetadata + + override class var __selections: [Selection] {[ + .field("__typename", String.self), + .field("name", String?.self) + ]} + + var name: String? { __data["name"] } + } + + let expected: JSONObject = [ + "__typename": "Human", + "name": "Johnny Tsunami" + ] + + let converted = try JSONConvert.selectionSetToJSONObject(Hero(data: expected)) + XCTAssertEqual(converted, expected) + } + + func testJSONConvertGraphQLResultEncoding() throws { + let jsonObj: [String: AnyHashable] = [ + "hero": [ + "name": "Luke Skywalker", + "__typename": "Human" + ] + ] + let heroData = try StarWarsAPI.HeroNameQuery.Data(data: jsonObj) + let result = GraphQLResult( + data: heroData, + extensions: nil, + errors: nil, + source: .server, + dependentKeys: nil + ) + + let expected: [String: Any] = [ + "data": [ + "hero": [ + "name": "Luke Skywalker", + "__typename": "Human" + ] + ] + ] + + let converted = JSONConvert.graphQLResultToJSONObject(result) + XCTAssertEqual(converted, expected) + } } diff --git a/apollo-ios/Sources/Apollo/GraphQLResult.swift b/apollo-ios/Sources/Apollo/GraphQLResult.swift index 0374ea6f3..735b71f10 100644 --- a/apollo-ios/Sources/Apollo/GraphQLResult.swift +++ b/apollo-ios/Sources/Apollo/GraphQLResult.swift @@ -63,10 +63,7 @@ extension GraphQLResult { } extension DataDict { - /// Converts a ``DataDict`` into a basic JSON dictionary for use. - /// - /// - Returns: A `[String: Any]` JSON dictionary representing the ``DataDict``. - public func asJSONDictionary() -> [String: Any] { + internal func asJSONDictionary() -> [String: Any] { _data.mapValues(convert(value:)) } diff --git a/apollo-ios/Sources/Apollo/JSONConvert.swift b/apollo-ios/Sources/Apollo/JSONConvert.swift new file mode 100644 index 000000000..f701077ee --- /dev/null +++ b/apollo-ios/Sources/Apollo/JSONConvert.swift @@ -0,0 +1,21 @@ +import Foundation +#if !COCOAPODS +import ApolloAPI +#endif + +public enum JSONConvert { + + /// Converts a ``SelectionSet`` into a basic JSON dictionary for use. + /// + /// - Returns: A `[String: Any]` JSON dictionary representing the ``SelectionSet``. + public static func selectionSetToJSONObject(_ selectionSet: some SelectionSet) -> [String: Any] { + selectionSet.__data.asJSONDictionary() + } + + /// Converts a ``GraphQLResult`` into a basic JSON dictionary for use. + /// + /// - Returns: A `[String: Any]` JSON dictionary representing the ``GraphQLResult``. + public static func graphQLResultToJSONObject(_ result: GraphQLResult) -> [String: Any] { + result.asJSONDictionary() + } +}