2
votes

I'm currently using AppSync to query against a GSI. I've been able to successfully use this block of code in a Pipeline Resolver function, but I don't know why it is not working when I try to use it in a traditional resolver.

I'm currently getting a mapping template error:

{
  "data": {
    "units": null
  },
  "errors": [
    {
      "path": [
        "units"
      ],
      "data": null,
      "errorType": "MappingTemplate",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Value for field '$[version]' not found."
    }
  ]
}

I tried searching in the AWS docs but adding "version" to the GraphQL type didn't work.

I also tried this (even though I'm not using S3) AppSync S3Object retrieval And the docs: https://docs.aws.amazon.com/appsync/latest/devguide/troubleshooting-and-common-mistakes.html#mapping-template-errors

Here's the request mapping template:

#set($arg=$context.args)

    {
    "operation": "Query",
    "index" : "userPK-userSK-index",
    "query": {
        "expression": "userPK = :pk and begins_with(userSK, :sk)",
        "expressionValues": {
            ":pk": {"S": "tenant:${arg.tenantId}" },
            ":sk": {"S": "school-year:${arg.schoolYear}:grades:${arg.gradeId}:subject:${arg.subjectId}:unit:"}
        }
    }
}

Here's the response mapping template:

$util.toJson($ctx.result.items)

Here is a snippet of the executed GraphQL:

query GetUnits{
  units(tenantId: "5fc30406-346c-42e2-8083-fda33ab6000a"
schoolYear: "2019-2020"
    gradeId: "c737e341-a0cb-4a16-95de-f3a092049e74"
subjectId: "d0306e25-422d-4628-8fcc-c354b67c932a") {
  id
  indicator {
    id,
    description
  }
  competency {
    id,
    description,
    name
  }
  description,
  name
}


}

Here is a snippet of the GraphQL schema:


type Unit {
  id: ID!
  competency: Competency
  indicator: Indicator
  name: String!
  description: String
  version: Int
}

type Competency {
  id: ID
  # grade: Grade
  # subject: Subject
  # schoolYear: String
  name: String
  description: String
}

type Indicator { 
  id: ID!
  description: String
}
type Query {
  units(
    tenantId: String!
    schoolYear: String!
    gradeId: String!
    subjectId: String!
  ): [Unit]

Here's a data example from the DynamoDB table: enter image description here

Here's a screenshot from a successful query in the Console: enter image description here

Note: I have created a GSI that maps the userPK and userSK as partition key and sort key respectively. I'm querying that Secondary Index. I've been able to query this successfully using the console.

1

1 Answers

3
votes

The error shows you forgot version parameter. This is the query template (docs):

{
    "version" : "2017-02-28",
    "operation" : "Query",
    "query" : {
        "expression" : "some expression",
        "expressionNames" : {
            "#foo" : "foo"
        },
        "expressionValues" : {
            ":bar" : ... typed value
        }
    }
    "index" : "fooIndex",
    "nextToken" : "a pagination token",
    "limit" : 10,
    "scanIndexForward" : true,
    "consistentRead" : false,
    "select" : "ALL_ATTRIBUTES",
    "filter" : {
        ...
    }
}

and the version is required:

version

The template definition version. 2017-02-28 and 2018-05-29 are currently supported. This value is required.