apollo로 전역 로컬파일 스토레이지 사용하는 방법
[apollo.ts]
export const client = new ApolloClient({
link: splitLink,
cache: new InMemoryCache({
addTypename: false,
typePolicies: {
Query: {
fields: {
isLoggedIn: {
read() {
return isLoggedInVar();
}
},
token: {
read() {
return authTokenVar();
}
},
}
}
}
})
});
[apollo.ts]
export const isLoggedInVar = makeVar(false) // 전역 사용
[app.tsx]
const isLoggedIn = useReactiveVar(isLoggedInVar)
console.log('isLoggedInVar', isLoggedIn)
[signIn.tsx]
const onCompleted = (data) => {
const { login: {ok, error, token}} = data;
if(ok == true && token){
authTokenVar(token);
localStorage.setItem("LOCALSTROAGE_TOKEN", token);
isLoggedInVar(true);
navigate('/');
}
}
const signOut = () => {
localStorage.removeItem("LOCALSTROAGE_TOKEN");
isLoggedInVar(false)
}
[main.tsx]
<ApolloProvider client={client}>
<Router>
<App />
</Router>
</ApolloProvider>