-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (35 loc) · 1.18 KB
/
index.js
File metadata and controls
38 lines (35 loc) · 1.18 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
import { GraphQLClient } from 'graphql-request';
import { createRequest, createResponse } from 'node-mocks-http';
/**
* Drop-in replacement for GraphQLClient. Uses the Postgraphile middleware
* object directly without a network request.
*/
class PostgraphileClient extends GraphQLClient {
constructor(postgraphile) {
this.postgraphile = postgraphile;
super('');
}
async request(query, variables) {
return new Promise((resolve) => {
const req = createRequest();
const res = createResponse();
req._setBody({ query, variables: variables || undefined });
req._setMethod('POST');
req._setURL('/graphql');
res.end = ((result) => resolve(JSON.parse(result).data)); // TODO: error handling
res.end = (result) => {
const json = JSON.parse(result);
if (json.errors && json.errors.length > 0) {
// "errors" seems to be a GraphQLFormattedErrorExtended[]
// (from postgraphile) but with an extra "data" prop
reject(json.errors);
} else {
resolve(json.data);
}
};
req.isInternalQuery = true;
this.postgraphile(req, res);
});
}
}
export { PostgraphileClient };