-
Notifications
You must be signed in to change notification settings - Fork 39
/
Reindex.js
147 lines (130 loc) · 3.45 KB
/
Reindex.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { graphql, formatError } from 'graphql';
import { isUserError } from './UserError';
import Monitoring from '../Monitoring';
import Metrics from '../server/Metrics';
import { trackEvent } from '../server/IntercomClient';
import createSchema from './createSchema';
export default class Reindex {
constructor(contextFunction) {
if (!(contextFunction instanceof Function)) {
this._contextFunction = () => contextFunction;
} else {
this._contextFunction = contextFunction;
}
}
async getOptions({ db, credentials, extraRootFields = null }) {
const { typeRegistry, ...context } = await this._contextFunction({ db });
const schema = createSchema(typeRegistry, extraRootFields);
return {
schema,
context: {
db,
credentials,
typeRegistry,
...context,
},
};
}
async processRequest(request) {
const query = request.payload.query;
const variables = request.payload.variables || {};
const credentials = request.auth.credentials;
const db = await request.getDB();
const {
schema,
context,
} = await this.getOptions({ db, credentials });
const start = process.hrtime();
const result = await graphql(
schema,
query,
null,
context,
variables
);
const elapsed = process.hrtime(start);
let rootName = 'unknown';
let hasErrors = false;
if (result.data) {
rootName = Object.keys(result.data).sort().join(',');
if (rootName === 'viewer') {
rootName = Object.keys(result.data.viewer).sort().join(',');
}
}
if (result.errors) {
result.errors = result.errors.map((error) => {
if (error.originalError &&
!isUserError(error.originalError)) {
hasErrors = true;
Monitoring.noticeError(error.originalError, {
request,
extra: {
graphQL: {
query,
variables,
},
},
tags: {
type: 'graphQL',
},
});
return {
message: 'Internal Server Error',
};
} else {
return formatError(error);
}
});
}
setImmediate(() => {
if (result.data && context.credentials.isAdmin) {
trackEvent(context.credentials, 'executed-query', {
query,
rootName,
variables: JSON.stringify(variables),
});
}
Metrics.increment(
`reindex.graphQL.count`,
1,
request.info.hostname,
);
Metrics.measureHrTime(
`reindex.graphQL.totalTime`,
elapsed,
request.info.hostname
);
if (context.db.stats) {
Metrics.measure(
`reindex.graphQL.dbTime`,
`${context.db.stats.totalTime.toFixed(2)}ms`,
request.info.hostname,
);
Metrics.measure(
`reindex.graphQL.dbQueryCount`,
context.db.stats.count,
request.info.hostname,
);
}
if (hasErrors) {
Metrics.increment(
`reindex.graphQL.errorCount`,
1,
request.info.hostname
);
}
});
console.log(JSON.stringify({
hostname: request.info.hostname,
credentials: context.credentials,
query,
variables,
stats: {
elapsed,
db: context.db.stats || {},
},
errors: result.errors || [],
}));
return result;
}
}