forked from sid88in/serverless-appsync-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql-playground.js
109 lines (97 loc) · 3.35 KB
/
graphql-playground.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
const AWS = require('aws-sdk');
const Koa = require('koa');
const koaPlayground = require('graphql-playground-middleware-koa').default;
const { getServerlessStackName, getValue } = require('./get-stack-value');
function getOutputValue(service, provider, key) {
return provider
.request(
'CloudFormation',
'describeStacks',
{ StackName: getServerlessStackName(service, provider) },
)
.then((result) => {
const stack = result.Stacks.pop();
const output = stack.Outputs.find(o => o.OutputKey === key);
if (!output) {
throw new Error(`Output ${key}: not found in serverless stack`);
}
return output.OutputValue;
});
}
function getHeaders(service, provider, config, options) {
switch (config.authenticationType) {
case 'AMAZON_COGNITO_USER_POOLS': {
if (!options.username || !options.password) {
throw new Error('Username and Password required for authentication type - AMAZON_COGNITO_USER_POOLS');
}
return Promise.all([
getValue(service, provider, config.userPoolConfig.userPoolId, 'userPoolConfig.userPoolId'),
getValue(service, provider, options.clientId || config.userPoolConfig.playgroundClientId, 'userPoolConfig.playgroundClientId'),
])
.then(([UserPoolId, ClientId]) => {
const cognito = new AWS.CognitoIdentityServiceProvider(provider.getCredentials());
return cognito
.adminInitiateAuth({
AuthFlow: 'ADMIN_NO_SRP_AUTH',
UserPoolId,
ClientId,
AuthParameters: {
USERNAME: options.username,
PASSWORD: options.password,
},
})
.promise();
})
.then(({ AuthenticationResult }) => {
if (!AuthenticationResult) {
throw new Error('Authentication Failed');
}
return {
Authorization: AuthenticationResult.IdToken,
};
});
}
case 'API_KEY': {
return getOutputValue(service, provider, 'GraphQlApiKeyDefault').then(apiKey => ({
'X-Api-Key': apiKey,
}), () => {
if (options.apiKey) {
return { 'X-Api-Key': options.apiKey };
}
throw new Error('ApiKey required for authentication type (either as GraphQLApiKeyDefault output or as --apiKey option) - API_KEY');
});
}
case 'OPENID_CONNECT': {
if (!options.jwtToken) {
throw new Error('jwtToken required for authentication type - OPENID_CONNECT');
}
return Promise.resolve({ Authorization: options.jwtToken });
}
default:
throw new Error(`Authentication Type ${config.authenticationType} Not Supported for Graphiql`);
}
}
function runGraphqlPlayground(service, provider, config, options) {
return Promise.all([
getHeaders(service, provider, config, options),
getOutputValue(service, provider, 'GraphQlApiUrl'),
]).then(([headers, endpoint]) => {
const app = new Koa();
app.use(koaPlayground({
endpoint,
settings: {
'editor.cursorShape': 'line',
'editor.reuseHeaders': true,
},
tabs: [{
endpoint,
headers,
}],
}));
const port = options.port || 3000;
app.listen(port);
const graphiqlUrl = `http://localhost:${port}`;
return graphiqlUrl;
});
}
module.exports = runGraphqlPlayground;