【问题标题】:Modeling Relay Cursor Connections模拟继电器光标连接
【发布时间】:2017-10-28 07:26:36
【问题描述】:

我正在构建一个graphql 应用程序,其中User 可以有一堆Entries。这是一个 n 到 m 的关系,中间表/边保存有关该关系的附加信息。 我的 graphql 架构看起来像这样:

type User {
    id: ID!,
    entries(…): [UserEntry]
}

type UserEntry {
    id: ID!,
    user: User,
    entry: Entry,
    someOtherAttribute: String,
    yetAnotherAttribute: String
}

type Entry {...}

type Query {
  me: User!
  userEntry(userEntryId: ID!): UserEntry!
}

我想在 entries 字段之后添加光标样式分页到 Relay Cursor Connections Specification 之后。 所以我猜UserEntry 会变成这样:

type UserEntryEdge {
    node: Entry,
    cursor: String,
    someOtherAttribute: String,
    yetAnotherEdgeAttribute: String
}

但我仍然希望能够直接查询UserEntry/UserEntryEdge,在这种情况下,例如cursor 字段将是无关紧要的。

设计我的 graphql 架构以便能够直接查询边缘数据的最佳方式是什么?

(仅供参考:我在服务器和客户端都使用 nodejs 和 apollo 框架套件)

【问题讨论】:

  • 所以要直接查询Entry?您在直接下的意思是什么?您能否提供您想要使用的示例查询?
  • 我可以在这里查询Entry。我希望能够仍然查询 UserEntry !我添加了当前查询的定义。谢谢

标签: graphql relay


【解决方案1】:

如果您仍然需要直接查询UserEntry,那么我想您应该将其作为单独的类型保留在您的架构中,而不是将其转换为Edge 类型。

所以只保留UserEntryUserEntryEdge

生成的架构可能如下所示:

type User {
    id: ID!,
    entries(…): [UserEntryConnection]
}

type UserEntryConnection {
  edges: [UserEntryEdge]
  nodes: [Entry] # shortcut (GitHub does like that)
  pageInfo: PageInfo!
}

type UserEntryEdge {
    node: Entry,
    cursor: String,
    info: UserEntry # To not duplicate attributes, you can use UserEntry type here
}

type UserEntry {
    id: ID!,
    user: User,
    entry: Foo,
    someOtherAttribute: String,
    yetAnotherAttribute: String
}

type Entry {...}

type Query {
  me: User!
  userEntry(userEntryId: ID!): UserEntry! # keep userEntry field as is
}

【讨论】:

  • 我曾想过这样做,但我感觉有点尴尬Entry 可以从UserEntryEdgenode 字段和entry 的字段entry 访问UserEntry。也许我应该在 api 中公开 EntryUserEntry 的合并版本...谢谢您的帮助
【解决方案2】:

您实际上是在像这样对您的架构进行建模

[User] hasAndBelongsToMany [Entry]

但你可以这样想

[User] hasMany [UserEntry] hasOne [Entry]
    and
[Entry] hasMany [UserEntry] hasOne [User]

那么,回到你的 GraphQL Schema:

type User {
    id: ID!,
    userEntriesConnection(…): UserEntriesConnection!
}

type UserEntriesConnection {
    edges: [UserEntryEdge]!,
    pageInfo: ...
}

type UserEntryEdge {
    cursor: String!,
    node: UserEntry,
}

type UserEntry {
    id: ID!,
    user: User,
    entry: Entry,
    someOtherAttribute: String,
    yetAnotherAttribute: String
}

type Entry { ... }

type Query {
  me: User!
  userEntry(userEntryId: ID!): UserEntry!
}

这符合您的需要吗?查询会更详细,因为更深入,但也更完整。

【讨论】:

    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 2018-02-04
    • 2015-12-29
    • 2018-08-25
    • 2018-04-14
    • 1970-01-01
    相关资源
    最近更新 更多