diff --git a/Tests/ApolloTests/Cache/ReadWriteFromStoreTests.swift b/Tests/ApolloTests/Cache/ReadWriteFromStoreTests.swift index 691b889b3b..b2210ee1c5 100644 --- a/Tests/ApolloTests/Cache/ReadWriteFromStoreTests.swift +++ b/Tests/ApolloTests/Cache/ReadWriteFromStoreTests.swift @@ -432,6 +432,80 @@ class ReadWriteFromStoreTests: XCTestCase, CacheDependentTesting, StoreLoading { } } } + + func test_updateCacheMutationWithOptionalField_updateNestedField_updatesObjects() throws { + // given + struct GivenSelectionSet: MockMutableRootSelectionSet { + public var __data: DataDict = DataDict([:], variables: nil) + init(data: DataDict) { __data = data } + + static var __selections: [Selection] { [ + .field("hero", Hero.self) + ]} + + var hero: Hero { + get { __data["hero"] } + set { __data["hero"] = newValue } + } + + struct Hero: MockMutableRootSelectionSet { + public var __data: DataDict = DataDict([:], variables: nil) + init(data: DataDict) { __data = data } + + static var __selections: [Selection] { [ + .field("name", String.self), + .field("nickname", String?.self) + ]} + + var name: String { + get { __data["name"] } + set { __data["name"] = newValue } + } + + var nickname: String? { + get { __data["nickname"] } + set { __data["nickname"] = newValue } + } + } + } + + let cacheMutation = MockLocalCacheMutation() + + mergeRecordsIntoCache([ + "QUERY_ROOT": ["hero": CacheReference("QUERY_ROOT.hero")], + "QUERY_ROOT.hero": ["__typename": "Droid", "name": "R2-D2", "nickname": NSNull()] + ]) + + runActivity("update mutation") { _ in + let updateCompletedExpectation = expectation(description: "Update completed") + + store.withinReadWriteTransaction({ transaction in + try transaction.update(cacheMutation) { data in + data.hero.name = "Artoo" + } + }, completion: { result in + defer { updateCompletedExpectation.fulfill() } + /// Expected success, but result was an error: GraphQLExecutionError(path: hero.nickname, underlying: ApolloAPI.JSONDecodingError.missingValue) + XCTAssertSuccessResult(result) + }) + + self.wait(for: [updateCompletedExpectation], timeout: Self.defaultWaitTimeout) + } + + let query = MockQuery() + + loadFromStore(operation: query) { result in + try XCTAssertSuccessResult(result) { graphQLResult in + XCTAssertEqual(graphQLResult.source, .cache) + XCTAssertNil(graphQLResult.errors) + + let data = try XCTUnwrap(graphQLResult.data) + XCTAssertEqual(data.hero.name, "Artoo") + XCTAssertNil(data.hero.nickname) + } + } + } + func test_updateCacheMutation_givenMutationOperation_updateNestedField_updatesObjectAtMutationRoot() throws { // given