· 6 years ago · Aug 03, 2019, 04:02 AM
1...
2exports.createSchemaCustomization = ({ actions }) => {
3 const { createTypes } = actions
4 const typeDefs = `
5 type RevealPost implements Node {
6 title: String,
7 ...
8 excerpt: String,
9 created_at: Int
10 }
11 `;
12 createTypes(typeDefs);
13};
14
15exports.sourceNodes = (
16 { actions, createContentDigest },
17 configOptions
18) => {
19 const { createNode } = actions;
20
21 const { api_host, contract_id } = configOptions;
22 delete configOptions.plugins;
23
24 const parseResponse = (response) => {
25 const { logs } = response;
26 const data = JSON.parse(logs[0]);
27 return data;
28 };
29
30 const processPost = post => {
31 const nodeContent = JSON.stringify(post);
32 const nodeData = Object.assign({}, post, {
33 parent: null,
34 children: [],
35 internal: {
36 type: `RevealPost`,
37 content: nodeContent,
38 contentDigest: createContentDigest(post),
39 },
40 })
41 return nodeData;
42 };
43
44 const client = new Wavelet(api_host);
45 const generatedKeys = nacl.sign.keyPair();
46 const secretKey = Buffer.from(generatedKeys.secretKey).toString("hex");
47 const wallet = Wavelet.loadWalletFromPrivateKey(secretKey);
48 const contract = new Contract(client, contract_id);
49
50 return contract.init().then(() => {
51 const response = contract.test(
52 wallet,
53 'get_posts',
54 BigInt(0),
55 {
56 type: 'uint32',
57 value: Math.floor(new Date() / 1000)
58 }
59 );
60
61 parseResponse(response).forEach(post => {
62 const nodeData = processPost(post);
63 createNode(nodeData);
64 });
65 });
66}