-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplugin.js
46 lines (42 loc) · 1.21 KB
/
plugin.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
const path = require('path');
const server = require('./server');
class MyPlugin {
constructor(sls, options) {
this.sls = sls;
this.options = options;
this.commands = {
graphiql: {
usage: 'Creates graphiql server',
lifecycleEvents: ['start'],
options: {
function: {
usage: 'Name of the GraphQL function. Default: graphql',
shortcut: 'f',
},
port: {
usage: 'Port to listen on. Default: 8000',
shortcut: 'p',
},
},
},
};
this.hooks = {
'graphiql:start': this.graphiqlStart.bind(this),
};
}
graphiqlStart() {
const babelOptions = ((this.sls.config.serverless.service.custom || {})['graphiql'] || {}).babelOptions;
if (babelOptions) {
require('babel-register')(babelOptions);
}
const fnName = this.options.function || 'graphql';
const fn = this.sls.config.serverless.service.functions[fnName].handler;
const [handler, graphql] = fn.split('.');
const fullPath = path.join(process.cwd(), handler);
return server.start({
handler: require(fullPath)[graphql],
port: this.options.port,
});
}
}
module.exports = MyPlugin;