serverless-appsync-plugin icon indicating copy to clipboard operation
serverless-appsync-plugin copied to clipboard

Substitutions not working

Open AbhishekDesai99 opened this issue 2 years ago • 3 comments

This is my runtime configurations, Node - 12.18.3 Serverless - 2.66.2

Tested it with the latest plugin version - 1.12.0, but it is not replacing variable values on the VTL templates Here is my code,

serverless.yml

service: temp
frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221
  region: 'us-west-2'

custom:
  appSync:
    - ${file(appsync.yml)}
    

plugins:
  - serverless-appsync-plugin

resources:
  Resources:
    AppSyncLoggingServiceRole:
      Type: "AWS::IAM::Role"
      Properties:
        RoleName: "appsync-testing"
        AssumeRolePolicyDocument:
          Version: "2012-10-17"
          Statement:
            - Effect: "Allow"
              Principal:
                Service:
                  - "appsync.amazonaws.com"
              Action:
                - "sts:AssumeRole"
        Policies:
          - PolicyName: "appsync-testing"
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Effect: "Allow"
                  Action: ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"]
                  Resource: "*"

appsync.yml

name: appsync-testing
schema: schema.api.graphql
authenticationType: API_KEY
apiKeys:
  - name: 'GuestKey'
    description: 'API key protection for open appsync endpoints'
logConfig:
  loggingRoleArn: { Fn::GetAtt: [AppSyncLoggingServiceRole, Arn] }
  level: ALL
  excludeVerboseContent: false
mappingTemplatesLocation: mapping-templates
mappingTemplates:
  - type: Mutation
    field: testing
    dataSource: none
  - type: Query
    field: testing
    dataSource: none
    substitutions:
      myVar2: "Hello world 2"
substitutions:
  myVar: "Hello world"
dataSources:
  - type: NONE
    name: none

I tried with both the approaches, a resolver level substitution, and a global level, but none of them worked for me. Can anyone help me to find out what is going wrong?

AbhishekDesai99 avatar Nov 18 '21 10:11 AbhishekDesai99

It should work. Can you also share your VTL template where you are using substitution?

bboure avatar Dec 01 '21 21:12 bboure

@bboure yes, here it my request-mapping-template for mutation testing

$util.qr($ctx.stash.put("var1", ${myVar}))
$util.qr($ctx.stash.put("var2", "var replaced"))
{
    "version": "2018-05-29",
    "payload": $util.toJson($myVar)
}

this is the response-mapping-template for the same

$util.toJson($myvar)

AbhishekDesai99 avatar Dec 02 '21 08:12 AbhishekDesai99

@bboure, this is the log details for the same

{
    "logType": "RequestMapping",
    "path": [
        "testing"
    ],
    "fieldName": "testing",
    "resolverArn": "arn:aws:appsync:us-west-2:<MyAccount>:apis/7vhyizb7j5hczc6wagraxzfowm/types/Mutation/resolvers/testing",
    "requestId": "ccf4a5da-755c-44f9-a0e8-c46c6c372b27",
    "context": {
        "arguments": {},
        "stash": {
            "var2": "var replaced"
        },
        "outErrors": []
    },
    "fieldInError": false,
    "errors": [],
    "parentType": "Mutation",
    "graphQLAPIId": "7vhyizb7j5hczc6wagraxzfowm",
    "transformedTemplate": "\n\n{\n    \"version\": \"2018-05-29\",\n    \"payload\": null\n}"
}
{
    "logType": "ResponseMapping",
    "path": [
        "testing"
    ],
    "fieldName": "testing",
    "resolverArn": "arn:aws:appsync:us-west-2:<MyAccount>:apis/7vhyizb7j5hczc6wagraxzfowm/types/Mutation/resolvers/testing",
    "requestId": "ccf4a5da-755c-44f9-a0e8-c46c6c372b27",
    "context": {
        "arguments": {},
        "stash": {
            "var2": "var replaced"
        },
        "outErrors": []
    },
    "fieldInError": false,
    "errors": [],
    "parentType": "Mutation",
    "graphQLAPIId": "7vhyizb7j5hczc6wagraxzfowm",
    "transformedTemplate": "null"
}

AbhishekDesai99 avatar Dec 02 '21 08:12 AbhishekDesai99