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

Make fetch(cachePolicy:) public in GraphQLQueryWatcher so ApolloClientProtocol.watch can be mocked. #559

Closed
wants to merge 1 commit into from

Conversation

JimRoepcke
Copy link

I am implementing a MockApolloClient. It implements ApolloClientProtocol. I got to the point of implementing watch(query:cachePolicy:context:callbackQueue:resultHandler:).

Looking at ApolloClient's implementation, it's very simple:

    let watcher = GraphQLQueryWatcher(client: self,
                                      query: query,
                                      context: context,
                                      callbackQueue: callbackQueue,
                                      resultHandler: resultHandler)
    watcher.fetch(cachePolicy: cachePolicy)
    return watcher

It is basically implemented by passing self to GraphQLQueryWatcher, which interacts with that ApolloClientProtocol instance.

As far as I can see, the best way to implement watch in my MockApolloClient is by having basically the exact same code. I would pass in self (being the MockApolloClient: ApolloClientProtocol) into the GraphQLQueryWatcher initializer, call fetch(cachePolicy:) on it, and then return it.

However, fetch(cachePolicy:) is not public on GraphQLQueryWatcher, it's internal, so I can't do that.

I suspect this is a simple oversight.

I don't think it makes sense to re-implement GraphQLQueryWatcher, it'd be better to have the same logic powering watching in the MockApolloClient because it can operate in terms of the mock implementation of ApolloClientProtocol.fetch and ApolloClientProtocol.store. And frankly, it'd be impossible to do anything else because the ApolloClientProtocol.watch method is declared as returning GraphQLQueryWatcher, and GraphQLQueryWatcher itself is public, not open (just like ApolloClient itself), so the only thing I can possibly do is use GraphQLQueryWatcher, and that means needing to use its implementation as-is.

As such, this PR makes fetch(cachePolicy:) public. That's all it does.

Thank you for your consideration.

Copy link

netlify bot commented Dec 19, 2024

👷 Deploy request for eclectic-pie-88a2ba pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit cc12430

Copy link

netlify bot commented Dec 19, 2024

👷 Deploy request for apollo-ios-docc pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit cc12430

@svc-apollo-docs
Copy link
Collaborator

svc-apollo-docs commented Dec 19, 2024

✅ Docs Preview Ready

No new or changed pages found.

@JimRoepcke
Copy link
Author

I did not notice that there's a public refetch method that only calls fetch. I'll use that instead!

@JimRoepcke JimRoepcke closed this Dec 19, 2024
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.

2 participants