AWSTemplateFormatVersion: 2010-09-09 Description: AppSync setup Parameters: FunctionStack: Description: The stack name that deploys the Lambda functions to invoke. Type: String Resources: DataSourceRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Action: sts:AssumeRole Effect: Allow Principal: Service: appsync.amazonaws.com Policies: - PolicyName: LambdaInvoker PolicyDocument: Version: 2012-10-17 Statement: - Action: lambda:InvokeFunction Effect: Allow Resource: - Fn::Join: - '' - - 'arn:aws:lambda:' - !Ref AWS::Region - ':' - !Ref AWS::AccountId - ':function:' - Fn::ImportValue: !Sub '${FunctionStack}-functions-SimilarProductRecommendation' - Fn::Join: - '' - - 'arn:aws:lambda:' - !Ref AWS::Region - ':' - !Ref AWS::AccountId - ':function:' - Fn::ImportValue: !Sub '${FunctionStack}-functions-SimilarProductRecommendation' - ':*' RoleName: !Sub '${AWS::StackName}-lambda-role' GqlApi: Type: AWS::AppSync::GraphQLApi Properties: AuthenticationType: AWS_IAM Name: !Ref AWS::StackName GqlSchema: Type: AWS::AppSync::GraphQLSchema Properties: ApiId: !GetAtt GqlApi.ApiId Definition: | type Mutation { } type Query { getSimilarProductRecommendation(context: similarProductContext, productId: String, color: String, limit: Int, referer: String, abTestName: String, abTestVariant: String, cognitoIdentityId__: String): SimilarProductRecommendation } input PriceInput { now: String onSale: Boolean was: String } type Price { now: String! onSale: Boolean! was: String! } input RecommendationInput { brandName: String numReviews: Int price: priceInput productId: String productImageUrl: String productName: String productUrl: String rating: Float } type Recommendation { brandName: String! numReviews: Int price: price! productId: String! productImageUrl: String! productName: String! productUrl: String! rating: Float } input SimilarProductRecommendationInput { recommendations: [recommendationInput] } type SimilarProductRecommendation { recommendations: [recommendation]! } enum SimilarProductContext { PDP } schema { query: Query mutation: Mutation } DependsOn: - GqlApi SimilarProductRecommendationDataSource: Type: AWS::AppSync::DataSource Properties: ApiId: !GetAtt GqlApi.ApiId LambdaConfig: LambdaFunctionArn: Fn::Join: - '' - - 'arn:aws:lambda:' - !Ref AWS::Region - ':' - !Ref AWS::AccountId - ':function:' - Fn::ImportValue: !Sub '${FunctionStack}-functions-SimilarProductRecommendation' Name: similar_product_recommendation_lambda ServiceRoleArn: !GetAtt DataSourceRole.Arn Type: AWS_LAMBDA DependsOn: - DataSourceRole - GqlApi GetSimilarProductRecommendationResolver: Type: AWS::AppSync::Resolver Properties: ApiId: !GetAtt GqlApi.ApiId FieldName: getSimilarProductRecommendation TypeName: Query DataSourceName: !GetAtt SimilarProductRecommendationDataSource.Name RequestMappingTemplate: | #set( $cognitoIdentityId = $context.identity.cognitoIdentityId ) #if($context.identity.userArn.matches('arn:aws:iam::\d+:user/.*')) #if($context.args.cognitoIdentityId__) #set( $cognitoIdentityId = $context.args.cognitoIdentityId__ ) #elseif( $context.source.cognitoIdentityId__ ) #set( $cognitoIdentityId = $context.source.cognitoIdentityId__ ) #end #end #set( $unamepwd = "$util.urlEncode($cognitoIdentityId):" ) { "version" : "2017-02-28", "operation": "Invoke", "payload": { "source": "AppSync", "httpMethod": "get", "headers": { "Authorization": "Basic $util.base64Encode($unamepwd)", "Referer": "$util.escapeJavaScript($context.args.referer)" }, "pathParameters": { "context": "$util.escapeJavaScript($context.args.context)", "productId": "$util.escapeJavaScript($context.args.productId)" }, "queryStringParameters": { "abTestName": "$util.escapeJavaScript($context.args.abTestName)", "abTestVariant": "$util.escapeJavaScript($context.args.abTestVariant)", "color": "$util.escapeJavaScript($context.args.color)", "limit": "$util.escapeJavaScript($context.args.limit)" } } } ResponseMappingTemplate: | #if($context.result.statusCode >= 200 && $context.result.statusCode <= 299) #set( $result = $util.parseJson($context.result.body) ) #if($context.args.cognitoIdentityId__) #set( $result.cognitoIdentityId__ = $context.args.cognitoIdentityId__ ) #elseif($context.source.cognitoIdentityId__) #set( $result.cognitoIdentityId__ = $context.source.cognitoIdentityId__ ) #end $util.toJson($result) #else $utils.error($util.parseJson($context.result.body).message, $context.result.statusCode.toString(), $util.parseJson($context.result.body)) #end DependsOn: - GqlApi - GqlSchema - SimilarProductRecommendationDataSource Outputs: ApiEndpoint: Description: The endpoint for the AppSync API. Value: !GetAtt GqlApi.GraphQLUrl