Skip to content

Update dependent keys after a successful load#1375

Merged
designatednerd merged 4 commits intomainfrom
fix/dependent-keys
Sep 1, 2020
Merged

Update dependent keys after a successful load#1375
designatednerd merged 4 commits intomainfrom
fix/dependent-keys

Conversation

@designatednerd
Copy link
Copy Markdown
Contributor

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

@teodorpenkov
Copy link
Copy Markdown

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

Thank you

@teodorpenkov
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown

@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
Copy Markdown
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