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

Local Mocking API doesn't honour custom resolver response template names #2380

Closed ghost closed 5 years ago

ghost commented 5 years ago

Describe the bug When custom resolver response templates have been defined with a non-standard name, even though those names are defined in the resolver CloudFormation template, these names are not used. Instead the local mocking code looks for a response template with the same name as the request template, but with .res.vtl extension.

Note that the same setup works fine non-locally after an amplify push

To Reproduce Create a custom resolver for a mutation eg. Mutation.createBodyBlock.

  1. Create a file in <api>/resolvers/Mutation.createBodyBlock.req.vtl.
  2. Create a file in <api>/resolvers/passthrough.res.vtl
  3. Create a CloudFormation template entry in stacks/CustomResources.json which specifies the above two locations for the Request and Response portions of the resolver:
    "MutationCreateBodyBlockResolver": {
      "Type": "AWS::AppSync::Resolver",
      "Properties": {
        "ApiId": {
          "Ref": "AppSyncApiId"
        },
        "DataSourceName": "BasicBlockTable",
        "TypeName": "Mutation",
        "FieldName": "createBodyBlock",
        "RequestMappingTemplateS3Location": {
          "Fn::Sub": [
            "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Mutation.createBodyBlock.req.vtl",
            {
              "S3DeploymentBucket": {
                "Ref": "S3DeploymentBucket"
              },
              "S3DeploymentRootKey": {
                "Ref": "S3DeploymentRootKey"
              }
            }
          ]
        },
        "ResponseMappingTemplateS3Location": {
          "Fn::Sub": [
            "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/passthrough.res.vtl",
            {
              "S3DeploymentBucket": {
                "Ref": "S3DeploymentBucket"
              },
              "S3DeploymentRootKey": {
                "Ref": "S3DeploymentRootKey"
              }
            }
          ]
        }
      }
    }
  4. Start local mocking: amplify mock api
  5. See error:
    Failed to start API Mock endpoint Error: Invalid config for UNIT_RESOLVER {"dataSourceName":"BasicBlockTable","typeName":"Mutation","fieldName":"createBodyBlock","requestMappingTemplateLocation":"resolvers/Mutation.createBodyBlock.req.vtl","responseMappingTemplateLocation":"resolvers/Mutation.createBodyBlock.res.vtl","kind":"UNIT"} 
    Missing mapping template resolvers/Mutation.createBodyBlock.res.vtl

Expected behavior It should use the name provided in the stack for the response template, and not assume that it's the same as the request template with a different suffix.

pspanchal commented 5 years ago

actually it does not even take the name for request template. I think it is taking the template names from fieldname parameter.

nikhname commented 5 years ago

This is fixed by https://github.com/aws-amplify/amplify-cli/pull/2355

alireza8101 commented 4 years ago

The problem still exists. Failed to start API Mock endpoint Error: Invalid config for UNIT_RESOLVER {"dataSourceName":"TodoTable","typeName":"Query","fieldName":"scanTodos","requestMappingTemplateLocation":"resolvers/Query.scanTodos.req.vtl","responseMappingTemplateLocation":"resolvers/Query.scanTodos.res.vtl","kind":"UNIT"} Missing mapping template resolvers/Query.scanTodos.req.vtl

dryhurst commented 4 years ago

running npm install -g @aws-amplify/cli to upgrade to the newest version fixed this for me. i am on windows and this patch fixed my issue: https://github.com/aws-amplify/amplify-cli/commit/87c4ad59a701995220946ad35f1491f0d4b57325

thank you team

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.