【问题标题】:Can I use edge type without connection types in GraphQL?我可以在 GraphQL 中使用没有连接类型的边缘类型吗?
【发布时间】:2017-03-07 14:56:59
【问题描述】:

我需要边缘类型来在两个节点之间添加额外的字段。但我不需要分页连接。

我可以在 GraphQL 中使用没有连接的边缘吗?这样做的最佳做法是什么?

【问题讨论】:

    标签: facebook-graph-api graphql graphql-js


    【解决方案1】:

    边缘类型没有什么特别之处——只需定义一个新的对象类型并直接引用它而不是其他类型。就像您可能在 SQL 中定义连接表一样。因此,假设您从(使用 GraphQL 模式语言)开始:

    type User {
      bestFriend: User
    }
    

    然后你想添加一些关于友谊的东西,你可以为此创建一个类型:

    type User {
      bestFriend: Friendship
    }
    
    type Friendship {
      friend: User
      startTime: Date
      commonPhotos: [Photo]
    }
    

    现在Friendship 类型就像一条边一样工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-22
      • 2022-06-21
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多