Skip to content

Commit

Permalink
refactor: Added new JSONConvert enum to contain JSON-related operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
TizianoCoroneo committed Jun 3, 2024
1 parent 87e18e4 commit 2cf5f28
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
51 changes: 51 additions & 0 deletions Tests/ApolloTests/JSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import XCTest
@testable import Apollo
import ApolloAPI
import ApolloInternalTestHelpers
import StarWarsAPI

class JSONTests: XCTestCase {
func testMissingValueMatchable() {
Expand Down Expand Up @@ -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)
}
}
5 changes: 1 addition & 4 deletions apollo-ios/Sources/Apollo/GraphQLResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:))
}

Expand Down
21 changes: 21 additions & 0 deletions apollo-ios/Sources/Apollo/JSONConvert.swift
Original file line number Diff line number Diff line change
@@ -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<T>(_ result: GraphQLResult<T>) -> [String: Any] {
result.asJSONDictionary()
}
}

0 comments on commit 2cf5f28

Please sign in to comment.