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

Weird caching behavior #59

Closed
attheodo opened this issue Feb 10, 2017 · 12 comments
Closed

Weird caching behavior #59

attheodo opened this issue Feb 10, 2017 · 12 comments

Comments

@attheodo
Copy link

Hey @martijnwalraven,

we've encountered a weird caching behavior around this query:

query GetLinkedIdentities {
    me {
        id
        linked_identities {
            records {
                id
                name
                relationship
                current_photo {
                    ...PhotoDetails
                }
            }
        }
    }
}

When we run our app, this query is executed first thing before anything else.

  • If link_identities are 0 when the app starts, even if we issue a mutation to add a new linked_identity and re-run the GetLinkedIdentities query, it still returns 0 results.
  • If linked_identities are > 0 when the app starts, when we issue a mutation to add a new linked_identity and re-reun the GetLinkedIdentities query, it fetches back the proper number of results.

Running the query with cachePolicy: .fetchIgnoringCacheData seems to solve the problem.

Does this look normal in terms of how Apollo client is handling client-side caching or could this be a bug?

@martijnwalraven
Copy link
Contributor

martijnwalraven commented Feb 11, 2017

What do the mutation results look like? Did you define cacheKeyForObject?

(Away until Monday, so may not respond until then.)

@martijnwalraven
Copy link
Contributor

@attheodo: Any new insights into this behavior?

@attheodo
Copy link
Author

Hey @martijnwalraven the mutation for creating the linked_identities looks like this:

mutation CreateLinkedIdentity($accountId: RecordID!, $relationship: IdentityRelationType!, $name: String!) {
    identityCreate(input: {account_id: $accountId, relationship: $relationship, name: $name}) {
        mutationId
        account {
            id
        }
        identity {
            id
        }
    }
}

No, we're not defining cacheForKeyObject in the query.

@martijnwalraven
Copy link
Contributor

If you haven't defined cacheKeyForObject, mutation results can't be associated with the query results, so at a minimum you would have to add something like apollo.cacheKeyForObject = { $0["id"] }.

That won't help here however, because even if the new identity is added to the cache under the right key, that won't update the list of linked_identities. Currently, the only way to update that list is to refresh it from the server, as you've done with cachePolicy: .fetchIgnoringCacheData (if you're using a query watcher, you can also call refresh on it).

@martijnwalraven
Copy link
Contributor

If linked_identities are > 0 when the app starts, when we issue a mutation to add a new linked_identity and re-reun the GetLinkedIdentities query, it fetches back the proper number of results.

This is puzzling to me, I would expect the query to always return the cached results, so not including the newly added one. Could you confirm this is the behavior you're seeing?

@attheodo
Copy link
Author

@martijnwalraven yes indeed, this is the behaviour we're seeing.

@martijnwalraven
Copy link
Contributor

When are identities added? Could this be a timing issue, where the difference is explained by query results either having been written to the cache or still being in flight?

Also, is account in the mutation the same object as me in the query?

@attheodo
Copy link
Author

@martijnwalraven the identities are added as a result of user actions. When the mutation to add them is finished successfully, the collection view re-triggers the query to get them and reloads itself. I don't really think this is a timing issue.

I might need to confirm with the platform team but yes I believe account is an alias for me

@martijnwalraven
Copy link
Contributor

martijnwalraven commented Feb 15, 2017

When the mutation to add them is finished successfully, the collection view re-triggers the query to get them and reloads itself.

Just checking, but does the collection view wait for the result to be returned before reloading itself? If you perform the reload in the result handler, that should be ok. Are you using a query watcher or just a fetch?

@attheodo
Copy link
Author

@martijnwalraven yes, we're obviously waiting for the new results to come by. No we're not using a query watcher yet, just a simple fetch.

@martijnwalraven
Copy link
Contributor

Could you try refetching (so not the original fetch) with .returnCacheDataDontFetch for both the case where linked_identities = 0 and the case where linked_identities > 0? I suspect you will get an error when linked_identities > 0, so hopefully that will help figure out what is going on.

@martijnwalraven
Copy link
Contributor

Going to close this for lack of response, but feel free to reopen it if it is still an issue.

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

2 participants