-
Notifications
You must be signed in to change notification settings - Fork 118
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
Document data defaults/initialization for 3.0 #320
Comments
@dcecile This is something I have overlooked.
Unless we will find better way I think docs will be the simplest to apply here |
It's very useful feature. To be honest i was expecting and sure that this feature exists, because it's just impossible to manage local state without initial data. |
We can provide helper in cache persist that will use read and write method to seed data. Hoping to get better answer from Apollo on this issue |
Is there an update to this? |
@hatchli Here's how we're initializing the cache (catching the const query = gql`
query {
// <INSERT QUERY HERE>
}
`
let cacheData = undefined
try {
cacheData = client.readQuery({ query })
} catch (error) {
console.log("No client data yet")
console.log(error)
cacheData = {}
}
let changed = false
// <INSERT INITIALIZATION CODE HERE>
if (changed) {
client.writeQuery({
query,
data: cacheData,
})
} |
Hi Do you have any public example using 3.x. All my apps still use Apollo 2.x but I would love to experiment with 3.x and see how much work is needed to support it. |
Sorry, I don't have public example for 3.x. Maybe someone else..? |
When using a local client cache, it's helpful to have a default state that only gets initialized the first time the cache is created, and not when the cache is restored from persist storage.
With 3.0, this doesn't seem simple anymore.
#85 has an old example of setting up defaults:
And this workaround:
But both of those code snippets are invalid in 3.0.
In #85, there's a link to Apollo Boost code, but that code has now been removed together with all of Apollo Boost.
See apollographql/apollo-client@2141cda for the explanation that neither defaults nor intializers are necessary in 3.0 because
writeData
is available.I'd like a way to initialize data only once, in a way that works with
apollo-cache-persist
. If such a mechanism already exists, then maybe it just needs documentation.After initializing the client, I can do a query to see if data exists in the local cache. This results in an error, as explained in apollographql/apollo-feature-requests#1. But swallowing the error is an OK workaround for me:
The text was updated successfully, but these errors were encountered: