aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

Placing function resolver in functions folder causes error while mocking API #4557

Closed buede closed 4 years ago

buede commented 4 years ago

Describe the bug With the API (AppSync - GraphQL), placing function resolvers in the functions folder causes the command amplify mock to throw an error about missing template mapping. When using amplify push everything works, so it only happens while trying to mock the API.

Amplify CLI Version 4.18.1

To Reproduce

  1. Create a function resolver inside the directory amplify/backend/api/<your API name>/functions
  2. Add the resource reference to CustomResources.json
  3. Run amplify mock

Expected behavior The command will throw an error (without exiting) Failed to start API Mock endpoint Error: Missing mapping template functions/Function.testFunction.req.vtl

Desktop (please complete the following information):

Additional context The folder pipelineFunctions can be used instead but the file amplify-util-mock/src/api/resolver-overrides.ts suggests that the folder functions is indeed a valid place for function vtl files.

SwaySway commented 4 years ago

Hello @buede Could you provide how you setup the pipeline resolver in your schema?

buede commented 4 years ago

Here's an example of how it was. Again it worked with amplify push but not with amplify mock (and as soon as I changed the folder where the functions were located from functions to pipelineFunctions it worked).

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "An auto-generated nested stack.",
  "Metadata": {},
  "Parameters": {
    "AppSyncApiId": {
      "Type": "String",
      "Description": "The id of the AppSync API associated with this project."
    },
    "AppSyncApiName": {
      "Type": "String",
      "Description": "The name of the AppSync API",
      "Default": "AppSyncSimpleTransform"
    },
    "env": {
      "Type": "String",
      "Description": "The environment name. e.g. Dev, Test, or Production",
      "Default": "NONE"
    },
    "S3DeploymentBucket": {
      "Type": "String",
      "Description": "The S3 bucket containing all deployment assets for the project."
    },
    "S3DeploymentRootKey": {
      "Type": "String",
      "Description": "An S3 key relative to the S3DeploymentBucket that points to the root\nof the deployment directory."
    }
  },
  "Resources": {
    "TestFunction": {
      "Type": "AWS::AppSync::FunctionConfiguration",
      "Properties": {
        "Name": "TestFunction",
        "ApiId": {
          "Ref": "AppSyncApiId"
        },
        "DataSourceName": {
          "Fn::ImportValue": {
            "Fn::Join": [
              ":",
              [
                {
                  "Ref": "AppSyncApiId"
                },
                "GetAtt",
                "TestDataSource",
                "Name"
              ]
            ]
          }
        },
        "FunctionVersion": "2018-05-29",
        "RequestMappingTemplateS3Location": {
          "Fn::Sub": [
            "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/functions/Function.testFunction.req.vtl",
            {
              "S3DeploymentBucket": {
                "Ref": "S3DeploymentBucket"
              },
              "S3DeploymentRootKey": {
                "Ref": "S3DeploymentRootKey"
              }
            }
          ]
        },
        "ResponseMappingTemplateS3Location": {
          "Fn::Sub": [
            "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/functions/Function.testFunction.res.vtl",
            {
              "S3DeploymentBucket": {
                "Ref": "S3DeploymentBucket"
              },
              "S3DeploymentRootKey": {
                "Ref": "S3DeploymentRootKey"
              }
            }
          ]
        }
      }
    },
    "TestResolver": {
      "Type": "AWS::AppSync::Resolver",
      "Properties": {
        "ApiId": {
          "Ref": "AppSyncApiId"
        },
        "TypeName": "Mutation",
        "FieldName": "testMutation",
        "Kind": "PIPELINE",
        "PipelineConfig": {
          "Functions": [
            {
              "Fn::GetAtt": [
                "TestFunction",
                "FunctionId"
              ]
            }
          ]
        },
        "RequestMappingTemplateS3Location": {
          "Fn::Sub": [
            "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Mutation.testMutation.req.vtl",
            {
              "S3DeploymentBucket": {
                "Ref": "S3DeploymentBucket"
              },
              "S3DeploymentRootKey": {
                "Ref": "S3DeploymentRootKey"
              }
            }
          ]
        },
        "ResponseMappingTemplateS3Location": {
          "Fn::Sub": [
            "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Mutation.testMutation.res.vtl",
            {
              "S3DeploymentBucket": {
                "Ref": "S3DeploymentBucket"
              },
              "S3DeploymentRootKey": {
                "Ref": "S3DeploymentRootKey"
              }
            }
          ]
        }
      }
    }
  },
  "Conditions": {
    "HasEnvironmentParameter": {
      "Fn::Not": [
        {
          "Fn::Equals": [
            {
              "Ref": "env"
            },
            "NONE"
          ]
        }
      ]
    },
    "AlwaysFalse": {
      "Fn::Equals": [
        "true",
        "false"
      ]
    }
  },
  "Outputs": {
    "EmptyOutput": {
      "Description": "An empty output. You may delete this if you have at least one resource above.",
      "Value": ""
    }
  }
}
jhockett commented 4 years ago

Hi @buede, sorry for the lack of updates. I wasn't able to reproduce this in 4.27.2. Placing a custom function resolver in the functions folder didn't work on an amplify push.

We recommend in our documentation that your custom resolvers go into the amplify/backend/api/<api name>/resolvers folder.

Closing this issue, but feel free to comment below if you have questions.

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.