This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraphql.js
110 lines (95 loc) · 2.25 KB
/
graphql.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const logger = require('winston')
/**
* Converts the fields into a flat list separated by spaces for GraphQL.
* @param {string[]|Object[]} fields Array of fields to be flattened
* @returns {string} GraphQL acceptable list
*/
function toString (fields) {
if (fields === undefined || fields.length === 0) {
logger.warn('No GraphQL fields requested; only id will be returned')
fields = ['id'] // add a default so the query succeeds
}
return fields.reduce((accumulator, currentValue) => {
switch (typeof currentValue) {
// if the field is a string, use as-is
case 'string':
return accumulator + ' ' + currentValue
case 'object':
return accumulator + ` ${currentValue['name']} { ${toString(currentValue['fields'])}}`
}
})
}
/**
* Checks if the id field is present since this is required for most functions.
* @param {string[]} fields Fields to check
*/
function hasId (fields) {
fields.forEach(field => {
if(field === 'id') {
return true
}
})
return false
}
/**
* Ensures the id field is always available.
* @param {string} fields Fields to check; adds id if not
*/
function guardId (fields) {
if(!hasId(fields)) {
fields.push('id')
}
}
exports.addMember = `mutation addMembers ($input: UpdateSpaceInput!) {
updateSpace(input: $input) {
memberIdsChanged
}
}
`
exports.addMessageFocus = `mutation AddMessageFocus($input: AddFocusInput!) {
addMessageFocus(input: $input) {
message {
id
annotations
}
}
}
}`
exports.createSynchronousMessage = `mutation createMessage($input: CreateMessageInput!) {
createMessage(input: $input) {
message {
id
}
}
}
`
exports.createTargetedMessage = `mutation CreateTargetedMessage($input: CreateTargetedMessageInput!) {
createTargetedMessage(input: $input) {
successful
}
}
`
exports.getMe = (fields) => {
guardId(fields)
return `query GetMe {
me {
${toString(fields)}
}
}`
}
exports.getMessage = (fields) => {
guardId(fields)
return `query GetMessage($id: ID!) {
message(id: $id) {
${toString(fields)}
}
}`
}
exports.getSpace = (fields) => {
guardId(fields)
return `query GetSpace($id: ID!) {
space(id: $id) {
${toString(fields)}
}
}`
}