Created
June 7, 2019 14:11
-
-
Save grgurev/4db069e3f8b0bf390f3aa8bf41993fd3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const defaults = { | |
state: { | |
token: localStorage.getItem('token') || null, | |
refreshToken: localStorage.getItem('refresh-token') || null, | |
userId: localStorage.getItem('user-id') || null, | |
isAuthenticated: !!localStorage.getItem('token'), | |
__typename: 'State' | |
} | |
} | |
const resolvers = { | |
Mutation: { | |
updateAuthStatus: (_, { isAuthenticated, refreshToken, token, userId }, { cache }) => { | |
const state = { | |
isAuthenticated, | |
refreshToken, | |
token, | |
userId, | |
__typename: 'State' | |
} | |
cache.writeData({ data: { state } }) | |
return state | |
} | |
} | |
} | |
const defaultOptions = { | |
httpEndpoint, | |
tokenName: AUTH_TOKEN, | |
persisting: false, | |
websocketsOnly: false, | |
ssr: false, | |
httpLinkOptions: { | |
uri: httpEndpoint, | |
fetch: customFetch | |
}, | |
getAuth: (tokenName) => { | |
const token = localStorage.getItem(AUTH_TOKEN) | |
if (token) { | |
return 'Bearer ' + token | |
} else { | |
return '' | |
} | |
}, | |
clientState: { | |
defaults, | |
resolvers | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment