【问题标题】:In graphql schema, how can I create a parent/child relationship of same model?在 graphql 模式中,如何创建同一模型的父/子关系?
【发布时间】:2019-08-04 07:51:57
【问题描述】:

我需要创建一个可搜索的列表表,其中一些记录的类型为ORGANIZATIONRESOURCE。关系是一对多的。因此,一个组织可以有许多资源。如何在一个模型下创建这种关系?

使用 AWS Amplify GraphQL API...

像这样? schema.graphql

enum ListingType {
  ORGANIZATION
  RESOURCE
}
type Listing @model {
  id: ID!
  title: String!
  type: ListingType!
  orginzation: Listing
}

然而,在 Mutations 中,我在创建第一个资源时无法引用父组织:

【问题讨论】:

  • 你还需要在组织字段上使用@connection 指令吗?
  • 嗯,好点子。对于一对一,@connection 将引用组织。我试试看……
  • @DanielRearden,做到了。谢谢!提交您的答案,我会将其标记为答案。

标签: graphql aws-amplify


【解决方案1】:

您需要为任何属于关系的字段添加@connection 指令,如docs 中所述。在这种情况下,这样的事情应该可以工作:

type Listing @model {
  id: ID!
  title: String!
  type: ListingType!
  organization: Listing @connection
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2018-06-24
    • 2021-12-26
    相关资源
    最近更新 更多