@ttoss/appsync-api 0.18.32 → 0.18.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/appsync-api",
3
- "version": "0.18.32",
3
+ "version": "0.18.33",
4
4
  "description": "A library for building GraphQL APIs for AWS AppSync.",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -24,11 +24,11 @@
24
24
  ],
25
25
  "sideEffects": false,
26
26
  "dependencies": {
27
- "@ttoss/cloudformation": "^0.10.8"
27
+ "@ttoss/cloudformation": "^0.10.9"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "graphql": "^16.6.0",
31
- "@ttoss/graphql-api": "^0.7.4"
31
+ "@ttoss/graphql-api": "^0.7.5"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/aws-lambda": "^8.10.138",
@@ -36,9 +36,9 @@
36
36
  "graphql-shield": "^7.6.5",
37
37
  "jest": "^29.7.0",
38
38
  "tsup": "^8.3.0",
39
- "@ttoss/config": "^1.32.10",
40
- "@ttoss/graphql-api": "^0.7.4",
41
- "@ttoss/ids": "^0.2.10"
39
+ "@ttoss/config": "^1.33.0",
40
+ "@ttoss/graphql-api": "^0.7.5",
41
+ "@ttoss/ids": "^0.2.11"
42
42
  },
43
43
  "keywords": [
44
44
  "api",
package/dist/esm/index.js DELETED
@@ -1,333 +0,0 @@
1
- /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
-
3
- // src/createApiTemplate.ts
4
- import { graphql } from "@ttoss/graphql-api";
5
- var AppSyncGraphQLApiLogicalId = "AppSyncGraphQLApi";
6
- var AppSyncGraphQLSchemaLogicalId = "AppSyncGraphQLSchema";
7
- var AppSyncLambdaFunctionLogicalId = "AppSyncLambdaFunction";
8
- var AppSyncLambdaFunctionAppSyncDataSourceLogicalId = "AppSyncLambdaFunctionAppSyncDataSource";
9
- var AppSyncGraphQLApiKeyLogicalId = "AppSyncGraphQLApiKey";
10
- var createApiTemplate = ({
11
- additionalAuthenticationProviders,
12
- authenticationType = "AMAZON_COGNITO_USER_POOLS",
13
- schemaComposer,
14
- dataSource,
15
- lambdaFunction,
16
- userPoolConfig,
17
- customDomain
18
- }) => {
19
- const sdlWithoutComments = schemaComposer.toSDL({
20
- commentDescriptions: false,
21
- omitDescriptions: true,
22
- omitScalars: true
23
- });
24
- graphql.validateSchema(schemaComposer.buildSchema());
25
- const resolveMethods = schemaComposer.getResolveMethods();
26
- const resolveMethodsEntries = Object.entries(resolveMethods).flatMap(([typeName, fieldResolvers]) => {
27
- return Object.entries(fieldResolvers).map(([fieldName, resolver]) => {
28
- if (typeof resolver !== "function") {
29
- return void 0;
30
- }
31
- if (typeName.toLowerCase().includes("enum")) {
32
- return void 0;
33
- }
34
- return {
35
- fieldName,
36
- typeName
37
- };
38
- });
39
- }).filter(Boolean);
40
- const template = {
41
- AWSTemplateFormatVersion: "2010-09-09",
42
- Parameters: {
43
- Environment: {
44
- Default: "Staging",
45
- Type: "String",
46
- AllowedValues: ["Staging", "Production"]
47
- },
48
- LambdaS3Bucket: {
49
- Type: "String"
50
- },
51
- LambdaS3Key: {
52
- Type: "String"
53
- },
54
- LambdaS3ObjectVersion: {
55
- Type: "String"
56
- }
57
- },
58
- Resources: {
59
- /**
60
- * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-graphqlapi.html#cfn-appsync-graphqlapi-name
61
- */
62
- [AppSyncGraphQLApiLogicalId]: {
63
- Type: "AWS::AppSync::GraphQLApi",
64
- Properties: {
65
- AuthenticationType: authenticationType,
66
- Name: {
67
- Ref: "AWS::StackName"
68
- }
69
- }
70
- },
71
- [AppSyncGraphQLSchemaLogicalId]: {
72
- Type: "AWS::AppSync::GraphQLSchema",
73
- Properties: {
74
- ApiId: {
75
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"]
76
- },
77
- Definition: sdlWithoutComments
78
- }
79
- },
80
- [AppSyncLambdaFunctionLogicalId]: {
81
- Type: "AWS::Lambda::Function",
82
- Properties: {
83
- Code: {
84
- S3Bucket: {
85
- Ref: "LambdaS3Bucket"
86
- },
87
- S3Key: {
88
- Ref: "LambdaS3Key"
89
- },
90
- S3ObjectVersion: {
91
- Ref: "LambdaS3ObjectVersion"
92
- }
93
- },
94
- Handler: "index.handler",
95
- Layers: lambdaFunction.layers,
96
- MemorySize: 512,
97
- Role: lambdaFunction.roleArn,
98
- Runtime: "nodejs20.x",
99
- /**
100
- * https://docs.aws.amazon.com/general/latest/gr/appsync.html
101
- * Request execution time for mutations, queries, and subscriptions: 30 seconds
102
- */
103
- Timeout: 29
104
- }
105
- },
106
- [AppSyncLambdaFunctionAppSyncDataSourceLogicalId]: {
107
- Type: "AWS::AppSync::DataSource",
108
- Properties: {
109
- ApiId: {
110
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"]
111
- },
112
- LambdaConfig: {
113
- LambdaFunctionArn: {
114
- "Fn::GetAtt": [AppSyncLambdaFunctionLogicalId, "Arn"]
115
- }
116
- },
117
- Name: "AppSyncLambdaFunctionAppSyncDataSource",
118
- ServiceRoleArn: dataSource.roleArn,
119
- Type: "AWS_LAMBDA"
120
- }
121
- }
122
- },
123
- Outputs: {
124
- AppSyncApiGraphQLUrl: {
125
- Export: {
126
- Name: {
127
- "Fn::Join": [":", [{
128
- Ref: "AWS::StackName"
129
- }, "AppSyncApiGraphQLUrl"]]
130
- }
131
- },
132
- Value: {
133
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "GraphQLUrl"]
134
- }
135
- },
136
- AppSyncApiArn: {
137
- Export: {
138
- Name: {
139
- "Fn::Join": [":", [{
140
- Ref: "AWS::StackName"
141
- }, "AppSyncApiArn"]]
142
- }
143
- },
144
- Value: {
145
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "Arn"]
146
- }
147
- }
148
- }
149
- };
150
- resolveMethodsEntries.forEach(({
151
- fieldName,
152
- typeName
153
- }) => {
154
- template.Resources[`${fieldName}${typeName}AppSyncResolver`] = {
155
- Type: "AWS::AppSync::Resolver",
156
- DependsOn: AppSyncGraphQLSchemaLogicalId,
157
- Properties: {
158
- ApiId: {
159
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"]
160
- },
161
- FieldName: fieldName,
162
- TypeName: typeName,
163
- DataSourceName: {
164
- "Fn::GetAtt": [AppSyncLambdaFunctionAppSyncDataSourceLogicalId, "Name"]
165
- }
166
- }
167
- };
168
- });
169
- const apiKey = additionalAuthenticationProviders?.includes("API_KEY") || authenticationType === "API_KEY";
170
- const cognitoUserPoolAuth = additionalAuthenticationProviders?.includes("AMAZON_COGNITO_USER_POOLS") || authenticationType === "AMAZON_COGNITO_USER_POOLS";
171
- if (additionalAuthenticationProviders) {
172
- template.Resources[AppSyncGraphQLApiLogicalId].Properties.AdditionalAuthenticationProviders = additionalAuthenticationProviders?.map(provider => {
173
- return {
174
- AuthenticationType: provider
175
- };
176
- });
177
- }
178
- if (apiKey) {
179
- template.Resources[AppSyncGraphQLApiKeyLogicalId] = {
180
- Type: "AWS::AppSync::ApiKey",
181
- Properties: {
182
- ApiId: {
183
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"]
184
- }
185
- }
186
- };
187
- if (!template.Outputs) {
188
- template.Outputs = {};
189
- }
190
- template.Outputs[AppSyncGraphQLApiKeyLogicalId] = {
191
- Value: {
192
- "Fn::GetAtt": [AppSyncGraphQLApiKeyLogicalId, "ApiKey"]
193
- }
194
- };
195
- }
196
- if (cognitoUserPoolAuth) {
197
- if (!userPoolConfig) {
198
- throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authentication.");
199
- }
200
- template.Resources[AppSyncGraphQLApiLogicalId].Properties.UserPoolConfig = {
201
- AppIdClientRegex: userPoolConfig.appIdClientRegex,
202
- AwsRegion: userPoolConfig.awsRegion,
203
- DefaultAction: userPoolConfig.defaultAction,
204
- UserPoolId: userPoolConfig.userPoolId
205
- };
206
- }
207
- if (lambdaFunction.environment?.variables) {
208
- template.Resources[AppSyncLambdaFunctionLogicalId].Properties.Environment = {
209
- Variables: lambdaFunction.environment.variables
210
- };
211
- }
212
- if (customDomain) {
213
- const AppSyncDomainNameLogicalId = "AppSyncDomainName";
214
- template.Resources[AppSyncDomainNameLogicalId] = {
215
- Type: "AWS::AppSync::DomainName",
216
- Properties: {
217
- CertificateArn: customDomain.certificateArn,
218
- Description: "Custom domain for AppSync API",
219
- DomainName: customDomain.domainName
220
- }
221
- };
222
- if (customDomain.hostedZoneName) {
223
- const hostedZoneName = customDomain.hostedZoneName.endsWith(".") ? customDomain.hostedZoneName : `${customDomain.hostedZoneName}.`;
224
- template.Resources.AppSyncDomainNameRoute53RecordSet = {
225
- Type: "AWS::Route53::RecordSet",
226
- Properties: {
227
- HostedZoneName: hostedZoneName,
228
- Name: customDomain.domainName,
229
- ResourceRecords: [{
230
- "Fn::GetAtt": [AppSyncDomainNameLogicalId, "AppSyncDomainName"]
231
- }],
232
- TTL: "900",
233
- Type: "CNAME"
234
- }
235
- };
236
- }
237
- template.Resources.AppSyncDomainNameApiAssociation = {
238
- Type: "AWS::AppSync::DomainNameApiAssociation",
239
- Properties: {
240
- ApiId: {
241
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"]
242
- },
243
- DomainName: {
244
- "Fn::GetAtt": [AppSyncDomainNameLogicalId, "DomainName"]
245
- }
246
- }
247
- };
248
- if (!template.Outputs) {
249
- template.Outputs = {};
250
- }
251
- template.Outputs.DomainName = {
252
- Description: "Custom domain name for AppSync API",
253
- Value: {
254
- "Fn::GetAtt": [AppSyncDomainNameLogicalId, "DomainName"]
255
- }
256
- };
257
- template.Outputs.CloudFrontDomainName = {
258
- Description: "CloudFront domain name for AppSync API",
259
- Value: {
260
- "Fn::GetAtt": [AppSyncDomainNameLogicalId, "AppSyncDomainName"]
261
- }
262
- };
263
- }
264
- return template;
265
- };
266
-
267
- // src/createAppSyncResolverHandler.ts
268
- import { buildSchema } from "@ttoss/graphql-api";
269
- var createAppSyncResolverHandler = ({
270
- ...buildSchemaInput
271
- }) => {
272
- return async (event, appSyncHandlerContext) => {
273
- const {
274
- schemaComposer
275
- } = buildSchemaInput;
276
- const {
277
- info,
278
- arguments: args,
279
- source,
280
- request
281
- } = event;
282
- const {
283
- parentTypeName,
284
- fieldName
285
- } = info;
286
- const context = {
287
- handler: appSyncHandlerContext,
288
- request,
289
- identity: event.identity
290
- };
291
- const schema = buildSchema(buildSchemaInput);
292
- const parentType = schema.getType(parentTypeName);
293
- if (!parentType) {
294
- throw new Error(`Type ${parentTypeName} not found`);
295
- }
296
- const field = parentType.getFields()[fieldName];
297
- const resolver = field?.resolve;
298
- if (!resolver) {
299
- throw new Error(`Resolver for ${parentTypeName}.${fieldName} not found`);
300
- }
301
- const argsWithEnumValues = (() => {
302
- const fieldsArgsIsEnumType = field.args.filter(arg => {
303
- return schemaComposer.isEnumType(arg.type);
304
- });
305
- const enumArgs = fieldsArgsIsEnumType.map(enumArg => {
306
- if (!args[enumArg.name]) {
307
- return {
308
- [enumArg.name]: enumArg.defaultValue
309
- };
310
- }
311
- const values = schemaComposer.getETC(enumArg.type).getFields();
312
- return {
313
- [enumArg.name]: values[args[enumArg.name]].value
314
- };
315
- }).reduce((acc, curr) => {
316
- return {
317
- ...acc,
318
- ...curr
319
- };
320
- }, {});
321
- return {
322
- ...args,
323
- ...enumArgs
324
- };
325
- })();
326
- const response = await resolver(source, argsWithEnumValues, context, info);
327
- if (response instanceof Error) {
328
- throw response;
329
- }
330
- return response;
331
- };
332
- };
333
- export { createApiTemplate, createAppSyncResolverHandler };
package/dist/index.d.mts DELETED
@@ -1,120 +0,0 @@
1
- import { SchemaComposer, BuildSchemaInput } from '@ttoss/graphql-api';
2
- import { AppSyncResolverHandler as AppSyncResolverHandler$1 } from 'aws-lambda';
3
- export { AppSyncIdentityCognito } from 'aws-lambda';
4
-
5
- type Parameter = {
6
- AllowedValues?: string[];
7
- Default?: string | number;
8
- Description?: string;
9
- Type: string;
10
- NoEcho?: boolean;
11
- };
12
- type Parameters = {
13
- [key: string]: Parameter;
14
- };
15
- type Resource = {
16
- Type: string;
17
- DeletionPolicy?: 'Delete' | 'Retain';
18
- Description?: string;
19
- DependsOn?: string[] | string;
20
- Condition?: string;
21
- Properties: any;
22
- };
23
- type Policy = {
24
- PolicyName: string;
25
- PolicyDocument: {
26
- Version: '2012-10-17';
27
- Statement: {
28
- Sid?: string;
29
- Effect: 'Allow' | 'Deny';
30
- Action: string | string[];
31
- Resource: string | string[] | {
32
- [key: string]: any;
33
- } | {
34
- [key: string]: any;
35
- }[];
36
- }[];
37
- };
38
- };
39
- type IAMRoleResource = Resource & {
40
- Type: 'AWS::IAM::Role';
41
- Properties: {
42
- AssumeRolePolicyDocument: {
43
- Version: '2012-10-17';
44
- Statement: {
45
- Effect: 'Allow' | 'Deny';
46
- Action: string;
47
- Principal: any;
48
- Condition?: {
49
- [key: string]: any;
50
- };
51
- }[];
52
- };
53
- ManagedPolicyArns?: string[];
54
- Path?: string;
55
- Policies?: Policy[];
56
- };
57
- };
58
- type Resources = {
59
- [key: string]: IAMRoleResource | Resource;
60
- };
61
- type Output = {
62
- Description?: string;
63
- Value: string | any;
64
- Export?: {
65
- Name: string | any;
66
- };
67
- };
68
- type Outputs = {
69
- [key: string]: Output;
70
- };
71
- type CloudFormationTemplate = {
72
- AWSTemplateFormatVersion: '2010-09-09';
73
- Metadata?: any;
74
- Description?: string;
75
- Transform?: 'AWS::Serverless-2016-10-31';
76
- Mappings?: any;
77
- Conditions?: any;
78
- Parameters?: Parameters;
79
- Resources: Resources;
80
- Outputs?: Outputs;
81
- };
82
-
83
- type StringOrImport = string | {
84
- 'Fn::ImportValue': string;
85
- };
86
- /**
87
- * https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html
88
- */
89
- type AuthenticationType = 'API_KEY' | 'AWS_LAMBDA' | 'AWS_IAM' | 'OPENID_CONNECT' | 'AMAZON_COGNITO_USER_POOLS';
90
- declare const createApiTemplate: ({ additionalAuthenticationProviders, authenticationType, schemaComposer, dataSource, lambdaFunction, userPoolConfig, customDomain, }: {
91
- additionalAuthenticationProviders?: AuthenticationType[];
92
- authenticationType?: AuthenticationType;
93
- customDomain?: {
94
- domainName: string;
95
- certificateArn: string;
96
- hostedZoneName?: string;
97
- };
98
- dataSource: {
99
- roleArn: StringOrImport;
100
- };
101
- lambdaFunction: {
102
- environment?: {
103
- variables: Record<string, string>;
104
- };
105
- layers?: any;
106
- roleArn: StringOrImport;
107
- };
108
- schemaComposer: SchemaComposer<any>;
109
- userPoolConfig?: {
110
- appIdClientRegex: StringOrImport;
111
- awsRegion: StringOrImport;
112
- defaultAction: "ALLOW" | "DENY";
113
- userPoolId: StringOrImport;
114
- };
115
- }) => CloudFormationTemplate;
116
-
117
- type AppSyncResolverHandler<TArguments, TResult, TSource = Record<string, any> | null> = AppSyncResolverHandler$1<TArguments, TResult, TSource>;
118
- declare const createAppSyncResolverHandler: ({ ...buildSchemaInput }: BuildSchemaInput) => AppSyncResolverHandler<any, any, any>;
119
-
120
- export { type AppSyncResolverHandler, createApiTemplate, createAppSyncResolverHandler };
package/dist/index.d.ts DELETED
@@ -1,120 +0,0 @@
1
- import { SchemaComposer, BuildSchemaInput } from '@ttoss/graphql-api';
2
- import { AppSyncResolverHandler as AppSyncResolverHandler$1 } from 'aws-lambda';
3
- export { AppSyncIdentityCognito } from 'aws-lambda';
4
-
5
- type Parameter = {
6
- AllowedValues?: string[];
7
- Default?: string | number;
8
- Description?: string;
9
- Type: string;
10
- NoEcho?: boolean;
11
- };
12
- type Parameters = {
13
- [key: string]: Parameter;
14
- };
15
- type Resource = {
16
- Type: string;
17
- DeletionPolicy?: 'Delete' | 'Retain';
18
- Description?: string;
19
- DependsOn?: string[] | string;
20
- Condition?: string;
21
- Properties: any;
22
- };
23
- type Policy = {
24
- PolicyName: string;
25
- PolicyDocument: {
26
- Version: '2012-10-17';
27
- Statement: {
28
- Sid?: string;
29
- Effect: 'Allow' | 'Deny';
30
- Action: string | string[];
31
- Resource: string | string[] | {
32
- [key: string]: any;
33
- } | {
34
- [key: string]: any;
35
- }[];
36
- }[];
37
- };
38
- };
39
- type IAMRoleResource = Resource & {
40
- Type: 'AWS::IAM::Role';
41
- Properties: {
42
- AssumeRolePolicyDocument: {
43
- Version: '2012-10-17';
44
- Statement: {
45
- Effect: 'Allow' | 'Deny';
46
- Action: string;
47
- Principal: any;
48
- Condition?: {
49
- [key: string]: any;
50
- };
51
- }[];
52
- };
53
- ManagedPolicyArns?: string[];
54
- Path?: string;
55
- Policies?: Policy[];
56
- };
57
- };
58
- type Resources = {
59
- [key: string]: IAMRoleResource | Resource;
60
- };
61
- type Output = {
62
- Description?: string;
63
- Value: string | any;
64
- Export?: {
65
- Name: string | any;
66
- };
67
- };
68
- type Outputs = {
69
- [key: string]: Output;
70
- };
71
- type CloudFormationTemplate = {
72
- AWSTemplateFormatVersion: '2010-09-09';
73
- Metadata?: any;
74
- Description?: string;
75
- Transform?: 'AWS::Serverless-2016-10-31';
76
- Mappings?: any;
77
- Conditions?: any;
78
- Parameters?: Parameters;
79
- Resources: Resources;
80
- Outputs?: Outputs;
81
- };
82
-
83
- type StringOrImport = string | {
84
- 'Fn::ImportValue': string;
85
- };
86
- /**
87
- * https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html
88
- */
89
- type AuthenticationType = 'API_KEY' | 'AWS_LAMBDA' | 'AWS_IAM' | 'OPENID_CONNECT' | 'AMAZON_COGNITO_USER_POOLS';
90
- declare const createApiTemplate: ({ additionalAuthenticationProviders, authenticationType, schemaComposer, dataSource, lambdaFunction, userPoolConfig, customDomain, }: {
91
- additionalAuthenticationProviders?: AuthenticationType[];
92
- authenticationType?: AuthenticationType;
93
- customDomain?: {
94
- domainName: string;
95
- certificateArn: string;
96
- hostedZoneName?: string;
97
- };
98
- dataSource: {
99
- roleArn: StringOrImport;
100
- };
101
- lambdaFunction: {
102
- environment?: {
103
- variables: Record<string, string>;
104
- };
105
- layers?: any;
106
- roleArn: StringOrImport;
107
- };
108
- schemaComposer: SchemaComposer<any>;
109
- userPoolConfig?: {
110
- appIdClientRegex: StringOrImport;
111
- awsRegion: StringOrImport;
112
- defaultAction: "ALLOW" | "DENY";
113
- userPoolId: StringOrImport;
114
- };
115
- }) => CloudFormationTemplate;
116
-
117
- type AppSyncResolverHandler<TArguments, TResult, TSource = Record<string, any> | null> = AppSyncResolverHandler$1<TArguments, TResult, TSource>;
118
- declare const createAppSyncResolverHandler: ({ ...buildSchemaInput }: BuildSchemaInput) => AppSyncResolverHandler<any, any, any>;
119
-
120
- export { type AppSyncResolverHandler, createApiTemplate, createAppSyncResolverHandler };
package/dist/index.js DELETED
@@ -1,369 +0,0 @@
1
- /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
- "use strict";
3
-
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all) __defProp(target, name, {
10
- get: all[name],
11
- enumerable: true
12
- });
13
- };
14
- var __copyProps = (to, from, except, desc) => {
15
- if (from && typeof from === "object" || typeof from === "function") {
16
- for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
17
- get: () => from[key],
18
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
- });
20
- }
21
- return to;
22
- };
23
- var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
24
- value: true
25
- }), mod);
26
-
27
- // src/index.ts
28
- var src_exports = {};
29
- __export(src_exports, {
30
- createApiTemplate: () => createApiTemplate,
31
- createAppSyncResolverHandler: () => createAppSyncResolverHandler
32
- });
33
- module.exports = __toCommonJS(src_exports);
34
-
35
- // src/createApiTemplate.ts
36
- var import_graphql_api = require("@ttoss/graphql-api");
37
- var AppSyncGraphQLApiLogicalId = "AppSyncGraphQLApi";
38
- var AppSyncGraphQLSchemaLogicalId = "AppSyncGraphQLSchema";
39
- var AppSyncLambdaFunctionLogicalId = "AppSyncLambdaFunction";
40
- var AppSyncLambdaFunctionAppSyncDataSourceLogicalId = "AppSyncLambdaFunctionAppSyncDataSource";
41
- var AppSyncGraphQLApiKeyLogicalId = "AppSyncGraphQLApiKey";
42
- var createApiTemplate = ({
43
- additionalAuthenticationProviders,
44
- authenticationType = "AMAZON_COGNITO_USER_POOLS",
45
- schemaComposer,
46
- dataSource,
47
- lambdaFunction,
48
- userPoolConfig,
49
- customDomain
50
- }) => {
51
- const sdlWithoutComments = schemaComposer.toSDL({
52
- commentDescriptions: false,
53
- omitDescriptions: true,
54
- omitScalars: true
55
- });
56
- import_graphql_api.graphql.validateSchema(schemaComposer.buildSchema());
57
- const resolveMethods = schemaComposer.getResolveMethods();
58
- const resolveMethodsEntries = Object.entries(resolveMethods).flatMap(([typeName, fieldResolvers]) => {
59
- return Object.entries(fieldResolvers).map(([fieldName, resolver]) => {
60
- if (typeof resolver !== "function") {
61
- return void 0;
62
- }
63
- if (typeName.toLowerCase().includes("enum")) {
64
- return void 0;
65
- }
66
- return {
67
- fieldName,
68
- typeName
69
- };
70
- });
71
- }).filter(Boolean);
72
- const template = {
73
- AWSTemplateFormatVersion: "2010-09-09",
74
- Parameters: {
75
- Environment: {
76
- Default: "Staging",
77
- Type: "String",
78
- AllowedValues: ["Staging", "Production"]
79
- },
80
- LambdaS3Bucket: {
81
- Type: "String"
82
- },
83
- LambdaS3Key: {
84
- Type: "String"
85
- },
86
- LambdaS3ObjectVersion: {
87
- Type: "String"
88
- }
89
- },
90
- Resources: {
91
- /**
92
- * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-graphqlapi.html#cfn-appsync-graphqlapi-name
93
- */
94
- [AppSyncGraphQLApiLogicalId]: {
95
- Type: "AWS::AppSync::GraphQLApi",
96
- Properties: {
97
- AuthenticationType: authenticationType,
98
- Name: {
99
- Ref: "AWS::StackName"
100
- }
101
- }
102
- },
103
- [AppSyncGraphQLSchemaLogicalId]: {
104
- Type: "AWS::AppSync::GraphQLSchema",
105
- Properties: {
106
- ApiId: {
107
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"]
108
- },
109
- Definition: sdlWithoutComments
110
- }
111
- },
112
- [AppSyncLambdaFunctionLogicalId]: {
113
- Type: "AWS::Lambda::Function",
114
- Properties: {
115
- Code: {
116
- S3Bucket: {
117
- Ref: "LambdaS3Bucket"
118
- },
119
- S3Key: {
120
- Ref: "LambdaS3Key"
121
- },
122
- S3ObjectVersion: {
123
- Ref: "LambdaS3ObjectVersion"
124
- }
125
- },
126
- Handler: "index.handler",
127
- Layers: lambdaFunction.layers,
128
- MemorySize: 512,
129
- Role: lambdaFunction.roleArn,
130
- Runtime: "nodejs20.x",
131
- /**
132
- * https://docs.aws.amazon.com/general/latest/gr/appsync.html
133
- * Request execution time for mutations, queries, and subscriptions: 30 seconds
134
- */
135
- Timeout: 29
136
- }
137
- },
138
- [AppSyncLambdaFunctionAppSyncDataSourceLogicalId]: {
139
- Type: "AWS::AppSync::DataSource",
140
- Properties: {
141
- ApiId: {
142
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"]
143
- },
144
- LambdaConfig: {
145
- LambdaFunctionArn: {
146
- "Fn::GetAtt": [AppSyncLambdaFunctionLogicalId, "Arn"]
147
- }
148
- },
149
- Name: "AppSyncLambdaFunctionAppSyncDataSource",
150
- ServiceRoleArn: dataSource.roleArn,
151
- Type: "AWS_LAMBDA"
152
- }
153
- }
154
- },
155
- Outputs: {
156
- AppSyncApiGraphQLUrl: {
157
- Export: {
158
- Name: {
159
- "Fn::Join": [":", [{
160
- Ref: "AWS::StackName"
161
- }, "AppSyncApiGraphQLUrl"]]
162
- }
163
- },
164
- Value: {
165
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "GraphQLUrl"]
166
- }
167
- },
168
- AppSyncApiArn: {
169
- Export: {
170
- Name: {
171
- "Fn::Join": [":", [{
172
- Ref: "AWS::StackName"
173
- }, "AppSyncApiArn"]]
174
- }
175
- },
176
- Value: {
177
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "Arn"]
178
- }
179
- }
180
- }
181
- };
182
- resolveMethodsEntries.forEach(({
183
- fieldName,
184
- typeName
185
- }) => {
186
- template.Resources[`${fieldName}${typeName}AppSyncResolver`] = {
187
- Type: "AWS::AppSync::Resolver",
188
- DependsOn: AppSyncGraphQLSchemaLogicalId,
189
- Properties: {
190
- ApiId: {
191
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"]
192
- },
193
- FieldName: fieldName,
194
- TypeName: typeName,
195
- DataSourceName: {
196
- "Fn::GetAtt": [AppSyncLambdaFunctionAppSyncDataSourceLogicalId, "Name"]
197
- }
198
- }
199
- };
200
- });
201
- const apiKey = additionalAuthenticationProviders?.includes("API_KEY") || authenticationType === "API_KEY";
202
- const cognitoUserPoolAuth = additionalAuthenticationProviders?.includes("AMAZON_COGNITO_USER_POOLS") || authenticationType === "AMAZON_COGNITO_USER_POOLS";
203
- if (additionalAuthenticationProviders) {
204
- template.Resources[AppSyncGraphQLApiLogicalId].Properties.AdditionalAuthenticationProviders = additionalAuthenticationProviders?.map(provider => {
205
- return {
206
- AuthenticationType: provider
207
- };
208
- });
209
- }
210
- if (apiKey) {
211
- template.Resources[AppSyncGraphQLApiKeyLogicalId] = {
212
- Type: "AWS::AppSync::ApiKey",
213
- Properties: {
214
- ApiId: {
215
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"]
216
- }
217
- }
218
- };
219
- if (!template.Outputs) {
220
- template.Outputs = {};
221
- }
222
- template.Outputs[AppSyncGraphQLApiKeyLogicalId] = {
223
- Value: {
224
- "Fn::GetAtt": [AppSyncGraphQLApiKeyLogicalId, "ApiKey"]
225
- }
226
- };
227
- }
228
- if (cognitoUserPoolAuth) {
229
- if (!userPoolConfig) {
230
- throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authentication.");
231
- }
232
- template.Resources[AppSyncGraphQLApiLogicalId].Properties.UserPoolConfig = {
233
- AppIdClientRegex: userPoolConfig.appIdClientRegex,
234
- AwsRegion: userPoolConfig.awsRegion,
235
- DefaultAction: userPoolConfig.defaultAction,
236
- UserPoolId: userPoolConfig.userPoolId
237
- };
238
- }
239
- if (lambdaFunction.environment?.variables) {
240
- template.Resources[AppSyncLambdaFunctionLogicalId].Properties.Environment = {
241
- Variables: lambdaFunction.environment.variables
242
- };
243
- }
244
- if (customDomain) {
245
- const AppSyncDomainNameLogicalId = "AppSyncDomainName";
246
- template.Resources[AppSyncDomainNameLogicalId] = {
247
- Type: "AWS::AppSync::DomainName",
248
- Properties: {
249
- CertificateArn: customDomain.certificateArn,
250
- Description: "Custom domain for AppSync API",
251
- DomainName: customDomain.domainName
252
- }
253
- };
254
- if (customDomain.hostedZoneName) {
255
- const hostedZoneName = customDomain.hostedZoneName.endsWith(".") ? customDomain.hostedZoneName : `${customDomain.hostedZoneName}.`;
256
- template.Resources.AppSyncDomainNameRoute53RecordSet = {
257
- Type: "AWS::Route53::RecordSet",
258
- Properties: {
259
- HostedZoneName: hostedZoneName,
260
- Name: customDomain.domainName,
261
- ResourceRecords: [{
262
- "Fn::GetAtt": [AppSyncDomainNameLogicalId, "AppSyncDomainName"]
263
- }],
264
- TTL: "900",
265
- Type: "CNAME"
266
- }
267
- };
268
- }
269
- template.Resources.AppSyncDomainNameApiAssociation = {
270
- Type: "AWS::AppSync::DomainNameApiAssociation",
271
- Properties: {
272
- ApiId: {
273
- "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"]
274
- },
275
- DomainName: {
276
- "Fn::GetAtt": [AppSyncDomainNameLogicalId, "DomainName"]
277
- }
278
- }
279
- };
280
- if (!template.Outputs) {
281
- template.Outputs = {};
282
- }
283
- template.Outputs.DomainName = {
284
- Description: "Custom domain name for AppSync API",
285
- Value: {
286
- "Fn::GetAtt": [AppSyncDomainNameLogicalId, "DomainName"]
287
- }
288
- };
289
- template.Outputs.CloudFrontDomainName = {
290
- Description: "CloudFront domain name for AppSync API",
291
- Value: {
292
- "Fn::GetAtt": [AppSyncDomainNameLogicalId, "AppSyncDomainName"]
293
- }
294
- };
295
- }
296
- return template;
297
- };
298
-
299
- // src/createAppSyncResolverHandler.ts
300
- var import_graphql_api2 = require("@ttoss/graphql-api");
301
- var createAppSyncResolverHandler = ({
302
- ...buildSchemaInput
303
- }) => {
304
- return async (event, appSyncHandlerContext) => {
305
- const {
306
- schemaComposer
307
- } = buildSchemaInput;
308
- const {
309
- info,
310
- arguments: args,
311
- source,
312
- request
313
- } = event;
314
- const {
315
- parentTypeName,
316
- fieldName
317
- } = info;
318
- const context = {
319
- handler: appSyncHandlerContext,
320
- request,
321
- identity: event.identity
322
- };
323
- const schema = (0, import_graphql_api2.buildSchema)(buildSchemaInput);
324
- const parentType = schema.getType(parentTypeName);
325
- if (!parentType) {
326
- throw new Error(`Type ${parentTypeName} not found`);
327
- }
328
- const field = parentType.getFields()[fieldName];
329
- const resolver = field?.resolve;
330
- if (!resolver) {
331
- throw new Error(`Resolver for ${parentTypeName}.${fieldName} not found`);
332
- }
333
- const argsWithEnumValues = (() => {
334
- const fieldsArgsIsEnumType = field.args.filter(arg => {
335
- return schemaComposer.isEnumType(arg.type);
336
- });
337
- const enumArgs = fieldsArgsIsEnumType.map(enumArg => {
338
- if (!args[enumArg.name]) {
339
- return {
340
- [enumArg.name]: enumArg.defaultValue
341
- };
342
- }
343
- const values = schemaComposer.getETC(enumArg.type).getFields();
344
- return {
345
- [enumArg.name]: values[args[enumArg.name]].value
346
- };
347
- }).reduce((acc, curr) => {
348
- return {
349
- ...acc,
350
- ...curr
351
- };
352
- }, {});
353
- return {
354
- ...args,
355
- ...enumArgs
356
- };
357
- })();
358
- const response = await resolver(source, argsWithEnumValues, context, info);
359
- if (response instanceof Error) {
360
- throw response;
361
- }
362
- return response;
363
- };
364
- };
365
- // Annotate the CommonJS export names for ESM import in node:
366
- 0 && (module.exports = {
367
- createApiTemplate,
368
- createAppSyncResolverHandler
369
- });