-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.ts
61 lines (53 loc) · 1.52 KB
/
example.ts
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
import ApolloClient, { gql } from 'apollo-boost';
import fetch from 'cross-fetch';
const client = new ApolloClient({
uri: 'http://localhost:4000/',
fetch: fetch
});
// Gets the authentication token for a user
const getUserToken = async (id: number): Promise<string> => {
const authQuery = gql`
query {
authenticate(userId:"${id}") {
jwt
}
}
`;
try {
const result = await client.query({ query: authQuery });
const token: string = result.data.authenticate.jwt;
return token;
} catch {
return "";
}
};
// Gets the vault content as a user
const getVaultAsUser = async (id: number): Promise<string> => {
// Get the token for user and use it as Authorization token for vault query
const token = await getUserToken(id);
const vaultQuery = gql`
query {
vault {
treasures
}
}
`;
try {
// Vault query, use token
const result = await client.query({
query: vaultQuery,
context: { headers: { Authorization: token } },
fetchPolicy: "no-cache",
});
return result.data.vault.treasures;
} catch (e) {
return e.message;
}
};
// Hero 1 should get access denied when trying to fetch treasures, hero 2 should get the treasures
(async () => {
const treasure1 = await getVaultAsUser(1);
const treasure2 = await getVaultAsUser(2);
console.log(`Hero 1 fetching treasure: ${treasure1}`);
console.log(`Hero 2 fetching treasure: ${treasure2}`);
})();