Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependent keys after a successful load #1375

Merged
merged 4 commits into from
Sep 1, 2020

Conversation

designatednerd
Copy link
Contributor

Addresses #1365 - @teodorpenkov, can you confirm this fixes your issue? I'm still futzing around on how to test this.

@teodorpenkov
Copy link

yes, this fixes the issue, I will think about the test today.

Thank you

@teodorpenkov
Copy link

teodorpenkov commented Sep 1, 2020

@designatednerd here is a test that verifies it's working. I wouldn't add it as it is, because it's messy (In order to make this work I've created a new query with the same structure as HeroAndFriendsNamesWithIDsQuery and I made the body of the mock network transport mutable for convenience.), maybe if we enhance the Star Wars API with mutations about characters it will allow us to build a better test.

If I have the time in the next week or so I will try to come up with something better because the issue we are fixing is a bit hidden and it's very easy to miss.

withCache(initialRecords: [:]) { (cache) in
      let networkTransport = MockNetworkTransport(body: [
        "data": [
          "hero": [
            "id": "0",
            "name": "Artoo",
            "__typename": "Droid",
            "friends": [
              ["id": "10", "__typename": "Human", "name": "Luke Skywalker"]
            ]
          ]
        ]
      ])

      let store = ApolloStore(cache: cache)
      let client = ApolloClient(networkTransport: networkTransport, store: store)
      client.store.cacheKeyForObject = { $0["id"] }

      let query = HeroAndFriendsNamesWithIDsQuery()
      let expectation = self.expectation(description: "Has friend named Han Solo")
      let initialFetch = self.expectation(description: "Initial fetch")
      initialFetch.assertForOverFulfill = false

      _ = client.watch(query: query) { result in
        initialFetch.fulfill()
        if case let Result.success(result) = result {
          if result.data.flatMap({ $0.hero?.friends?.contains(where: { $0?.name == "Han Solo" })}) == true {
            expectation.fulfill()
          }
        }
      }
      wait(for: [initialFetch], timeout: 1)

      let encahnceInitialQuery = self.expectation(description: "Update initial query")
      store.withinReadWriteTransaction({ transaction in
        try transaction.update(query: query) { (data: inout HeroAndFriendsNamesWithIDsQuery.Data) in
          data.hero?.friends?.append(try .init(jsonObject: ["id": "11", "__typename": "Human", "name": "Not Han Solo"]))
          encahnceInitialQuery.fulfill()
        }
      })
      wait(for: [encahnceInitialQuery], timeout: 1)

      networkTransport.body = [
        "data": [
          "hero": [
            "id": "2",
            "name": "R2-D2",
            "__typename": "Droid",
            "friends": [
              ["id": "11", "__typename": "Human", "name": "Han Solo"]
            ]
          ]
        ]
      ]

      client.fetch(query: HeroAndFriendsNamesWithIdQuery(episode: .newhope), cachePolicy: .fetchIgnoringCacheData)

      waitForExpectations(timeout: 5, handler: nil)
    }

@designatednerd
Copy link
Contributor Author

@teodorpenkov Thanks for your help with the idea - I've taken the test and fleshed it out a bit, would love your feedback, but it passes when resetting the dependent keys is there and fails when it's commented out, so I think it should be an effective safety net.

@designatednerd designatednerd added this to the Next Release milestone Sep 1, 2020
@designatednerd designatednerd linked an issue Sep 1, 2020 that may be closed by this pull request
Copy link

@teodorpenkov teodorpenkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@designatednerd designatednerd merged commit e80ec55 into main Sep 1, 2020
@designatednerd designatednerd deleted the fix/dependent-keys branch September 1, 2020 20:50
@designatednerd
Copy link
Contributor Author

@teodorpenkov thanks for the inspo on the test, it already caught a massive bug with query watchers on the networking rewrite branch 🙃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GraphQLQueryWatcher doesn't update its dependent keys
2 participants