Skip to content

Commit

Permalink
Cleaned up JSONConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed Jun 6, 2024
1 parent da0f5f6 commit 4a743b3
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 66 deletions.
17 changes: 4 additions & 13 deletions .package.resolved
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
{
"pins" : [
{
"identity" : "apollo-ios",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apollographql/apollo-ios.git",
"state" : {
"revision" : "99dfbdb869dbba16bd09e086b2b669bfcab811ec",
"version" : "1.12.2"
}
},
{
"identity" : "cwlcatchexception",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -50,17 +41,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/stephencelis/SQLite.swift.git",
"state" : {
"revision" : "f0af5e0a2b6917ae38e3019e3456fd0fa9365864",
"version" : "0.15.1"
"revision" : "a95fc6df17d108bd99210db5e8a9bac90fe984b8",
"version" : "0.15.3"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "46989693916f56d1186bd59ac15124caef896560",
"version" : "1.3.1"
"revision" : "0fbc8848e389af3bb55c182bc19ca9d5dc2f255b",
"version" : "1.4.0"
}
},
{
Expand Down
30 changes: 26 additions & 4 deletions Tests/ApolloTests/JSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import XCTest
@testable import Apollo
import ApolloAPI
import ApolloInternalTestHelpers
import StarWarsAPI

class JSONTests: XCTestCase {
func testMissingValueMatchable() {
Expand Down Expand Up @@ -118,18 +117,41 @@ class JSONTests: XCTestCase {
"name": "Johnny Tsunami"
]

let converted = try JSONConvert.selectionSetToJSONObject(Hero(data: expected))
let converted = try JSONConverter.convert(Hero(data: expected))
XCTAssertEqual(converted, expected)
}

func testJSONConvertGraphQLResultEncoding() throws {
class MockData: MockSelectionSet {
typealias Schema = MockSchemaMetadata

override class var __selections: [Selection] {[
.field("hero", Hero?.self)
]}

var hero: Hero? { __data["hero"] }

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 jsonObj: [String: AnyHashable] = [
"hero": [
"name": "Luke Skywalker",
"__typename": "Human"
]
]
let heroData = try StarWarsAPI.HeroNameQuery.Data(data: jsonObj)

let heroData = try MockData(data: jsonObj)

let result = GraphQLResult(
data: heroData,
extensions: nil,
Expand All @@ -147,7 +169,7 @@ class JSONTests: XCTestCase {
]
]

let converted = JSONConvert.graphQLResultToJSONObject(result)
let converted = JSONConverter.convert(result)
XCTAssertEqual(converted, expected)
}
}
7 changes: 1 addition & 6 deletions apollo-ios/Sources/Apollo/GraphQLError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
23 changes: 1 addition & 22 deletions apollo-ios/Sources/Apollo/GraphQLResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +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"] = data.__data.asJSONDictionary() }
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
}
}

extension DataDict {
internal func asJSONDictionary() -> [String: Any] {
_data.mapValues(convert(value:))
}

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
}
}
21 changes: 0 additions & 21 deletions apollo-ios/Sources/Apollo/JSONConvert.swift

This file was deleted.

52 changes: 52 additions & 0 deletions apollo-ios/Sources/Apollo/JSONConverter.swift
Original file line number Diff line number Diff line change
@@ -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<T>(_ result: GraphQLResult<T>) -> [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
}
}

0 comments on commit 4a743b3

Please sign in to comment.