在AWS AppSync解析器中添加额外的字段

时间:2019-02-12 11:54:24

标签: amazon-dynamodb graphql aws-appsync

我的架构中的用户和组之间存在多对多关系,并且在关系级别有一个额外的枚举字段。我可以将额外的关系字段添加到我的数据集中,但是出现架构验证错误:

  

“提供的键元素与架构不匹配(服务:   AmazonDynamoDBv2;状态码:400;错误代码:ValidationException;   请求ID:2615V0KG8272IHVENE3DBSBCTVVV4KQNSO5AEMVJF66Q9ASUAAJG)“

架构:

type Group {
    id: ID!
    created: AWSDateTime!
    createdById: ID!
    createdBy: User!
    title: String!
    description: String
    users(first: Int, after: String): GroupUsersConnection
}

enum GroupRoles {
    admin
    user
}

type GroupUsers {
    id: ID!
    created: AWSDateTime!
    role: GroupRoles!
    userId: ID!
    groupId: ID!
}

type GroupConnection {
    items: [Group]
    nextToken: String
}

type GroupUsersConnection {
    items: [UserWithRole]
    nextToken: String
}

type Mutation {
    createGroup(input: CreateGroupInput!): Group
    createUser(input: CreateUserInput!): User
    createGroupUsers(input: CreateGroupUsersInput!): GroupUsers
}

type Query {
    getGroup(id: ID!): Group
    listGroups(filter: TableGroupFilterInput, limit: Int, nextToken: String): GroupConnection   
    getUser(id: ID!): User
    listUsers(filter: TableUserFilterInput, limit: Int, nextToken: String): UserConnection
}

input TableGroupFilterInput {
    id: TableIDFilterInput
    created: TableStringFilterInput
    createdById: TableIDFilterInput
    title: TableStringFilterInput
    description: TableStringFilterInput
}

input TableUserFilterInput {
    id: TableIDFilterInput
    created: TableStringFilterInput
    email: TableStringFilterInput
    password: TableStringFilterInput
    name: TableStringFilterInput
    avatar: TableStringFilterInput
    isOnline: TableBooleanFilterInput
}

type User {
    id: ID!
    created: AWSDateTime!
    email: AWSEmail!
    password: String!
    name: String
    avatar: String
    isOnline: Boolean!
    groups(first: Int, after: String): UserGroupsConnection
}

type UserConnection {
    items: [User]
    nextToken: String
}

type UserGroupsConnection {
    items: [Group]
    nextToken: String
}

type UserWithRole {
    id: ID!
    created: AWSDateTime!
    email: AWSEmail!
    password: String!
    name: String
    avatar: String
    isOnline: Boolean!
    role: GroupRoles!
}

schema {
    query: Query
    mutation: Mutation
}

用于GroupUsersConnection.items的解析器,其中UserTable作为数据源:

##--------------------------
## request mapping template
##--------------------------
#if( ${ctx.source.items.isEmpty()} )
{
    "version" : "2017-02-28",
    "operation" : "Scan",
    "consistentRead": true
}
#else
  #set($userIds = [])
  #foreach($groupUser in ${ctx.source.items})    
      #set($userIdMap = {})
      $util.qr($userIdMap.put("id", $util.dynamodb.toString($groupUser.get("userId"))))
      $util.qr($userIds.add($userIdMap))
  #end

  {
      "version" : "2018-05-29",
      "operation" : "BatchGetItem",
      "tables" : {
          "UserTable": {
              "keys": $util.toJson($userIds),
              "consistentRead": true
          }
      }
  }
#end

##--------------------------
## response mapping template
##--------------------------
#if ( ! ${ctx.result.data} )
    $util.toJson([])
#else
    #foreach($user in ${ctx.result.data.UserTable})
        #set($idx = $foreach.count-1)
        #set($role = $ctx.source.items.get(0).get("role"))
        $util.qr($user.put("role", $role))
        $util.qr($usersWithRoles.add($user))        
    #end
    $util.toJson($ctx.result.data.UserTable)
#end

这是我尝试运行的查询,结果完整:

query ListGroups {
  listGroups {
    items {
      id
      title
      createdById
      createdBy {
        name
        email
      }
      users {
        items {
          id
          name
          role
        }
      }
    }
  }
}

{
  "data": {
    "listGroups": {
      "items": [
        {
          "id": "72423554-fe02-4644-92e4-4bd2a2702d85",
          "title": "My first group",
          "createdById": "e626fab4-b099-4736-91f0-dcbf5fc2e47e",
          "createdBy": {
            "name": "John Doe",
            "email": "john@doe.com"
          },
          "users": {
            "items": [
              null
            ]
          }
        }
      ]
    }
  },
  "errors": [
    {
      "path": [
        "listGroups",
        "items",
        0,
        "users",
        "items",
        0,
        "role"
      ],
      "data": null,
      "errorType": "DynamoDB:AmazonDynamoDBException",
      "errorInfo": null,
      "locations": [
        {
          "line": 105,
          "column": 11,
          "sourceName": null
        }
      ],
      "message": "The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: NB3G15C13BUMEQ3OK6VPNVA3LFVV4KQNSO5AEMVJF66Q9ASUAAJG)"
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

这似乎是一个400错误,这可能意味着您的请求映射模板结构与Amazon DynamoDB密钥结构不匹配。

请从API的“设置”页面启用CloudWatch日志(带有“所有”选项)。完成后,再次运行相同的查询。这会将评估后的RequestMapping模板记录在日志中。然后,您可以将查询内省到DynamoDB,并将其与表架构进行比较。其中一个呼叫应该不匹配。

映射模板日志中的“ fieldInError”布尔字段指示特定路径的解析是否成功,因此您可以内省失败的路径。