Search code examples
aws-amplifyaws-appsyncaws-amplify-cli

Receiving "Unsupported element '$[operation]'." when running a mutation


I am having an issue running a mutation that was generated by the Amplify CLI.
I'm on node v14.18.1, amplify CLI 7.6.2.

I just recently migrated to the GraphQL Transformer v2.

Here is my model:

type User
  @model
  @auth(
    rules: [
      { allow: groups, groups: ["admin"] }
      { allow: owner, ownerField: "id", operations: [read] }
    ]
  ) {
  id: ID! 
  first_name: String
  last_name: String
  email: String!
  customer: Customer @hasOne(fields: ["userCustomerId"])
  userCustomerId: ID! @index(name: "usersByCreatedAt", queryField: "usersByCreatedAt" sortKeyFields: ["createdAt"])
  createdAt: String!
  isAdmin: Boolean
}

The mutation I'm calling from within AppSync:

mutation UpdateUser {
  updateUser(input: {id: "asdfasdfasdf", isAdmin: true, last_name: "Franklin", first_name: "Tim", email: "[email protected]", userCustomerId: "my_customer"}) {
    id
  }
}

Error:

{
  "data": {
    "updateUser": null
  },
  "errors": [
    {
      "path": [
        "updateUser"
      ],
      "data": null,
      "errorType": "MappingTemplate",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Unsupported element '$[operation]'."
    }
  ]
}

Has anyone ran into this error, and how did you resolve it? Thank you!


Solution

  • Check if there are any .vtl files in project_dir/amplify/backend/api/api_name/resolvers. If there are and you're not sure why/don't recognize them, back up and delete those files, deploy your local backend with amplify push, and see if the mutation succeeds.

    I was getting the exact same error during a delete mutation and this resolved it for me. The Amplify CLI auto-generates templates for the API service, but overrides them with any it finds in that directory. Just make sure to back up all of those files before deleting them just in case..