-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathquery-resolvers.js
More file actions
119 lines (111 loc) · 3.47 KB
/
query-resolvers.js
File metadata and controls
119 lines (111 loc) · 3.47 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const pjs = require('./package.json');
const debug = require('debug')('hubspot-gql');
const assertHasCredentials = ctx => {
if (!ctx.hs) {
throw new Error('Credentials are required');
}
};
const flattenProps = properties => Object.keys(properties).reduce((acc, curr) => {
acc[curr] = properties[curr].value;
return acc;
}, {});
const contactsResponse = contact => {
const {vid, properties} = contact;
return Object.assign({
vid
}, flattenProps(properties));
};
const companiesResponse = company => {
const {portalId, properties, additionalDomains} = company;
return Object.assign({
portalId,
properties
}, flattenProps(properties));
};
module.exports = {
version: (_, opts, context) => {
return pjs.version;
},
contacts: async (_, opts, context) => {
assertHasCredentials(context);
const {hs} = context;
// Define extra properties as required by the schema
const property = ['email', 'firstname', 'lastname', 'company'];
Object.assign(opts, {property});
const response = await hs.contacts.getContacts(opts);
const {contacts} = response;
return contacts.map(contactsResponse);
},
contact: async (_, opts, context) => {
assertHasCredentials(context);
const {hs} = context;
const {id, email, utk} = opts;
let response;
if (id) {
response = await hs.contacts.getById(id);
} else if (email) {
response = await hs.contacts.getByEmail(email);
} else if (utk) {
response = await hs.contacts.getByUtk(utk);
} else {
throw new Error('You must specify one of `id`, `email`, `utk` in your query');
}
const {vid, properties} = response;
return contactsResponse(response);
},
blogAuthor: async (_, opts, context) => {
assertHasCredentials(context);
const {hs} = context;
const response = await hs.blog.getAuthor(opts.id);
return response;
},
blogAuthors: async (_, opts, context) => {
assertHasCredentials(context);
const {hs} = context;
const response = await hs.blog.getAuthors(opts);
const {objects} = response;
return objects;
},
page: async (_, opts, context, {cacheControl}) => {
assertHasCredentials(context);
const {hs} = context;
const response = await hs.pages.getPageById(opts.id);
return response;
},
pages: async (_, opts, context, {cacheControl}) => {
assertHasCredentials(context);
const {hs} = context;
const response = await hs.pages.getPages(opts);
const {objects} = response;
return objects;
},
blogPost: async (_, opts, context) => {
assertHasCredentials(context);
const {hs} = context;
const response = await hs.blog.getPostById(opts);
return response;
},
blogPosts: async (_, opts, context) => {
const {contentGroupId: content_group_id, blogAuthorId: blog_author_id, limit} = opts;
assertHasCredentials(context);
const {hs} = context;
const response = await hs.blog.getPosts({content_group_id, blog_author_id, limit});
const {objects} = response;
return objects;
},
workflows: async (_, opts, context) => {
assertHasCredentials(context);
const {hs} = context;
const response = await hs.workflows.getAll();
const {workflows} = response;
// Filtering (as this is not provided by the API)
return workflows;
},
workflow: async (_, opts, context) => {
assertHasCredentials(context);
const {hs} = context;
const response = await hs.workflows.getWorkflow(opts.id);
debug(response);
return response;
}
};