Skip to content

Custom SQLiteDatabase Implementations #664

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

Conversation

ChrisLaganiere
Copy link
Contributor

@ChrisLaganiere ChrisLaganiere commented Jun 4, 2025

Add a public initializer to DatabaseRow to enable custom SQLiteDatabase implementations

Background

The SQLiteNormalizedCache is nicely structured with protocols so that consumers of the library can implement their own components under the hood if they choose to. There are a couple reasons one might want to do this:

  • to use a different SQLite implementation (mentioned as a goal in a comment on the original dependency inversion PR)
  • to add custom functionality under the hood, like a TTL policy for rows in the cache (I am trying to follow this path, motivating this PR)

The Problem

All of that is possible! ...almost

The use of SQLiteDatabase protocol makes it easy to implement and inject your own implementation, with one blocking exception. The missing piece is a public initializer for DatabaseRow, a type returned by one of the methods:

func selectRawRows(forKeys keys: Set<CacheKey>) throws -> [DatabaseRow]

If I as a consumer try to implement the public SQLiteDatabase protocol, I am blocked by this method, because I cannot instantiate DatabaseRow values. The default, automatic memberwise initializer for any Swift struct is internal, meaning that consumers of a library cannot use it, and get an error if trying to instantiate:

Screenshot 2025-06-03 at 6 56 02 PM

The change here, to add an explicit public init to this struct type, is all that is needed to enable custom SQLite implementations, as the original PR from 2021 seems to have intended. This change would allow me to implement the custom TTL policy on my own Apollo cache

Testing

There is no imperative logic change here to test. The best way to unit test this would be to add a full custom SQLiteDatabase implementation to the test suite, which I am happy to do, but seemed like a larger change. Risk here is low, so I skipped doing that for now

Alternatives

I could implement an entire NormalizedCache, possibly copying lots of code already in the repo. That is possible, but it would be much easier to allow flexibility with the utilities already provided by the sdk, by making this small change

Add public initializer that would enable a custom SQLiteDatabase implementation
Copy link

netlify bot commented Jun 4, 2025

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

Visit the deploys page to approve it

Name Link
🔨 Latest commit 2e8c0ce

@svc-apollo-docs
Copy link
Collaborator

svc-apollo-docs commented Jun 4, 2025

✅ Docs preview has no changes

The preview was not built because there were no changes.

Build ID: e1dc8528829c91c4fbc56a0b

@ChrisLaganiere
Copy link
Contributor Author

by the way, super excited for #615!

Copy link
Contributor

@AnthonyMDev AnthonyMDev left a comment

Choose a reason for hiding this comment

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

Thank you for the detailed write-up on the use case here. I'm open to accepting this PR.

@calvincestari calvincestari merged commit 9e7bada into apollographql:main Jun 4, 2025
24 checks passed
BobaFetters pushed a commit that referenced this pull request Jun 4, 2025
BobaFetters pushed a commit to apollographql/apollo-ios that referenced this pull request Jun 4, 2025
BobaFetters pushed a commit that referenced this pull request Jun 4, 2025
b0007f91 Custom SQLiteDatabase Implementations (#664)

git-subtree-dir: apollo-ios
git-subtree-split: b0007f917ae773fdaf4fe8222a324230eb2affe5
BobaFetters pushed a commit that referenced this pull request Jun 4, 2025
git-subtree-dir: apollo-ios
git-subtree-mainline: 945b418
git-subtree-split: b0007f917ae773fdaf4fe8222a324230eb2affe5
@ChrisLaganiere
Copy link
Contributor Author

Thanks for your help!

@apollographql apollographql deleted a comment from donpablo-0 Jul 8, 2025
@apollographql apollographql deleted a comment from donpablo-0 Jul 8, 2025
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.

5 participants