-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapollo.js
78 lines (71 loc) · 2.12 KB
/
apollo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { ApolloClient, makeVar, InMemoryCache } from "@apollo/client";
import { createUploadLink } from "apollo-upload-client";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { setContext } from "@apollo/client/link/context";
import { offsetLimitPagination } from "@apollo/client/utilities";
export const isLoggedInVar = makeVar(false);
export const tokenVar = makeVar("");
export const memberVar = makeVar();
const TOKEN = "token";
const MEMBER = "member";
const KEY_VALUE = "keyFirstLaunch";
// 키값에 true로 저장한다.
function setAppLaunched() {
AsyncStorage.setItem(KEY_VALUE, "true");
}
export const logUserIn = async (token, member) => {
await AsyncStorage.setItem(TOKEN, token);
await AsyncStorage.setItem(MEMBER, JSON.stringify(member));
isLoggedInVar(true);
tokenVar(token);
memberVar(JSON.stringify(member));
};
export const logUserOut = async () => {
await AsyncStorage.removeItem(TOKEN);
await AsyncStorage.removeItem(MEMBER);
isLoggedInVar(false);
tokenVar(null);
memberVar(null);
};
export const checkFirstLaunch = async () => {
try {
const isFirstLaunched = await AsyncStorage.getItem(KEY_VALUE); //우선 값을 읽자.
if (isFirstLaunched === null) {
// 값이 없다면,
setAppLaunched(); // 키값에 true로 저장하고,
return true; // true를 반환 ==> 최초 실행 !!
}
return false; // 값이 없다면, 최초 실행 아님 !!
} catch (error) {
// 에러 발생 시에도 false 로 반환
console.log(" [chk first launch] :" + error);
return false; // Error
}
};
const httpLink = createUploadLink({
uri: "http://api.care-korea.kr/graphql",
});
const authLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
token: tokenVar(),
member: memberVar(),
},
};
});
export const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
pointUsed: offsetLimitPagination(),
citizenpaticipate: offsetLimitPagination(),
},
},
},
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache,
});
export default client;