Skip to content

Commit c43f880

Browse files
diegorozenmartijnwalraven
authored andcommitted
Fixes #435 Invoking Query.Data(jsonObject:) fails for queries with params … (#437)
1 parent 86b3b65 commit c43f880

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

Apollo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
9FF90A711DDDEB420034C3B6 /* ReadFieldValueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FF90A6B1DDDEB420034C3B6 /* ReadFieldValueTests.swift */; };
9696
9FF90A731DDDEB420034C3B6 /* ParseQueryResponseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FF90A6C1DDDEB420034C3B6 /* ParseQueryResponseTests.swift */; };
9797
E86D8E05214B32FD0028EFE1 /* JSONTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E86D8E03214B32DA0028EFE1 /* JSONTests.swift */; };
98+
F16D083C21EF6F7300C458B8 /* QueryFromJSONBuildingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F16D083B21EF6F7300C458B8 /* QueryFromJSONBuildingTests.swift */; };
9899
/* End PBXBuildFile section */
99100

100101
/* Begin PBXContainerItemProxy section */
@@ -333,6 +334,7 @@
333334
9FF90A6B1DDDEB420034C3B6 /* ReadFieldValueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadFieldValueTests.swift; sourceTree = "<group>"; };
334335
9FF90A6C1DDDEB420034C3B6 /* ParseQueryResponseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParseQueryResponseTests.swift; sourceTree = "<group>"; };
335336
E86D8E03214B32DA0028EFE1 /* JSONTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONTests.swift; sourceTree = "<group>"; };
337+
F16D083B21EF6F7300C458B8 /* QueryFromJSONBuildingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryFromJSONBuildingTests.swift; sourceTree = "<group>"; };
336338
/* End PBXFileReference section */
337339

338340
/* Begin PBXFrameworksBuildPhase section */
@@ -560,6 +562,7 @@
560562
9F295E301E27534800A24949 /* NormalizeQueryResults.swift */,
561563
9FF90A6C1DDDEB420034C3B6 /* ParseQueryResponseTests.swift */,
562564
9FE1C6E61E634C8D00C02284 /* PromiseTests.swift */,
565+
F16D083B21EF6F7300C458B8 /* QueryFromJSONBuildingTests.swift */,
563566
9FF90A6B1DDDEB420034C3B6 /* ReadFieldValueTests.swift */,
564567
9F19D8451EED8D3B00C57247 /* ResultOrPromiseTests.swift */,
565568
);
@@ -1128,6 +1131,7 @@
11281131
9FADC8541E6B86D900C677E6 /* DataLoaderTests.swift in Sources */,
11291132
E86D8E05214B32FD0028EFE1 /* JSONTests.swift in Sources */,
11301133
9F8622FA1EC2117C00C38162 /* FragmentConstructionAndConversionTests.swift in Sources */,
1134+
F16D083C21EF6F7300C458B8 /* QueryFromJSONBuildingTests.swift in Sources */,
11311135
9FF90A711DDDEB420034C3B6 /* ReadFieldValueTests.swift in Sources */,
11321136
9F295E311E27534800A24949 /* NormalizeQueryResults.swift in Sources */,
11331137
9FF90A731DDDEB420034C3B6 /* ParseQueryResponseTests.swift in Sources */,

Sources/Apollo/GraphQLSelectionSet.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public protocol GraphQLSelectionSet {
88
}
99

1010
public extension GraphQLSelectionSet {
11-
init(jsonObject: JSONObject) throws {
11+
init(jsonObject: JSONObject, variables: GraphQLMap? = nil) throws {
1212
let executor = GraphQLExecutor { object, info in
1313
.result(.success(object[info.responseKeyForField]))
1414
}
1515
executor.shouldComputeCachePath = false
16-
self = try executor.execute(selections: Self.selections, on: jsonObject, accumulator: GraphQLSelectionSetMapper<Self>()).await()
16+
self = try executor.execute(selections: Self.selections, on: jsonObject, variables: variables, accumulator: GraphQLSelectionSetMapper<Self>()).await()
1717
}
1818

1919
var jsonObject: JSONObject {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import XCTest
2+
@testable import Apollo
3+
import ApolloTestSupport
4+
import StarWarsAPI
5+
6+
class QueryFromJSONBuildingTests: XCTestCase {
7+
func testHeroDetailsWithFragmentQueryHuman() throws {
8+
let jsonObject = [
9+
"hero": ["__typename": "Human", "name": "Luke Skywalker", "height": 1.72]
10+
]
11+
12+
let data = try HeroDetailsWithFragmentQuery.Data(jsonObject: jsonObject)
13+
14+
guard let human = data.hero?.fragments.heroDetails.asHuman else {
15+
XCTFail("Wrong type")
16+
return
17+
}
18+
19+
XCTAssertEqual(human.height, 1.72)
20+
}
21+
22+
func testConditionalInclusionQuery() throws {
23+
let jsonObject = [
24+
"hero": [
25+
"__typename": "Hero",
26+
"name": "R2-D2"
27+
]
28+
]
29+
30+
let nameData = try HeroNameConditionalInclusionQuery.Data(jsonObject: jsonObject, variables: ["includeName" : true])
31+
XCTAssertEqual(nameData.hero?.name, "R2-D2")
32+
33+
let noNameData = try HeroNameConditionalInclusionQuery.Data(jsonObject: jsonObject, variables: ["includeName" : false])
34+
XCTAssertNil(noNameData.hero?.name)
35+
}
36+
37+
func testConditionalInclusionQueryWithoutVariables() throws {
38+
let jsonObject = [
39+
"hero": [
40+
"__typename": "Hero",
41+
"name": "R2-D2"
42+
]
43+
]
44+
45+
XCTAssertThrowsError(try HeroNameConditionalInclusionQuery.Data(jsonObject: jsonObject)) { error in
46+
if case let error as GraphQLResultError = error {
47+
XCTAssertEqual(error.path, ["hero"])
48+
} else {
49+
XCTFail("Unexpected error: \(error)")
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)