Comments (3)
Hi @reubn –
I took a quick look at your minimal test case, and I think that there's a bit of a mis-understanding of how the cache functions. Within your CountryDetailView
, you're attempting to create an object of type CountryDetailQuery.Data.Country
by fetching a CountryDetailQuery
via the cache. However, as you've never fetched CountryDetailQuery
before, you don't have a cache entry for that query.
As you said, you're really looking for the country
field – which is being supplied by a shared fragment CountryInfo
. You can, instead of storing CountryDetailQuery.Data.Country
, store CountryInfo
. And instead of fetching CountryDetailQuery
, you can do a read operation on the cache for a CountryInfo
.
Your fetch data function becomes something like this:
func fetchData() {
GraphQL.shared.apollo.store.withinReadTransaction { transaction in
let countryInfo = try transaction.readObject(ofType: CountryInfo.self, withKey: "Country:\(code)")
self.country = countryInfo
}
}
More important that having a fix (which may or may not work for your use-case) is understanding why what you're attempting to do fails.
Imagine, for a moment, that your CountryDataQuery
was much more complex:
query CountryDetailQuery($code: ID!) {
country(code: $code) {
...CountryInfo
population
officialLanguage
currentLeader
governmentType
# etc
}
}
At this point, there's a very clear divergence between the AllCountriesQuery.Country
and CountryDataQuery.Country
data types. Further, when we ask the cache to fetch a whole query, we must have executed that query before in the past. That's because of how queries are keyed.
For example, a CountryDetailQuery
with code AD
would be keyed as QUERY_ROOT.CountryDetailQuery(code: "AD")
, with selections you have keyed similarly (QUERY_ROOT.CountryDetailQuery(code: "AD").country(code: "AD")
. When you ask the cache to read
for a query, there is no entry in the cache that resolves to that query, as we've never executed it. At its core, returnCacheDataDontFetch
will resolve to code that's quite similar to the code I've written above for fetchData
but will instead tell the transaction to read
for the CountryDetailQuery
, not the CountryInfo
fragment.
from apollo-ios.
I'm closing this issue due to inactivity. If this is still unresolved, feel free to provide more information and we can re-open this, or create a new issue.
from apollo-ios.
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.
from apollo-ios.
Related Issues (20)
- How should I continue after handleErrorAsync inside a custom ApolloErrorInterceptor? HOT 5
- Defining a CustomScalarType which is [String:Any] HOT 12
- Deprecate `legacyResponse` and prepare for partial/incremental caching HOT 2
- Custom scalar isn't being respected HOT 7
- @defer issue HOT 8
- Documentation for @import directive. HOT 9
- Add debugDescription api for Apollo generated types
- Circular reference Build Error HOT 6
- Invalid redeclaration of synthesized property '_id' HOT 7
- Split/Hybrid Caching between InMemory and SQL review and thoughts HOT 2
- ID as a custom scalar HOT 6
- Bug: Cache writes of incremental responses uses incorrect parent key (custom cache keys)
- Incorrect PR link in release notes for version 1.11.0 HOT 2
- apollo-ios-cli deprecated --schema-name HOT 2
- Confused about cache behavior HOT 2
- CLI fails to run with 1.12.0 HOT 4
- Response value getting nil HOT 2
- Version mismatch between package and CLI HOT 3
- Pagination: Pager does not provide updates during a refresh HOT 5
- Slow compilation speed when migrating from 0.x to 1.12.2 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apollo-ios.