/
appsync-function.ts
148 lines (139 loc) · 3.91 KB
/
appsync-function.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { Resource, IResource, Lazy, Fn } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnFunctionConfiguration } from './appsync.generated';
import { BaseDataSource } from './data-source';
import { IGraphqlApi } from './graphqlapi-base';
import { MappingTemplate } from './mapping-template';
/**
* the base properties for AppSync Functions
*/
export interface BaseAppsyncFunctionProps {
/**
* the name of the AppSync Function
*/
readonly name: string;
/**
* the description for this AppSync Function
*
* @default - no description
*/
readonly description?: string;
/**
* the request mapping template for the AppSync Function
*
* @default - no request mapping template
*/
readonly requestMappingTemplate?: MappingTemplate;
/**
* the response mapping template for the AppSync Function
*
* @default - no response mapping template
*/
readonly responseMappingTemplate?: MappingTemplate;
}
/**
* the CDK properties for AppSync Functions
*/
export interface AppsyncFunctionProps extends BaseAppsyncFunctionProps {
/**
* the GraphQL Api linked to this AppSync Function
*/
readonly api: IGraphqlApi;
/**
* the data source linked to this AppSync Function
*/
readonly dataSource: BaseDataSource;
}
/**
* The attributes for imported AppSync Functions
*/
export interface AppsyncFunctionAttributes {
/**
* the ARN of the AppSync function
*/
readonly functionArn: string;
}
/**
* Interface for AppSync Functions
*/
export interface IAppsyncFunction extends IResource {
/**
* the name of this AppSync Function
*
* @attribute
*/
readonly functionId: string;
/**
* the ARN of the AppSync function
*
* @attribute
*/
readonly functionArn: string;
}
/**
* AppSync Functions are local functions that perform certain operations
* onto a backend data source. Developers can compose operations (Functions)
* and execute them in sequence with Pipeline Resolvers.
*
* @resource AWS::AppSync::FunctionConfiguration
*/
export class AppsyncFunction extends Resource implements IAppsyncFunction {
/**
* Import Appsync Function from arn
*/
public static fromAppsyncFunctionAttributes(scope: Construct, id: string, attrs: AppsyncFunctionAttributes): IAppsyncFunction {
class Import extends Resource {
public readonly functionId = Lazy.stringValue({
produce: () => Fn.select(3, Fn.split('/', attrs.functionArn)),
});
public readonly functionArn = attrs.functionArn;
constructor (s: Construct, i: string) {
super(s, i);
}
}
return new Import(scope, id);
}
/**
* the name of this AppSync Function
*
* @attribute Name
*/
public readonly functionName: string;
/**
* the ARN of the AppSync function
*
* @attribute
*/
public readonly functionArn: string;
/**
* the ID of the AppSync function
*
* @attribute
*/
public readonly functionId: string;
/**
* the data source of this AppSync Function
*
* @attribute DataSourceName
*/
public readonly dataSource: BaseDataSource;
private readonly function: CfnFunctionConfiguration;
public constructor(scope: Construct, id: string, props: AppsyncFunctionProps) {
super(scope, id);
this.function = new CfnFunctionConfiguration(this, 'Resource', {
name: props.name,
description: props.description,
apiId: props.api.apiId,
dataSourceName: props.dataSource.name,
functionVersion: '2018-05-29',
requestMappingTemplate: props.requestMappingTemplate?.renderTemplate(),
responseMappingTemplate: props.responseMappingTemplate?.renderTemplate(),
});
this.functionName = this.function.attrName;
this.functionArn = this.function.attrFunctionArn;
this.functionId = this.function.attrFunctionId;
this.dataSource = props.dataSource;
this.function.addDependsOn(this.dataSource.ds);
props.api.addSchemaDependency(this.function);
}
}