forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParseGraphQLServer.js
115 lines (107 loc) · 3.44 KB
/
ParseGraphQLServer.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
import corsMiddleware from 'cors';
import bodyParser from 'body-parser';
import { graphqlUploadExpress } from 'graphql-upload';
import { graphqlExpress } from 'apollo-server-express/dist/expressApollo';
import { renderPlaygroundPage } from '@apollographql/graphql-playground-html';
import { execute, subscribe } from 'graphql';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { handleParseErrors, handleParseHeaders } from '../middlewares';
import requiredParameter from '../requiredParameter';
import defaultLogger from '../logger';
import { ParseGraphQLSchema } from './ParseGraphQLSchema';
class ParseGraphQLServer {
constructor(parseServer, config) {
this.parseServer =
parseServer ||
requiredParameter('You must provide a parseServer instance!');
if (!config || !config.graphQLPath) {
requiredParameter('You must provide a config.graphQLPath!');
}
this.config = config;
this.parseGraphQLSchema = new ParseGraphQLSchema(
this.parseServer.config.databaseController,
(this.parseServer.config && this.parseServer.config.loggerController) ||
defaultLogger
);
}
async _getGraphQLOptions(req) {
return {
schema: await this.parseGraphQLSchema.load(),
context: {
info: req.info,
config: req.config,
auth: req.auth,
},
};
}
applyGraphQL(app) {
if (!app || !app.use) {
requiredParameter('You must provide an Express.js app instance!');
}
const maxUploadSize = this.parseServer.config.maxUploadSize || '20mb';
const maxFileSize =
(Number(maxUploadSize.slice(0, -2)) * 1024) ^
{
kb: 1,
mb: 2,
gb: 3,
}[maxUploadSize.slice(-2).toLowerCase()];
app.use(this.config.graphQLPath, graphqlUploadExpress({ maxFileSize }));
app.use(this.config.graphQLPath, corsMiddleware());
app.use(this.config.graphQLPath, bodyParser.json());
app.use(this.config.graphQLPath, handleParseHeaders);
app.use(this.config.graphQLPath, handleParseErrors);
app.use(
this.config.graphQLPath,
graphqlExpress(async req => await this._getGraphQLOptions(req))
);
}
applyPlayground(app) {
if (!app || !app.get) {
requiredParameter('You must provide an Express.js app instance!');
}
app.get(
this.config.playgroundPath ||
requiredParameter(
'You must provide a config.playgroundPath to applyPlayground!'
),
(_req, res) => {
res.setHeader('Content-Type', 'text/html');
res.write(
renderPlaygroundPage({
endpoint: this.config.graphQLPath,
subscriptionEndpoint: this.config.subscriptionsPath,
headers: {
'X-Parse-Application-Id': this.parseServer.config.appId,
'X-Parse-Master-Key': this.parseServer.config.masterKey,
},
})
);
res.end();
}
);
}
createSubscriptions(server) {
SubscriptionServer.create(
{
execute,
subscribe,
onOperation: async (_message, params, webSocket) =>
Object.assign(
{},
params,
await this._getGraphQLOptions(webSocket.upgradeReq)
),
},
{
server,
path:
this.config.subscriptionsPath ||
requiredParameter(
'You must provide a config.subscriptionsPath to createSubscriptions!'
),
}
);
}
}
export { ParseGraphQLServer };