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

Document data defaults/initialization for 3.0 #320

Open
dcecile opened this issue Nov 14, 2019 · 7 comments
Open

Document data defaults/initialization for 3.0 #320

dcecile opened this issue Nov 14, 2019 · 7 comments

Comments

@dcecile
Copy link

dcecile commented Nov 14, 2019

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:

 withClientState({
      resolvers,
      cache,
      defaults: window.localStorage.length === 0 ? defaults : {},
      typeDefs
    })

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:

        let data = undefined
        try {
            data = client.readQuery({ query })
        } catch (error) {
            console.log("No client data yet")
            console.log(error)
        }

        if (!data) {
            client.writeQuery({ query, data: initialData })
        }
@wtrocki
Copy link
Collaborator

wtrocki commented Nov 14, 2019

@dcecile This is something I have overlooked.
I have quickly checked and in fact this is what needs to be done. Best way to do it is to:

  • Create some helper in cache persist for seeding data.
  • Add this code to README.md

Unless we will find better way I think docs will be the simplest to apply here

@dilame
Copy link

dilame commented Feb 19, 2020

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.
Could you please tell, what is the recommended way to initialize now?

@wtrocki
Copy link
Collaborator

wtrocki commented Feb 19, 2020

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

@hatchli
Copy link

hatchli commented Mar 4, 2020

Is there an update to this?
@dcecile Would you mind sharing more of your code to show how you implement this? I'm really having a hard time getting the initialization to work...

@dcecile
Copy link
Author

dcecile commented Mar 21, 2020

@hatchli Here's how we're initializing the cache (catching the readQuery error and later running writeQuery):

    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,
        })
    }

@wtrocki
Copy link
Collaborator

wtrocki commented Mar 21, 2020

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.

@dcecile
Copy link
Author

dcecile commented Mar 22, 2020

Sorry, I don't have public example for 3.x. Maybe someone else..?

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

No branches or pull requests

4 participants