如何在 AwsAppSync 突变中插入具有正确@connection值的记录?



我已经在我的架构中添加了一个资源表,连接到一个植物表:

type Resource @model
{
id: ID!
name: String!
Plants: [Plant] @connection(name: "ResourcePlant")
}

冉冉放大推送,所有资源都创建正常。

现在我想添加一个资源,并将其正确链接到所有植物。

你知道我应该如何使用sintaxe来运行最近创建的突变createResource,以便将我要包含的植物上的项目添加到该资源中吗?

我试着这样运行:

mutation CreateResource {
createResource (input: {
name: "Plant",
Plants : {
items :  
{ id: "f9a0468e-da74-41d5-8287-1cb6a76b25a5" }
}
}
) {
name,
Plants {
items {
id
}
nextToken
}
} 
}

这是错误消息:

Validation error of type WrongType: argument 'input' with value
'ObjectValue{objectFields=[ObjectField{name='name',
value=StringValue{value='Plant'}}, ObjectField{name='Plants',
value=ObjectValue{objectFields=[ObjectField{name='items', value=ObjectValue{objectFields=[ObjectField{name='id', 
value=StringValue{value='f9a0468e-da74-41d5-8287-1cb6a76b25a5'}}]}}]}}]}'
contains a field not in 'CreateResourceInput': 'Plants' @ 'createResource'

你是如何定义植物的?

你检查过这个例子吗?https://aws-amplify.github.io/docs/cli-toolchain/graphql#connection

好的,经过一番头痛,我发现了我的模型中缺少的东西。到目前为止,对我来说,这已被证明是建立这种关系的最佳方式......

我已经在我的植物类型,在模式定义上,添加了一个名为plantResourceId的字段(除了用于@connection指令的字段(。我发现,按照惯例,当在"工厂"上插入/更新记录并添加我要"连接"到该工厂的资源的资源"id"字段内容时,当查询"资源"时,它将自动检索,对于每个项目 - 更好的是:从 codegen 开箱即用。

插入示例

mutation CreatePlant {
createPlant(input:{
name: "MyPlant",
plantResourceId: "id-for-connected-resource"
}) {
name,
plantResourceId
}
}

检索项目的查询示例:

query listPlantsOnResource {
listResources(filter: {
name: {
contains: "myfilter"
}
}) {
items {
id
name
Plants 
{
items {
id
name
description
}
}
}
}
}

效果很好!

感谢所有做出贡献的人!

相关内容

最新更新