1+ from pathlib import Path
12from typing import Dict , Optional
23
3- from aws_cdk import CfnOutput
4+ from aws_cdk import CfnOutput , Duration , Expiration
45from aws_cdk import aws_apigateway as apigwv1
56from aws_cdk import aws_apigatewayv2_alpha as apigwv2
67from aws_cdk import aws_apigatewayv2_authorizers_alpha as apigwv2authorizers
78from aws_cdk import aws_apigatewayv2_integrations_alpha as apigwv2integrations
89from aws_cdk import aws_ec2 as ec2
910from aws_cdk import aws_elasticloadbalancingv2 as elbv2
1011from aws_cdk import aws_elasticloadbalancingv2_targets as targets
12+ from aws_cdk import aws_appsync_alpha as appsync
13+ from aws_cdk import aws_iam
1114from aws_cdk .aws_lambda import Function , FunctionUrlAuthType
1215
1316from tests .e2e .utils .infrastructure import BaseInfrastructure
@@ -17,10 +20,11 @@ class EventHandlerStack(BaseInfrastructure):
1720 def create_resources (self ):
1821 functions = self .create_lambda_functions ()
1922
20- self ._create_alb (function = functions ["AlbHandler" ])
21- self ._create_api_gateway_rest (function = functions ["ApiGatewayRestHandler" ])
22- self ._create_api_gateway_http (function = functions ["ApiGatewayHttpHandler" ])
23- self ._create_lambda_function_url (function = functions ["LambdaFunctionUrlHandler" ])
23+ # self._create_alb(function=functions["AlbHandler"])
24+ # self._create_api_gateway_rest(function=functions["ApiGatewayRestHandler"])
25+ # self._create_api_gateway_http(function=functions["ApiGatewayHttpHandler"])
26+ # self._create_lambda_function_url(function=functions["LambdaFunctionUrlHandler"])
27+ self ._create_appsync_endpoint (function = functions ["AppsyncResolverHandler" ])
2428
2529 def _create_alb (self , function : Function ):
2630 vpc = ec2 .Vpc .from_lookup (
@@ -84,3 +88,40 @@ def _create_lambda_function_url(self, function: Function):
8488 # Maintenance: move auth to IAM when we create sigv4 builders
8589 function_url = function .add_function_url (auth_type = FunctionUrlAuthType .AWS_IAM )
8690 CfnOutput (self .stack , "LambdaFunctionUrl" , value = function_url .url )
91+
92+ def _create_appsync_endpoint (self , function : Function ):
93+ api = appsync .GraphqlApi (
94+ self .stack ,
95+ "Api" ,
96+ name = "e2e-tests" ,
97+ schema = appsync .SchemaFile .from_asset (str (Path (self .feature_path , "files/schema.graphql" ))),
98+ authorization_config = appsync .AuthorizationConfig (
99+ default_authorization = appsync .AuthorizationMode (
100+ authorization_type = appsync .AuthorizationType .API_KEY ,
101+ api_key_config = appsync .ApiKeyConfig (
102+ description = "public key for getting data" ,
103+ expires = Expiration .after (Duration .hours (25 )),
104+ name = "API Token" ,
105+ ),
106+ )
107+ ),
108+ xray_enabled = False ,
109+ )
110+ lambda_datasource = api .add_lambda_data_source ("DataSource" , lambda_function = function )
111+
112+ lambda_datasource .create_resolver (
113+ "QueryGetAllPostsResolver" ,
114+ type_name = "Query" ,
115+ field_name = "allPosts" ,
116+ )
117+ lambda_datasource .create_resolver (
118+ "QueryGetPostResolver" ,
119+ type_name = "Query" ,
120+ field_name = "getPost" ,
121+ )
122+ lambda_datasource .create_resolver (
123+ "QueryGetPostRelatedResolver" , type_name = "Post" , field_name = "relatedPosts" , max_batch_size = 10
124+ )
125+
126+ CfnOutput (self .stack , "GraphQLHTTPUrl" , value = api .graphql_url )
127+ CfnOutput (self .stack , "GraphQLAPIKey" , value = api .api_key )
0 commit comments