forked from aws/aws-cdk
/
integ.api-import.ts
90 lines (76 loc) · 2.8 KB
/
integ.api-import.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
84
85
86
87
88
89
90
/// !cdk-integ *
import * as path from 'path';
import * as db from '@aws-cdk/aws-dynamodb';
import * as cdk from '@aws-cdk/core';
import * as appsync from '../lib';
/*
* Creates an Appsync GraphQL API in a separate stack.
* Add dependencies to imported api.
*
* Stack verification steps:
* Install dependencies and deploy integration test. Check if data sources are
* connected to the GraphQL Api
*
* -- cdk deploy --app 'node integ.api-import.js' stack -- start --
* -- aws appsync list-graphql-apis -- obtain api id --
* -- aws appsync list-data-sources --api-id [api_id] -- testDS/None --
* -- cdk destroy --app 'node integ.api-import.js' stack baseStack -- clean --
*/
const app = new cdk.App();
const baseStack = new cdk.Stack(app, 'baseStack');
const baseApi = new appsync.GraphqlApi(baseStack, 'baseApi', {
name: 'baseApi',
schema: appsync.SchemaFile.fromAsset(path.join(__dirname, 'appsync.test.graphql')),
});
const stack = new cdk.Stack(app, 'stack');
const api = appsync.GraphqlApi.fromGraphqlApiAttributes(stack, 'Api', {
graphqlApiId: `${baseApi.apiId}`,
});
const testTable = new db.Table(stack, 'TestTable', {
billingMode: db.BillingMode.PAY_PER_REQUEST,
partitionKey: {
name: 'id',
type: db.AttributeType.STRING,
},
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
const testDS = api.addDynamoDbDataSource('ds', testTable);
testDS.createResolver('QueryGetTests', {
typeName: 'Query',
fieldName: 'getTests',
requestMappingTemplate: appsync.MappingTemplate.dynamoDbScanTable(),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultList(),
});
testDS.createResolver('MutationAddTest', {
typeName: 'Mutation',
fieldName: 'addTest',
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(appsync.PrimaryKey.partition('id').auto(), appsync.Values.projecting('test')),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});
const api2 = appsync.GraphqlApi.fromGraphqlApiAttributes(stack, 'api2', {
graphqlApiId: baseApi.apiId,
graphqlApiArn: baseApi.arn,
});
const none = api2.addNoneDataSource('none');
const func = none.createFunction('PipelineFunction', {
name: 'pipeline_function',
requestMappingTemplate: appsync.MappingTemplate.fromString(JSON.stringify({
version: '2017-02-28',
})),
responseMappingTemplate: appsync.MappingTemplate.fromString(JSON.stringify({
version: 'v1',
})),
});
new appsync.Resolver(stack, 'pipeline_resolver', {
api: api2,
typeName: 'test',
fieldName: 'version',
pipelineConfig: [func],
requestMappingTemplate: appsync.MappingTemplate.fromString(JSON.stringify({
version: '2017-02-28',
})),
responseMappingTemplate: appsync.MappingTemplate.fromString(JSON.stringify({
version: 'v1',
})),
});
app.synth();