Amazon web services 在aws appsync解析器中添加额外字段

Amazon web services 在aws appsync解析器中添加额外字段,amazon-web-services,amazon-dynamodb,graphql,aws-appsync,Amazon Web Services,Amazon Dynamodb,Graphql,Aws Appsync,在我的模式中,用户和组之间有一个多对多关系,关系级别有一个额外的枚举字段。我可以将额外的关系字段添加到我的数据集中,但我得到架构验证错误: “提供的键元素与架构不匹配(服务: AmazonDynamoDBv2;状态代码:400;错误代码:ValidationException; 请求ID:2615V0KG8272IHVENE3DBSBCTVV4KQNSO5AEMVJF66Q9ASUAJG)” 模式: GroupUsersConnection.items的解析器,将UserTable作为数据源:

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

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

模式:

GroupUsersConnection.items
的解析器,将
UserTable
作为数据源:

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


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

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


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

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

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

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

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
}
##--------------------------
## 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)"
    }
  ]
}