Skip to content

Commit

Permalink
Fixing bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed Sep 21, 2021
1 parent cc882a0 commit 4bd5a93
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
18 changes: 13 additions & 5 deletions Sources/Apollo/GraphQLExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ final class GraphQLExecutor {
}
case let .typeCase(typeCase):
#warning("Write extensive tests for this.")
#warning("Write extensive tests for this. Multiple merged sibling type cases.")
if let runtimeType = runtimeType, runtimeType._canBeConverted(to: typeCase.typeCondition) {
try groupFields(typeCase.selectionSet.selections,
forRuntimeType: runtimeType,
Expand Down Expand Up @@ -390,10 +390,18 @@ final class GraphQLExecutor {
elementFieldInfo.cachePath.append(indexSegment)
}
return self.complete(fields: elementFieldInfo,
withValue: element,
asType: innerType,
accumulator: accumulator)
return self
.complete(fields: elementFieldInfo,
withValue: element,
asType: innerType,
accumulator: accumulator)
.mapError { error in
if !(error is GraphQLResultError) {
return GraphQLResultError(path: elementFieldInfo.responsePath, underlying: error)
} else {
return error
}
}
}
return lazilyEvaluateAll(completedArray).map {
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApolloAPI/CodegenV1/GraphQLEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where T: RawRepresentable & CaseIterable, T.RawValue == String {
extension GraphQLEnum: CustomScalarType {
public init(jsonValue: JSONValue) throws {
guard let stringData = jsonValue as? String else {
throw CacheError.Reason.unrecognizedCacheData(jsonValue, forType: Self.self)
throw JSONDecodingError.couldNotConvert(value: jsonValue, to: String.self)
}
self.init(rawValue: stringData)
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/ApolloAPI/ScalarTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extension ScalarType {
public var asInputValue: InputValue { .scalar(self) }
}

#warning("TODO: do we really need to have both scalar and custom scalar?")
public protocol CustomScalarType:
AnyScalarType,
JSONDecodable,
Expand All @@ -36,5 +37,7 @@ extension CustomScalarType {
try Self.init(jsonValue: cacheData)
}

@inlinable public static var asOutputType: Selection.Field.OutputType { .customScalar(self) }
@inlinable public static var asOutputType: Selection.Field.OutputType {
.nonNull(.customScalar(self))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class GraphQLExecutor_SelectionSetMapper_FromResponse_Tests: XCTestCase {
class GivenSelectionSet: MockSelectionSet {
override class var selections: [Selection] { [Selection.field("name", String?.self)] }
}
let object: JSONObject = ["name": "Luke Skywalker"]
let object: JSONObject = [:]

// when
XCTAssertThrowsError(try readValues(GivenSelectionSet.self, from: object)) { (error) in
Expand Down Expand Up @@ -257,7 +257,9 @@ class GraphQLExecutor_SelectionSetMapper_FromResponse_Tests: XCTestCase {
func test__nonnull_enum__givenDataHasNullValueForField_throwsNullValueError() {
// given
class GivenSelectionSet: MockSelectionSet {
override class var selections: [Selection] { [Selection.field("size", GraphQLEnum<MockEnum>.self)] }
override class var selections: [Selection] {[
Selection.field("size", GraphQLEnum<MockEnum>.self)
]}
}
let object: JSONObject = ["size": NSNull()]

Expand Down Expand Up @@ -419,16 +421,15 @@ class GraphQLExecutor_SelectionSetMapper_FromResponse_Tests: XCTestCase {
class GivenSelectionSet: MockSelectionSet {
override class var selections: [Selection] { [Selection.field("favorites", [String].self)] }
}
let object: JSONObject = ["favorites": [10, 20.0, 30]]
let object: JSONObject = ["favorites": [10.0, 20.0, 30]]

// when
XCTAssertThrowsError(try readValues(GivenSelectionSet.self, from: object)) { (error) in
// then
if let error = error as? GraphQLResultError,
case JSONDecodingError.couldNotConvert(let value, let expectedType) = error.underlying {
XCTAssertEqual(error.path, ["favorites", "1"])
#warning("Does this need the index 1 path?")
XCTAssertEqual(value as? Double, 4.0)
XCTAssertEqual(error.path, ["favorites", "0"])
XCTAssertEqual(value as? Double, 10.0)
XCTAssertTrue(expectedType == String.self)
} else {
XCTFail("Unexpected error: \(error)")
Expand Down Expand Up @@ -518,15 +519,14 @@ class GraphQLExecutor_SelectionSetMapper_FromResponse_Tests: XCTestCase {
class GivenSelectionSet: MockSelectionSet {
override class var selections: [Selection] { [Selection.field("favorites", [String]?.self)] }
}
let object: JSONObject = ["favorites": [10, 20.0, 30]]
let object: JSONObject = ["favorites": [4.0, 20.0, 30]]

// when
XCTAssertThrowsError(try readValues(GivenSelectionSet.self, from: object)) { (error) in
// then
if let error = error as? GraphQLResultError,
case JSONDecodingError.couldNotConvert(let value, let expectedType) = error.underlying {
XCTAssertEqual(error.path, ["favorites", "1"])
#warning("Does this need the index 1 path?")
XCTAssertEqual(error.path, ["favorites", "0"])
XCTAssertEqual(value as? Double, 4.0)
XCTAssertTrue(expectedType == String.self)
} else {
Expand Down

0 comments on commit 4bd5a93

Please sign in to comment.