I'me learning the local state management capabilities of Apollo Client 2.5 and finding it rather confusing. For what it's worth, I was able to understand Apollo Server pretty readily, but this is proving more daunting.
I (kind of) understand how to use it when one wants to set up local resolvers and queries to read local state, but is there a way to simply set some local state variables, as one can pretty easily with Redux. For example, setting isLoggedIn
as true
on the completion of a login mutation? I tried this (following https://www.apollographql.com/docs/react/essentials/local-state)
return <Mutation mutation={LOGIN} onCompleted={data => {
if (data && data.login) {
const {login: {token}} = data
if (token) {
history.push('/list')
}
}
}}>
{(register, {data, error, loading, client}) => {
if (error) {
return <div>{error.toString()}</div>
}
if (loading) return <div>Loading...</div>
if (data && data.login) {
const {login: {token}} = data
if (token) {
localStorage.setItem('token', token)
client.writeData({isLoggedIn:true})
}
but got the error
Error: Error writing result to store for query: {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GeneratedClientQuery"},"selectionSet":null}]} Cannot read property 'selections' of null
But I don't know if I'm using writeData
correctly. More importantly, how to I read the data? There doesn't seem to be a corresponding readData
call in the API. Do I need to set up a GraphQL query and run that to see if the user is logged in? Do I need to then make a resolver for that query and include it when constructing the Apollo Client?
I saw some examples using apollo-link-state
that seemed to be simpler, but apparently that's deprecated and included in Apollo Client 2.5.
Clearly I am not seeing the forest for the trees, and would welcome any advice.