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

Parity with Apollo Kotlin: Ability to Observe ApolloStore Changes? #3350

Closed
jamesonwilliams opened this issue Mar 12, 2024 · 3 comments
Closed
Assignees
Labels
feature New addition or enhancement to existing solutions planned-next Slated to be included in the next release

Comments

@jamesonwilliams
Copy link
Contributor

Use case

Hey, Apollo team!

I recently joined a startup that has built some custom SwiftUI tooling on top of Apollo iOS. Specifically, we've built a @Sync property wrapper that (under the hood) watches the Apollo Store for cache changes, and then updates the view via binding. To support this, the team forked Apollo so they could observe changes in the ApolloStore via an ApolloStoreSubscriber instance in our app. I'm looking at options that will get us back onto Apollo's mainline. 😃

There have been similar discussions around making ApolloStoreSubscriber a public API in 2019 (ref: #630) and 2022 (ref: #2300). Importantly, some of the risks brought up in 2019 have been assuaged by general refactoring on the ApolloStore APIs (no longer are there UnsafeMutableRawPointer).

As a reference, Apollo Kotlin does currently expose this functionality via a public property, changedKeys.

Aside from observing the store directly, we've also considered using ApolloClient.watch(query:). But, it wouldn't allow us to update the UI when individual fragments change.

Anyway, appreciate any feedback/thoughts you have! Thank you - Jameson

Describe the solution you'd like

As an uber-collapsed TL;DR, I'm essentially inquiring about adding public to these four lines: 1, 2, 3, 4.

@jamesonwilliams jamesonwilliams added the feature New addition or enhancement to existing solutions label Mar 12, 2024
@AnthonyMDev AnthonyMDev added the planned-next Slated to be included in the next release label Mar 13, 2024
@AnthonyMDev AnthonyMDev added this to the Minor Releases (1.x) milestone Mar 13, 2024
@AnthonyMDev AnthonyMDev self-assigned this Mar 13, 2024
@AnthonyMDev
Copy link
Contributor

Thanks for the request and all the details. It really helped me to have the references to the past discussions!

I'm very open to doing this at this time. The discussion about subscribers being strongly retained in #2300 just means that we need some documentation around this that indicates that a subscriber needs to be unsubscribed from the store before you let it go out of scope or it will be retained. There are plenty of easy ways to ensure that you're doing that, and this is more of an advanced API, so I'm okay with just requiring users to be responsible for that.

In the future, I'd love to improve on this API and make it harder to create retain cycles there, but for now, I think it's fine to just document it.

I'd be happy to get this out for our next release. But if you'd like to make the contribution, feel free to open up a PR and make this API public!

@jamesonwilliams
Copy link
Contributor Author

Thanks so much for the fast response, @AnthonyMDev - and glad to hear y'all are open to opening up this functionality! I agree some strong docs can help avoid foot-guns re: retain cycles, here. I'll aim to get a PR up later today; may need your help to iterate on the best copy for that comment, once the PR is up.

jamesonwilliams added a commit to jamesonwilliams/apollo-ios-dev that referenced this issue Mar 15, 2024
Apollo Kotlin currently provides a public API to get notified when
objects in the ApolloStore are updated. iOS has a similar facility as
well, but it has not been a public API. This change exposes the
ApolloStoreSubscriber for public use.

Refer: apollographql/apollo-ios#3350
jamesonwilliams added a commit to jamesonwilliams/apollo-ios-dev that referenced this issue Mar 22, 2024
Apollo Kotlin currently provides a public API to get notified when
objects in the ApolloStore are updated. iOS has a similar facility as
well, but it has not been a public API. This change exposes the
ApolloStoreSubscriber for public use.

Refer: apollographql/apollo-ios#3350
AnthonyMDev pushed a commit to apollographql/apollo-ios-dev that referenced this issue Mar 22, 2024
(Full context in apollographql/apollo-ios#3350.)

Apollo Kotlin currently provides a public [`changedKeys`](https://github.com/apollographql/apollo-kotlin/blob/v4.0.0-beta.5/libraries/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/ApolloStore.kt#L27-L30) property to get notified when objects in the ApolloStore change. Internally, iOS has had a similar facility, too -- but it hasn't been `public` until now.
Copy link

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New addition or enhancement to existing solutions planned-next Slated to be included in the next release
Projects
None yet
Development

No branches or pull requests

2 participants