-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
87 lines (78 loc) · 1.8 KB
/
api.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
79
80
81
82
83
84
85
86
87
const { config } = require('./config');
const { ApolloServer, gql } = require('apollo-server-lambda');
const { unmarshall } = require('@aws-sdk/util-dynamodb');
const { DynamoDBClient, ScanCommand } = require('@aws-sdk/client-dynamodb');
const client = new DynamoDBClient({ region: config.region });
const typeDefs = gql`
scalar JSON
type Bookmark {
id: String!
status: String
subject: String
recieved: String
toName: String
toAddress: String
fromName: String
fromAddress: String
bookmark: String
url: String
title: String
description: String
screenshot: String
}
type Query {
bookmarks: [Bookmark]
unverifiedBookmarks: [Bookmark]
}
`;
const getBookmarks = async (status = 'verified') => {
const params = {
TableName: config.tableName,
FilterExpression: '#status = :status',
ExpressionAttributeNames: {
'#status': 'status',
},
ExpressionAttributeValues: { ':status': { S: status } },
};
try {
const results = await client.send(new ScanCommand(params));
const bookmarks = [];
results.Items.forEach((item) => {
const newRecord = unmarshall(item);
bookmarks.push(newRecord);
});
return bookmarks;
} catch (err) {
console.error(err);
return err;
}
};
const resolvers = {
Query: {
bookmarks: () => {
return getBookmarks();
},
unverifiedBookmarks: () => {
return getBookmarks('unverified');
},
},
Mutation: {
update: async (_, data, { dataSources }) => {
// TODO
},
submit: async (_, data, { dataSources }) => {
// TODO
},
},
};
const gqlServer = new ApolloServer({
typeDefs,
resolvers,
introspection: false,
});
module.exports.graphql = gqlServer.createHandler({
cors: {
origin: true,
credentials: true,
},
});