forked from aws/aws-cdk
/
graphqlapi-base.ts
304 lines (277 loc) · 9.25 KB
/
graphqlapi-base.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { ITable } from '@aws-cdk/aws-dynamodb';
import { IDomain as IElasticsearchDomain } from '@aws-cdk/aws-elasticsearch';
import { IFunction } from '@aws-cdk/aws-lambda';
import { IDomain as IOpenSearchDomain } from '@aws-cdk/aws-opensearchservice';
import { IServerlessCluster } from '@aws-cdk/aws-rds';
import { ISecret } from '@aws-cdk/aws-secretsmanager';
import { CfnResource, IResource, Resource } from '@aws-cdk/core';
import { DynamoDbDataSource, HttpDataSource, LambdaDataSource, NoneDataSource, RdsDataSource, AwsIamConfig, ElasticsearchDataSource, OpenSearchDataSource } from './data-source';
import { Resolver, ExtendedResolverProps } from './resolver';
/**
* Optional configuration for data sources
*/
export interface DataSourceOptions {
/**
* The name of the data source, overrides the id given by cdk
*
* @default - generated by cdk given the id
*/
readonly name?: string;
/**
* The description of the data source
*
* @default - No description
*/
readonly description?: string;
}
/**
* Optional configuration for Http data sources
*/
export interface HttpDataSourceOptions extends DataSourceOptions {
/**
* The authorization config in case the HTTP endpoint requires authorization
*
* @default - none
*/
readonly authorizationConfig?: AwsIamConfig;
}
/**
* Interface for GraphQL
*/
export interface IGraphqlApi extends IResource {
/**
* an unique AWS AppSync GraphQL API identifier
* i.e. 'lxz775lwdrgcndgz3nurvac7oa'
*
* @attribute
*/
readonly apiId: string;
/**
* the ARN of the API
*
* @attribute
*/
readonly arn: string;
/**
* add a new dummy data source to this API. Useful for pipeline resolvers
* and for backend changes that don't require a data source.
*
* @param id The data source's id
* @param options The optional configuration for this data source
*/
addNoneDataSource(id: string, options?: DataSourceOptions): NoneDataSource;
/**
* add a new DynamoDB data source to this API
*
* @param id The data source's id
* @param table The DynamoDB table backing this data source
* @param options The optional configuration for this data source
*/
addDynamoDbDataSource(id: string, table: ITable, options?: DataSourceOptions): DynamoDbDataSource;
/**
* add a new http data source to this API
*
* @param id The data source's id
* @param endpoint The http endpoint
* @param options The optional configuration for this data source
*/
addHttpDataSource(id: string, endpoint: string, options?: HttpDataSourceOptions): HttpDataSource;
/**
* add a new Lambda data source to this API
*
* @param id The data source's id
* @param lambdaFunction The Lambda function to call to interact with this data source
* @param options The optional configuration for this data source
*/
addLambdaDataSource(id: string, lambdaFunction: IFunction, options?: DataSourceOptions): LambdaDataSource;
/**
* add a new Rds data source to this API
*
* @param id The data source's id
* @param serverlessCluster The serverless cluster to interact with this data source
* @param secretStore The secret store that contains the username and password for the serverless cluster
* @param databaseName The optional name of the database to use within the cluster
* @param options The optional configuration for this data source
*/
addRdsDataSource(
id: string,
serverlessCluster: IServerlessCluster,
secretStore: ISecret,
databaseName?: string,
options?: DataSourceOptions
): RdsDataSource;
/**
* add a new elasticsearch data source to this API
*
* @deprecated - use `addOpenSearchDataSource`
* @param id The data source's id
* @param domain The elasticsearch domain for this data source
* @param options The optional configuration for this data source
*/
addElasticsearchDataSource(id: string, domain: IElasticsearchDomain, options?: DataSourceOptions): ElasticsearchDataSource;
/**
* Add a new OpenSearch data source to this API
*
* @param id The data source's id
* @param domain The OpenSearch domain for this data source
* @param options The optional configuration for this data source
*/
addOpenSearchDataSource(id: string, domain: IOpenSearchDomain, options?: DataSourceOptions): OpenSearchDataSource;
/**
* creates a new resolver for this datasource and API using the given properties
*/
createResolver(id: string, props: ExtendedResolverProps): Resolver;
/**
* Add schema dependency if not imported
*
* @param construct the dependee
*/
addSchemaDependency(construct: CfnResource): boolean;
}
/**
* Base Class for GraphQL API
*/
export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
/**
* an unique AWS AppSync GraphQL API identifier
* i.e. 'lxz775lwdrgcndgz3nurvac7oa'
*/
public abstract readonly apiId: string;
/**
* the ARN of the API
*/
public abstract readonly arn: string;
/**
* add a new dummy data source to this API. Useful for pipeline resolvers
* and for backend changes that don't require a data source.
*
* @param id The data source's id
* @param options The optional configuration for this data source
*/
public addNoneDataSource(id: string, options?: DataSourceOptions): NoneDataSource {
return new NoneDataSource(this, id, {
api: this,
name: options?.name,
description: options?.description,
});
}
/**
* add a new DynamoDB data source to this API
*
* @param id The data source's id
* @param table The DynamoDB table backing this data source
* @param options The optional configuration for this data source
*/
public addDynamoDbDataSource(id: string, table: ITable, options?: DataSourceOptions): DynamoDbDataSource {
return new DynamoDbDataSource(this, id, {
api: this,
table,
name: options?.name,
description: options?.description,
});
}
/**
* add a new http data source to this API
*
* @param id The data source's id
* @param endpoint The http endpoint
* @param options The optional configuration for this data source
*/
public addHttpDataSource(id: string, endpoint: string, options?: HttpDataSourceOptions): HttpDataSource {
return new HttpDataSource(this, id, {
api: this,
endpoint,
name: options?.name,
description: options?.description,
authorizationConfig: options?.authorizationConfig,
});
}
/**
* add a new Lambda data source to this API
*
* @param id The data source's id
* @param lambdaFunction The Lambda function to call to interact with this data source
* @param options The optional configuration for this data source
*/
public addLambdaDataSource(id: string, lambdaFunction: IFunction, options?: DataSourceOptions): LambdaDataSource {
return new LambdaDataSource(this, id, {
api: this,
lambdaFunction,
name: options?.name,
description: options?.description,
});
}
/**
* add a new Rds data source to this API
* @param id The data source's id
* @param serverlessCluster The serverless cluster to interact with this data source
* @param secretStore The secret store that contains the username and password for the serverless cluster
* @param databaseName The optional name of the database to use within the cluster
* @param options The optional configuration for this data source
*/
public addRdsDataSource(
id: string,
serverlessCluster: IServerlessCluster,
secretStore: ISecret,
databaseName?: string,
options?: DataSourceOptions,
): RdsDataSource {
return new RdsDataSource(this, id, {
api: this,
name: options?.name,
description: options?.description,
serverlessCluster,
secretStore,
databaseName,
});
}
/**
* add a new elasticsearch data source to this API
*
* @deprecated - use `addOpenSearchDataSource`
* @param id The data source's id
* @param domain The elasticsearch domain for this data source
* @param options The optional configuration for this data source
*/
public addElasticsearchDataSource(id: string, domain: IElasticsearchDomain, options?: DataSourceOptions): ElasticsearchDataSource {
return new ElasticsearchDataSource(this, id, {
api: this,
name: options?.name,
description: options?.description,
domain,
});
}
/**
* add a new OpenSearch data source to this API
*
* @param id The data source's id
* @param domain The OpenSearch domain for this data source
* @param options The optional configuration for this data source
*/
public addOpenSearchDataSource(id: string, domain: IOpenSearchDomain, options?: DataSourceOptions): OpenSearchDataSource {
return new OpenSearchDataSource(this, id, {
api: this,
name: options?.name,
description: options?.description,
domain,
});
}
/**
* creates a new resolver for this datasource and API using the given properties
*/
public createResolver(id: string, props: ExtendedResolverProps): Resolver {
return new Resolver(this, id, {
api: this,
...props,
});
}
/**
* Add schema dependency if not imported
*
* @param construct the dependee
*/
public addSchemaDependency(construct: CfnResource): boolean {
construct;
return false;
}
}