@ttoss/appsync-api 0.2.0
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/dist/esm/index.js +210 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +236 -0
- package/package.json +33 -0
- package/src/appSyncResolverHandler.ts +17 -0
- package/src/createApiTemplate.ts +217 -0
- package/src/index.ts +3 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { schemaComposer } from "graphql-compose";
|
|
5
|
+
|
|
6
|
+
// src/createApiTemplate.ts
|
|
7
|
+
var AppSyncGraphQLApiLogicalId = "AppSyncGraphQLApi";
|
|
8
|
+
var AppSyncGraphQLSchemaLogicalId = "AppSyncGraphQLSchema";
|
|
9
|
+
var AppSyncLambdaFunctionIAMRoleLogicalId = "AppSyncLambdaFunctionIAMRole";
|
|
10
|
+
var AppSyncLambdaFunctionLogicalId = "AppSyncLambdaFunction";
|
|
11
|
+
var AppSyncLambdaFunctionAppSyncDataSourceIAMRoleLogicalId = "AppSyncLambdaFunctionAppSyncDataSourceIAMRole";
|
|
12
|
+
var AppSyncLambdaFunctionAppSyncDataSourceLogicalId = "AppSyncLambdaFunctionAppSyncDataSource";
|
|
13
|
+
var createApiTemplate = ({
|
|
14
|
+
schemaComposer: schemaComposer2
|
|
15
|
+
}) => {
|
|
16
|
+
const sdl = schemaComposer2.toSDL();
|
|
17
|
+
const resolveMethods = schemaComposer2.getResolveMethods();
|
|
18
|
+
const resolveMethodsEntries = Object.entries(resolveMethods).flatMap(
|
|
19
|
+
([typeName, fieldResolvers]) => {
|
|
20
|
+
return Object.entries(fieldResolvers).map(([fieldName]) => {
|
|
21
|
+
return {
|
|
22
|
+
fieldName,
|
|
23
|
+
typeName
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
);
|
|
28
|
+
const template = {
|
|
29
|
+
AWSTemplateFormatVersion: "2010-09-09",
|
|
30
|
+
Parameters: {
|
|
31
|
+
Environment: {
|
|
32
|
+
Default: "Staging",
|
|
33
|
+
Type: "String",
|
|
34
|
+
AllowedValues: ["Staging", "Production"]
|
|
35
|
+
},
|
|
36
|
+
LambdaS3Bucket: {
|
|
37
|
+
Type: "String"
|
|
38
|
+
},
|
|
39
|
+
LambdaS3Key: {
|
|
40
|
+
Type: "String"
|
|
41
|
+
},
|
|
42
|
+
LambdaS3ObjectVersion: {
|
|
43
|
+
Type: "String"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
Resources: {
|
|
47
|
+
[AppSyncGraphQLApiLogicalId]: {
|
|
48
|
+
Type: "AWS::AppSync::GraphQLApi",
|
|
49
|
+
Properties: {
|
|
50
|
+
AuthenticationType: "API_KEY",
|
|
51
|
+
Name: {
|
|
52
|
+
"Fn::Join": [
|
|
53
|
+
":",
|
|
54
|
+
[{ Ref: "AWS::StackName" }, AppSyncGraphQLApiLogicalId]
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
[AppSyncGraphQLSchemaLogicalId]: {
|
|
60
|
+
Type: "AWS::AppSync::GraphQLSchema",
|
|
61
|
+
Properties: {
|
|
62
|
+
ApiId: { "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"] },
|
|
63
|
+
Definition: sdl
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
[AppSyncLambdaFunctionIAMRoleLogicalId]: {
|
|
67
|
+
Type: "AWS::IAM::Role",
|
|
68
|
+
Properties: {
|
|
69
|
+
AssumeRolePolicyDocument: {
|
|
70
|
+
Version: "2012-10-17",
|
|
71
|
+
Statement: [
|
|
72
|
+
{
|
|
73
|
+
Effect: "Allow",
|
|
74
|
+
Action: "sts:AssumeRole",
|
|
75
|
+
Principal: {
|
|
76
|
+
Service: "lambda.amazonaws.com"
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
},
|
|
81
|
+
ManagedPolicyArns: [
|
|
82
|
+
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
|
|
83
|
+
],
|
|
84
|
+
Path: "/custom-iam/"
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
[AppSyncLambdaFunctionLogicalId]: {
|
|
88
|
+
Type: "AWS::Lambda::Function",
|
|
89
|
+
Properties: {
|
|
90
|
+
Code: {
|
|
91
|
+
S3Bucket: { Ref: "LambdaS3Bucket" },
|
|
92
|
+
S3Key: { Ref: "LambdaS3Key" },
|
|
93
|
+
S3ObjectVersion: { Ref: "LambdaS3ObjectVersion" }
|
|
94
|
+
},
|
|
95
|
+
Handler: "index.handler",
|
|
96
|
+
Layers: [
|
|
97
|
+
{
|
|
98
|
+
"Fn::ImportValue": "LambdaLayer-Graphql-16-6-0"
|
|
99
|
+
}
|
|
100
|
+
],
|
|
101
|
+
MemorySize: 512,
|
|
102
|
+
Role: {
|
|
103
|
+
"Fn::GetAtt": [AppSyncLambdaFunctionIAMRoleLogicalId, "Arn"]
|
|
104
|
+
},
|
|
105
|
+
Runtime: "nodejs18.x",
|
|
106
|
+
Timeout: 29
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
[AppSyncLambdaFunctionAppSyncDataSourceIAMRoleLogicalId]: {
|
|
110
|
+
Type: "AWS::IAM::Role",
|
|
111
|
+
Properties: {
|
|
112
|
+
AssumeRolePolicyDocument: {
|
|
113
|
+
Version: "2012-10-17",
|
|
114
|
+
Statement: [
|
|
115
|
+
{
|
|
116
|
+
Effect: "Allow",
|
|
117
|
+
Action: "sts:AssumeRole",
|
|
118
|
+
Principal: {
|
|
119
|
+
Service: "appsync.amazonaws.com"
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
]
|
|
123
|
+
},
|
|
124
|
+
ManagedPolicyArns: [
|
|
125
|
+
"arn:aws:iam::aws:policy/service-role/AWSAppSyncPushToCloudWatchLogs"
|
|
126
|
+
],
|
|
127
|
+
Path: "/custom-iam/",
|
|
128
|
+
Policies: [
|
|
129
|
+
{
|
|
130
|
+
PolicyName: "AppSyncGraphQLApiIAMRolePolicyName",
|
|
131
|
+
PolicyDocument: {
|
|
132
|
+
Version: "2012-10-17",
|
|
133
|
+
Statement: [
|
|
134
|
+
{
|
|
135
|
+
Effect: "Allow",
|
|
136
|
+
Action: ["lambda:InvokeFunction"],
|
|
137
|
+
Resource: [
|
|
138
|
+
{ "Fn::GetAtt": [AppSyncLambdaFunctionLogicalId, "Arn"] }
|
|
139
|
+
]
|
|
140
|
+
}
|
|
141
|
+
]
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
]
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
[AppSyncLambdaFunctionAppSyncDataSourceLogicalId]: {
|
|
148
|
+
Type: "AWS::AppSync::DataSource",
|
|
149
|
+
Properties: {
|
|
150
|
+
ApiId: { "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"] },
|
|
151
|
+
LambdaConfig: {
|
|
152
|
+
LambdaFunctionArn: {
|
|
153
|
+
"Fn::GetAtt": [AppSyncLambdaFunctionLogicalId, "Arn"]
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
Name: "AppSyncLambdaFunctionAppSyncDataSource",
|
|
157
|
+
ServiceRoleArn: {
|
|
158
|
+
"Fn::GetAtt": [
|
|
159
|
+
AppSyncLambdaFunctionAppSyncDataSourceIAMRoleLogicalId,
|
|
160
|
+
"Arn"
|
|
161
|
+
]
|
|
162
|
+
},
|
|
163
|
+
Type: "AWS_LAMBDA"
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
Outputs: {
|
|
168
|
+
[AppSyncGraphQLApiLogicalId]: {
|
|
169
|
+
Value: {
|
|
170
|
+
"Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "GraphQLUrl"]
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
resolveMethodsEntries.forEach(({ fieldName, typeName }) => {
|
|
176
|
+
template.Resources[`${fieldName}${typeName}AppSyncResolver`] = {
|
|
177
|
+
Type: "AWS::AppSync::Resolver",
|
|
178
|
+
DependsOn: AppSyncGraphQLSchemaLogicalId,
|
|
179
|
+
Properties: {
|
|
180
|
+
ApiId: { "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"] },
|
|
181
|
+
FieldName: fieldName,
|
|
182
|
+
TypeName: typeName,
|
|
183
|
+
DataSourceName: {
|
|
184
|
+
"Fn::GetAtt": [
|
|
185
|
+
AppSyncLambdaFunctionAppSyncDataSourceLogicalId,
|
|
186
|
+
"Name"
|
|
187
|
+
]
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
return template;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// src/appSyncResolverHandler.ts
|
|
196
|
+
var appSyncResolverHandler = ({
|
|
197
|
+
schemaComposer: schemaComposer2
|
|
198
|
+
}) => {
|
|
199
|
+
return async (event, context) => {
|
|
200
|
+
const { info, arguments: args, source } = event;
|
|
201
|
+
const { parentTypeName, fieldName } = info;
|
|
202
|
+
const resolver = schemaComposer2.getResolveMethods()[parentTypeName][fieldName];
|
|
203
|
+
return resolver(source, args, context, info);
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
export {
|
|
207
|
+
appSyncResolverHandler,
|
|
208
|
+
createApiTemplate,
|
|
209
|
+
schemaComposer
|
|
210
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SchemaComposer } from 'graphql-compose';
|
|
2
|
+
export { ResolverResolveParams, schemaComposer } from 'graphql-compose';
|
|
3
|
+
import { CloudFormationTemplate } from 'carlin/src/utils/cloudFormationTemplate';
|
|
4
|
+
import { AppSyncResolverHandler } from 'aws-lambda';
|
|
5
|
+
|
|
6
|
+
declare const createApiTemplate: ({ schemaComposer, }: {
|
|
7
|
+
schemaComposer: SchemaComposer<any>;
|
|
8
|
+
}) => CloudFormationTemplate;
|
|
9
|
+
|
|
10
|
+
declare const appSyncResolverHandler: ({ schemaComposer, }: {
|
|
11
|
+
schemaComposer: SchemaComposer<any>;
|
|
12
|
+
}) => AppSyncResolverHandler<any, any, any>;
|
|
13
|
+
|
|
14
|
+
export { appSyncResolverHandler, createApiTemplate };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var src_exports = {};
|
|
23
|
+
__export(src_exports, {
|
|
24
|
+
appSyncResolverHandler: () => appSyncResolverHandler,
|
|
25
|
+
createApiTemplate: () => createApiTemplate,
|
|
26
|
+
schemaComposer: () => import_graphql_compose.schemaComposer
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(src_exports);
|
|
29
|
+
var import_graphql_compose = require("graphql-compose");
|
|
30
|
+
|
|
31
|
+
// src/createApiTemplate.ts
|
|
32
|
+
var AppSyncGraphQLApiLogicalId = "AppSyncGraphQLApi";
|
|
33
|
+
var AppSyncGraphQLSchemaLogicalId = "AppSyncGraphQLSchema";
|
|
34
|
+
var AppSyncLambdaFunctionIAMRoleLogicalId = "AppSyncLambdaFunctionIAMRole";
|
|
35
|
+
var AppSyncLambdaFunctionLogicalId = "AppSyncLambdaFunction";
|
|
36
|
+
var AppSyncLambdaFunctionAppSyncDataSourceIAMRoleLogicalId = "AppSyncLambdaFunctionAppSyncDataSourceIAMRole";
|
|
37
|
+
var AppSyncLambdaFunctionAppSyncDataSourceLogicalId = "AppSyncLambdaFunctionAppSyncDataSource";
|
|
38
|
+
var createApiTemplate = ({
|
|
39
|
+
schemaComposer: schemaComposer2
|
|
40
|
+
}) => {
|
|
41
|
+
const sdl = schemaComposer2.toSDL();
|
|
42
|
+
const resolveMethods = schemaComposer2.getResolveMethods();
|
|
43
|
+
const resolveMethodsEntries = Object.entries(resolveMethods).flatMap(
|
|
44
|
+
([typeName, fieldResolvers]) => {
|
|
45
|
+
return Object.entries(fieldResolvers).map(([fieldName]) => {
|
|
46
|
+
return {
|
|
47
|
+
fieldName,
|
|
48
|
+
typeName
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
const template = {
|
|
54
|
+
AWSTemplateFormatVersion: "2010-09-09",
|
|
55
|
+
Parameters: {
|
|
56
|
+
Environment: {
|
|
57
|
+
Default: "Staging",
|
|
58
|
+
Type: "String",
|
|
59
|
+
AllowedValues: ["Staging", "Production"]
|
|
60
|
+
},
|
|
61
|
+
LambdaS3Bucket: {
|
|
62
|
+
Type: "String"
|
|
63
|
+
},
|
|
64
|
+
LambdaS3Key: {
|
|
65
|
+
Type: "String"
|
|
66
|
+
},
|
|
67
|
+
LambdaS3ObjectVersion: {
|
|
68
|
+
Type: "String"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
Resources: {
|
|
72
|
+
[AppSyncGraphQLApiLogicalId]: {
|
|
73
|
+
Type: "AWS::AppSync::GraphQLApi",
|
|
74
|
+
Properties: {
|
|
75
|
+
AuthenticationType: "API_KEY",
|
|
76
|
+
Name: {
|
|
77
|
+
"Fn::Join": [
|
|
78
|
+
":",
|
|
79
|
+
[{ Ref: "AWS::StackName" }, AppSyncGraphQLApiLogicalId]
|
|
80
|
+
]
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
[AppSyncGraphQLSchemaLogicalId]: {
|
|
85
|
+
Type: "AWS::AppSync::GraphQLSchema",
|
|
86
|
+
Properties: {
|
|
87
|
+
ApiId: { "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"] },
|
|
88
|
+
Definition: sdl
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
[AppSyncLambdaFunctionIAMRoleLogicalId]: {
|
|
92
|
+
Type: "AWS::IAM::Role",
|
|
93
|
+
Properties: {
|
|
94
|
+
AssumeRolePolicyDocument: {
|
|
95
|
+
Version: "2012-10-17",
|
|
96
|
+
Statement: [
|
|
97
|
+
{
|
|
98
|
+
Effect: "Allow",
|
|
99
|
+
Action: "sts:AssumeRole",
|
|
100
|
+
Principal: {
|
|
101
|
+
Service: "lambda.amazonaws.com"
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
]
|
|
105
|
+
},
|
|
106
|
+
ManagedPolicyArns: [
|
|
107
|
+
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
|
|
108
|
+
],
|
|
109
|
+
Path: "/custom-iam/"
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
[AppSyncLambdaFunctionLogicalId]: {
|
|
113
|
+
Type: "AWS::Lambda::Function",
|
|
114
|
+
Properties: {
|
|
115
|
+
Code: {
|
|
116
|
+
S3Bucket: { Ref: "LambdaS3Bucket" },
|
|
117
|
+
S3Key: { Ref: "LambdaS3Key" },
|
|
118
|
+
S3ObjectVersion: { Ref: "LambdaS3ObjectVersion" }
|
|
119
|
+
},
|
|
120
|
+
Handler: "index.handler",
|
|
121
|
+
Layers: [
|
|
122
|
+
{
|
|
123
|
+
"Fn::ImportValue": "LambdaLayer-Graphql-16-6-0"
|
|
124
|
+
}
|
|
125
|
+
],
|
|
126
|
+
MemorySize: 512,
|
|
127
|
+
Role: {
|
|
128
|
+
"Fn::GetAtt": [AppSyncLambdaFunctionIAMRoleLogicalId, "Arn"]
|
|
129
|
+
},
|
|
130
|
+
Runtime: "nodejs18.x",
|
|
131
|
+
Timeout: 29
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
[AppSyncLambdaFunctionAppSyncDataSourceIAMRoleLogicalId]: {
|
|
135
|
+
Type: "AWS::IAM::Role",
|
|
136
|
+
Properties: {
|
|
137
|
+
AssumeRolePolicyDocument: {
|
|
138
|
+
Version: "2012-10-17",
|
|
139
|
+
Statement: [
|
|
140
|
+
{
|
|
141
|
+
Effect: "Allow",
|
|
142
|
+
Action: "sts:AssumeRole",
|
|
143
|
+
Principal: {
|
|
144
|
+
Service: "appsync.amazonaws.com"
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
]
|
|
148
|
+
},
|
|
149
|
+
ManagedPolicyArns: [
|
|
150
|
+
"arn:aws:iam::aws:policy/service-role/AWSAppSyncPushToCloudWatchLogs"
|
|
151
|
+
],
|
|
152
|
+
Path: "/custom-iam/",
|
|
153
|
+
Policies: [
|
|
154
|
+
{
|
|
155
|
+
PolicyName: "AppSyncGraphQLApiIAMRolePolicyName",
|
|
156
|
+
PolicyDocument: {
|
|
157
|
+
Version: "2012-10-17",
|
|
158
|
+
Statement: [
|
|
159
|
+
{
|
|
160
|
+
Effect: "Allow",
|
|
161
|
+
Action: ["lambda:InvokeFunction"],
|
|
162
|
+
Resource: [
|
|
163
|
+
{ "Fn::GetAtt": [AppSyncLambdaFunctionLogicalId, "Arn"] }
|
|
164
|
+
]
|
|
165
|
+
}
|
|
166
|
+
]
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
]
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
[AppSyncLambdaFunctionAppSyncDataSourceLogicalId]: {
|
|
173
|
+
Type: "AWS::AppSync::DataSource",
|
|
174
|
+
Properties: {
|
|
175
|
+
ApiId: { "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"] },
|
|
176
|
+
LambdaConfig: {
|
|
177
|
+
LambdaFunctionArn: {
|
|
178
|
+
"Fn::GetAtt": [AppSyncLambdaFunctionLogicalId, "Arn"]
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
Name: "AppSyncLambdaFunctionAppSyncDataSource",
|
|
182
|
+
ServiceRoleArn: {
|
|
183
|
+
"Fn::GetAtt": [
|
|
184
|
+
AppSyncLambdaFunctionAppSyncDataSourceIAMRoleLogicalId,
|
|
185
|
+
"Arn"
|
|
186
|
+
]
|
|
187
|
+
},
|
|
188
|
+
Type: "AWS_LAMBDA"
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
Outputs: {
|
|
193
|
+
[AppSyncGraphQLApiLogicalId]: {
|
|
194
|
+
Value: {
|
|
195
|
+
"Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "GraphQLUrl"]
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
resolveMethodsEntries.forEach(({ fieldName, typeName }) => {
|
|
201
|
+
template.Resources[`${fieldName}${typeName}AppSyncResolver`] = {
|
|
202
|
+
Type: "AWS::AppSync::Resolver",
|
|
203
|
+
DependsOn: AppSyncGraphQLSchemaLogicalId,
|
|
204
|
+
Properties: {
|
|
205
|
+
ApiId: { "Fn::GetAtt": [AppSyncGraphQLApiLogicalId, "ApiId"] },
|
|
206
|
+
FieldName: fieldName,
|
|
207
|
+
TypeName: typeName,
|
|
208
|
+
DataSourceName: {
|
|
209
|
+
"Fn::GetAtt": [
|
|
210
|
+
AppSyncLambdaFunctionAppSyncDataSourceLogicalId,
|
|
211
|
+
"Name"
|
|
212
|
+
]
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
});
|
|
217
|
+
return template;
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// src/appSyncResolverHandler.ts
|
|
221
|
+
var appSyncResolverHandler = ({
|
|
222
|
+
schemaComposer: schemaComposer2
|
|
223
|
+
}) => {
|
|
224
|
+
return async (event, context) => {
|
|
225
|
+
const { info, arguments: args, source } = event;
|
|
226
|
+
const { parentTypeName, fieldName } = info;
|
|
227
|
+
const resolver = schemaComposer2.getResolveMethods()[parentTypeName][fieldName];
|
|
228
|
+
return resolver(source, args, context, info);
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
232
|
+
0 && (module.exports = {
|
|
233
|
+
appSyncResolverHandler,
|
|
234
|
+
createApiTemplate,
|
|
235
|
+
schemaComposer
|
|
236
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ttoss/appsync-api",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"author": "ttoss",
|
|
7
|
+
"contributors": [
|
|
8
|
+
"Pedro Arantes <pedro@arantespp.com> (https://arantespp.com)"
|
|
9
|
+
],
|
|
10
|
+
"main": "dist/index.js",
|
|
11
|
+
"module": "dist/esm/index.js",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"src"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsup",
|
|
18
|
+
"test": "jest"
|
|
19
|
+
},
|
|
20
|
+
"typings": "dist/index.d.ts",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"graphql": "^16.6.0",
|
|
23
|
+
"graphql-compose": "^9.0.10"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/aws-lambda": "^8.10.109"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"gitHead": "7283c8bffc1b432659339927173eee2ad19b622d"
|
|
33
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AppSyncResolverHandler } from 'aws-lambda';
|
|
2
|
+
import type { SchemaComposer } from 'graphql-compose';
|
|
3
|
+
|
|
4
|
+
export const appSyncResolverHandler = ({
|
|
5
|
+
schemaComposer,
|
|
6
|
+
}: {
|
|
7
|
+
schemaComposer: SchemaComposer<any>;
|
|
8
|
+
}): AppSyncResolverHandler<any, any, any> => {
|
|
9
|
+
return async (event, context) => {
|
|
10
|
+
const { info, arguments: args, source } = event;
|
|
11
|
+
const { parentTypeName, fieldName } = info;
|
|
12
|
+
const resolver = (
|
|
13
|
+
schemaComposer.getResolveMethods()[parentTypeName] as any
|
|
14
|
+
)[fieldName];
|
|
15
|
+
return resolver(source, args, context, info);
|
|
16
|
+
};
|
|
17
|
+
};
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import type { CloudFormationTemplate } from 'carlin/src/utils/cloudFormationTemplate';
|
|
2
|
+
import type { SchemaComposer } from 'graphql-compose';
|
|
3
|
+
|
|
4
|
+
const AppSyncGraphQLApiLogicalId = 'AppSyncGraphQLApi';
|
|
5
|
+
|
|
6
|
+
const AppSyncGraphQLSchemaLogicalId = 'AppSyncGraphQLSchema';
|
|
7
|
+
|
|
8
|
+
const AppSyncLambdaFunctionIAMRoleLogicalId = 'AppSyncLambdaFunctionIAMRole';
|
|
9
|
+
|
|
10
|
+
const AppSyncLambdaFunctionLogicalId = 'AppSyncLambdaFunction';
|
|
11
|
+
|
|
12
|
+
const AppSyncLambdaFunctionAppSyncDataSourceIAMRoleLogicalId =
|
|
13
|
+
'AppSyncLambdaFunctionAppSyncDataSourceIAMRole';
|
|
14
|
+
|
|
15
|
+
const AppSyncLambdaFunctionAppSyncDataSourceLogicalId =
|
|
16
|
+
'AppSyncLambdaFunctionAppSyncDataSource';
|
|
17
|
+
|
|
18
|
+
export const createApiTemplate = ({
|
|
19
|
+
schemaComposer,
|
|
20
|
+
}: {
|
|
21
|
+
schemaComposer: SchemaComposer<any>;
|
|
22
|
+
}): CloudFormationTemplate => {
|
|
23
|
+
/**
|
|
24
|
+
* It should be on top of the file, otherwise it will have empty Mutation
|
|
25
|
+
* or Subscription if there are no resolvers for them.
|
|
26
|
+
*/
|
|
27
|
+
const sdl = schemaComposer.toSDL();
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Get FieldName and TypeName. `resolveMethods` is a Map of
|
|
31
|
+
* `typeName: { fieldName: resolverFn }`
|
|
32
|
+
*/
|
|
33
|
+
const resolveMethods = schemaComposer.getResolveMethods();
|
|
34
|
+
|
|
35
|
+
const resolveMethodsEntries = Object.entries(resolveMethods).flatMap(
|
|
36
|
+
([typeName, fieldResolvers]) => {
|
|
37
|
+
return Object.entries(fieldResolvers).map(([fieldName]) => {
|
|
38
|
+
return {
|
|
39
|
+
fieldName,
|
|
40
|
+
typeName,
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const template: CloudFormationTemplate = {
|
|
47
|
+
AWSTemplateFormatVersion: '2010-09-09',
|
|
48
|
+
Parameters: {
|
|
49
|
+
Environment: {
|
|
50
|
+
Default: 'Staging',
|
|
51
|
+
Type: 'String',
|
|
52
|
+
AllowedValues: ['Staging', 'Production'],
|
|
53
|
+
},
|
|
54
|
+
LambdaS3Bucket: {
|
|
55
|
+
Type: 'String',
|
|
56
|
+
},
|
|
57
|
+
LambdaS3Key: {
|
|
58
|
+
Type: 'String',
|
|
59
|
+
},
|
|
60
|
+
LambdaS3ObjectVersion: {
|
|
61
|
+
Type: 'String',
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
Resources: {
|
|
65
|
+
[AppSyncGraphQLApiLogicalId]: {
|
|
66
|
+
Type: 'AWS::AppSync::GraphQLApi',
|
|
67
|
+
Properties: {
|
|
68
|
+
AuthenticationType: 'API_KEY',
|
|
69
|
+
Name: {
|
|
70
|
+
'Fn::Join': [
|
|
71
|
+
':',
|
|
72
|
+
[{ Ref: 'AWS::StackName' }, AppSyncGraphQLApiLogicalId],
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
[AppSyncGraphQLSchemaLogicalId]: {
|
|
78
|
+
Type: 'AWS::AppSync::GraphQLSchema',
|
|
79
|
+
Properties: {
|
|
80
|
+
ApiId: { 'Fn::GetAtt': [AppSyncGraphQLApiLogicalId, 'ApiId'] },
|
|
81
|
+
Definition: sdl,
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
[AppSyncLambdaFunctionIAMRoleLogicalId]: {
|
|
85
|
+
Type: 'AWS::IAM::Role',
|
|
86
|
+
Properties: {
|
|
87
|
+
AssumeRolePolicyDocument: {
|
|
88
|
+
Version: '2012-10-17',
|
|
89
|
+
Statement: [
|
|
90
|
+
{
|
|
91
|
+
Effect: 'Allow',
|
|
92
|
+
Action: 'sts:AssumeRole',
|
|
93
|
+
Principal: {
|
|
94
|
+
Service: 'lambda.amazonaws.com',
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
ManagedPolicyArns: [
|
|
100
|
+
'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole',
|
|
101
|
+
],
|
|
102
|
+
Path: '/custom-iam/',
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
[AppSyncLambdaFunctionLogicalId]: {
|
|
106
|
+
Type: 'AWS::Lambda::Function',
|
|
107
|
+
Properties: {
|
|
108
|
+
Code: {
|
|
109
|
+
S3Bucket: { Ref: 'LambdaS3Bucket' },
|
|
110
|
+
S3Key: { Ref: 'LambdaS3Key' },
|
|
111
|
+
S3ObjectVersion: { Ref: 'LambdaS3ObjectVersion' },
|
|
112
|
+
},
|
|
113
|
+
Handler: 'index.handler',
|
|
114
|
+
Layers: [
|
|
115
|
+
{
|
|
116
|
+
'Fn::ImportValue': 'LambdaLayer-Graphql-16-6-0',
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
MemorySize: 512,
|
|
120
|
+
Role: {
|
|
121
|
+
'Fn::GetAtt': [AppSyncLambdaFunctionIAMRoleLogicalId, 'Arn'],
|
|
122
|
+
},
|
|
123
|
+
Runtime: 'nodejs18.x',
|
|
124
|
+
/**
|
|
125
|
+
* https://docs.aws.amazon.com/general/latest/gr/appsync.html
|
|
126
|
+
* Request execution time for mutations, queries, and subscriptions: 30 seconds
|
|
127
|
+
*/
|
|
128
|
+
Timeout: 29,
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
[AppSyncLambdaFunctionAppSyncDataSourceIAMRoleLogicalId]: {
|
|
132
|
+
Type: 'AWS::IAM::Role',
|
|
133
|
+
Properties: {
|
|
134
|
+
AssumeRolePolicyDocument: {
|
|
135
|
+
Version: '2012-10-17',
|
|
136
|
+
Statement: [
|
|
137
|
+
{
|
|
138
|
+
Effect: 'Allow',
|
|
139
|
+
Action: 'sts:AssumeRole',
|
|
140
|
+
Principal: {
|
|
141
|
+
Service: 'appsync.amazonaws.com',
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
},
|
|
146
|
+
ManagedPolicyArns: [
|
|
147
|
+
'arn:aws:iam::aws:policy/service-role/AWSAppSyncPushToCloudWatchLogs',
|
|
148
|
+
],
|
|
149
|
+
Path: '/custom-iam/',
|
|
150
|
+
Policies: [
|
|
151
|
+
{
|
|
152
|
+
PolicyName: 'AppSyncGraphQLApiIAMRolePolicyName',
|
|
153
|
+
PolicyDocument: {
|
|
154
|
+
Version: '2012-10-17',
|
|
155
|
+
Statement: [
|
|
156
|
+
{
|
|
157
|
+
Effect: 'Allow',
|
|
158
|
+
Action: ['lambda:InvokeFunction'],
|
|
159
|
+
Resource: [
|
|
160
|
+
{ 'Fn::GetAtt': [AppSyncLambdaFunctionLogicalId, 'Arn'] },
|
|
161
|
+
],
|
|
162
|
+
},
|
|
163
|
+
],
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
[AppSyncLambdaFunctionAppSyncDataSourceLogicalId]: {
|
|
170
|
+
Type: 'AWS::AppSync::DataSource',
|
|
171
|
+
Properties: {
|
|
172
|
+
ApiId: { 'Fn::GetAtt': [AppSyncGraphQLApiLogicalId, 'ApiId'] },
|
|
173
|
+
LambdaConfig: {
|
|
174
|
+
LambdaFunctionArn: {
|
|
175
|
+
'Fn::GetAtt': [AppSyncLambdaFunctionLogicalId, 'Arn'],
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
Name: 'AppSyncLambdaFunctionAppSyncDataSource',
|
|
179
|
+
ServiceRoleArn: {
|
|
180
|
+
'Fn::GetAtt': [
|
|
181
|
+
AppSyncLambdaFunctionAppSyncDataSourceIAMRoleLogicalId,
|
|
182
|
+
'Arn',
|
|
183
|
+
],
|
|
184
|
+
},
|
|
185
|
+
Type: 'AWS_LAMBDA',
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
Outputs: {
|
|
190
|
+
[AppSyncGraphQLApiLogicalId]: {
|
|
191
|
+
Value: {
|
|
192
|
+
'Fn::GetAtt': [AppSyncGraphQLApiLogicalId, 'GraphQLUrl'],
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
resolveMethodsEntries.forEach(({ fieldName, typeName }) => {
|
|
199
|
+
template.Resources[`${fieldName}${typeName}AppSyncResolver`] = {
|
|
200
|
+
Type: 'AWS::AppSync::Resolver',
|
|
201
|
+
DependsOn: AppSyncGraphQLSchemaLogicalId,
|
|
202
|
+
Properties: {
|
|
203
|
+
ApiId: { 'Fn::GetAtt': [AppSyncGraphQLApiLogicalId, 'ApiId'] },
|
|
204
|
+
FieldName: fieldName,
|
|
205
|
+
TypeName: typeName,
|
|
206
|
+
DataSourceName: {
|
|
207
|
+
'Fn::GetAtt': [
|
|
208
|
+
AppSyncLambdaFunctionAppSyncDataSourceLogicalId,
|
|
209
|
+
'Name',
|
|
210
|
+
],
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return template;
|
|
217
|
+
};
|
package/src/index.ts
ADDED