-
Notifications
You must be signed in to change notification settings - Fork 816
/
create.ts
83 lines (81 loc) · 2.79 KB
/
create.ts
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
import { Command, command, param } from 'clime';
import File from '../types/File';
import GraphQLTransform from 'graphql-transform';
import DynamoDBTransformer from 'graphql-dynamodb-transformer'
import { SearchableModelTransformer } from 'graphql-elasticsearch-transformer'
import AuthTransformer from 'graphql-auth-transformer'
import log from '../log'
import { CloudFormation } from 'aws-sdk'
import { ResourceConstants } from 'graphql-transformer-common'
import AppSyncTransformer from 'graphql-appsync-transformer'
async function createStack(template: any, name: string, region: string) {
const cloudformation = new CloudFormation({ apiVersion: '2010-05-15', region });
const params = [
{
ParameterKey: ResourceConstants.PARAMETERS.AppSyncApiName,
ParameterValue: name
}
// {
// ParameterKey: 'ElasticSearchDomainName',
// ParameterValue: name.toLowerCase()
// },
// {
// ParameterKey: 'IAMRoleName',
// ParameterValue: name + 'Role'
// },
// {
// ParameterKey: 'StreamingIAMRoleName',
// ParameterValue: name + 'StreamingLambda'
// }
]
// const paramOverrides = Object.keys(params).map((k: string) => `${k}=${params[k]}`).join(' ')
return await new Promise((resolve, reject) => {
cloudformation.createStack({
StackName: name,
Capabilities: ['CAPABILITY_NAMED_IAM'],
Parameters: params,
TemplateBody: JSON.stringify(template)
}, (err: Error, data: any) => {
if (err) {
log.error(err.message)
reject(err)
return
}
resolve(data)
})
})
}
@command({
description: 'Deploy an AppSync API from your schema.graphql file',
})
export default class extends Command {
public async execute(
@param({
description: 'Path to schema.graphql',
required: true,
})
schema: File,
@param({
description: 'The name of the application',
required: true,
})
name: string,
@param({
description: 'The region to launch the stack in. Defaults to us-west-2',
required: false,
default: 'us-west-2'
})
region: string
) {
const transformer = new GraphQLTransform({
transformers: [
new AppSyncTransformer(),
new DynamoDBTransformer(),
new AuthTransformer()
]
})
const cfdoc = transformer.transform(schema.readSync());
const out = await createStack(cfdoc, name, region)
return 'Application creation successfully started. It may take a few minutes to finish.'
}
}