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

How can I insert records in AwsAppSync mutation with proper @connection values?

我已将资源 table 添加到我的模式,连接到 Plants table:

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

运行放大推送,所有资源都创建正常

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

你知道我应该如何使用语法来 运行 最近创建的 mutation createResource 以便添加我想包含在该资源中的 Plant 上的项目吗?

我试过这样运行:

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

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

我在 Plant 类型的架构定义中添加了一个名为 plantResourceId 的字段(而不是用于 @connection 指令的字段)。我发现的是,按照惯例,当 inserting/updating "Plant" 上的记录并将我想要 "connect" 的资源的资源 "id" 字段内容添加到该工厂时,当查询 "Resources" 时,它会自动检索,对于每个项目 - 更好的是:开箱即用,来自 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
        }
      }
    }
  }
}

效果很好!

感谢所有贡献者!