创建解析器以通过 AppSync 更新 dynamoDB 项目 - AWS CDK
Creating a resolver to update dynamoDB item through AppSync - AWS CDK
所以我创建了一个解析器来使用下面的代码在 table 中创建一个项目。
const configSettingsDS = api.addDynamoDbDataSource('configSettingsDynamoTable', configurationSettingsTable);
configSettingsDS.createResolver({
typeName:'Mutation',
fieldName: 'createConfigSettings',
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').auto(),
appsync.Values.projecting('configSettings')),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});
我似乎找不到在更新操作中复制相同内容的工具。任何帮助表示赞赏。谢谢
更新解析器的工作原理与创建解析器几乎相同。在 DynamoDB 中,两者的操作都是 PutItem
,因此应用相同的映射模板。您需要将第一个参数从 appsync.PrimaryKey.partion('id').auto()
更改为 appsync.PrimaryKey.partion('id').is('<PATH_TO_YOUR_ID>')
.
id 可以是您作为输入传递的对象的一部分。虽然我更喜欢把它分开,所以 id 不是输入对象的一部分。这是两个变体的非常基本的示例。
graphql 模式:
// Input A includes ID
input InputA {
id: ID!
name: String!
}
// Input B does not include an ID
input InputB {
name: String!
}
type Mutation {
// Id is part of input
updateA(input: InputA)
// Id needs to be provided seperately
updateB(id: ID!, InputB)
}
解析器代码:
// Configure the resolver where ID is part of the input
const resolverA = datasource.createResolver({
typeName: `Mutation`,
fieldName: `updateA`,
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').is('input.id'),
appsync.Values.projecting('input'),
),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});
// Configure the resolver where ID is provided as a separate input parameter.
const resolverB = datasource.createResolver({
typeName: `Mutation`,
fieldName: `updateB`,
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').is('id'),
appsync.Values.projecting('input'),
),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});
我刚刚不得不为我的项目解决同样的问题。以下是我如何设置它的一些片段:
部分 graphql 模式:
input Label {
id: ID!
name: String!
imageUrl: String
}
input LabelInput {
name: String!
imageUrl: String
}
type Mutation {
createLabel(input: LabelInput!): Label
updateLabel(id: ID!, input: LabelInput!): Label
}
cdk中对应的解析器:
datasource.createResolver({
typeName: `Mutation`,
fieldName: `createLabel`,
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').auto(),
appsync.Values.projecting('input'),
),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});
datasource.createResolver({
typeName: 'Mutation',
fieldName: `updateLabel`,
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').is('id'),
appsync.Values.projecting('input'),
),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});
所以我创建了一个解析器来使用下面的代码在 table 中创建一个项目。
const configSettingsDS = api.addDynamoDbDataSource('configSettingsDynamoTable', configurationSettingsTable);
configSettingsDS.createResolver({
typeName:'Mutation',
fieldName: 'createConfigSettings',
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').auto(),
appsync.Values.projecting('configSettings')),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});
我似乎找不到在更新操作中复制相同内容的工具。任何帮助表示赞赏。谢谢
更新解析器的工作原理与创建解析器几乎相同。在 DynamoDB 中,两者的操作都是 PutItem
,因此应用相同的映射模板。您需要将第一个参数从 appsync.PrimaryKey.partion('id').auto()
更改为 appsync.PrimaryKey.partion('id').is('<PATH_TO_YOUR_ID>')
.
id 可以是您作为输入传递的对象的一部分。虽然我更喜欢把它分开,所以 id 不是输入对象的一部分。这是两个变体的非常基本的示例。
graphql 模式:
// Input A includes ID
input InputA {
id: ID!
name: String!
}
// Input B does not include an ID
input InputB {
name: String!
}
type Mutation {
// Id is part of input
updateA(input: InputA)
// Id needs to be provided seperately
updateB(id: ID!, InputB)
}
解析器代码:
// Configure the resolver where ID is part of the input
const resolverA = datasource.createResolver({
typeName: `Mutation`,
fieldName: `updateA`,
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').is('input.id'),
appsync.Values.projecting('input'),
),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});
// Configure the resolver where ID is provided as a separate input parameter.
const resolverB = datasource.createResolver({
typeName: `Mutation`,
fieldName: `updateB`,
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').is('id'),
appsync.Values.projecting('input'),
),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});
我刚刚不得不为我的项目解决同样的问题。以下是我如何设置它的一些片段:
部分 graphql 模式:
input Label {
id: ID!
name: String!
imageUrl: String
}
input LabelInput {
name: String!
imageUrl: String
}
type Mutation {
createLabel(input: LabelInput!): Label
updateLabel(id: ID!, input: LabelInput!): Label
}
cdk中对应的解析器:
datasource.createResolver({
typeName: `Mutation`,
fieldName: `createLabel`,
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').auto(),
appsync.Values.projecting('input'),
),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});
datasource.createResolver({
typeName: 'Mutation',
fieldName: `updateLabel`,
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').is('id'),
appsync.Values.projecting('input'),
),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});