【问题标题】:Pagination in apollo-graphql serverapollo-graphql 服务器中的分页
【发布时间】:2017-11-07 02:06:10
【问题描述】:

我想在 Apollo graphql 服务器中实现基于光标的分页。我已经准备了具有分页要求的模式。但我被困在解析器方面。 这是我的架构

const typeDefinitions = `
input CreateDeveloperInput {
  # An arbitrary string value with no semantic meaning. Will be included in the
  # payload verbatim. May be used to track mutations by the client.
  clientMutationId: String
  developer: DeveloperInput!
}

type CreateDeveloperPayload {
  clientMutationId: String
  developerEdge(orderBy: DevelopersOrderBy = PRIMARY_KEY_ASC): DevelopersEdge
  query: Query
}
input DeleteDeveloperByIdInput {
  # An arbitrary string value with no semantic meaning. Will be included in the
  # payload verbatim. May be used to track mutations by the client.
  clientMutationId: String
  id: Int!
}

input DeleteDeveloperInput {
  clientMutationId: String
  nodeId: ID!
}

type DeleteDeveloperPayload {
  clientMutationId: String
  developer: Developer
  deletedDeveloperId: ID

  # Our root query field type. Allows us to run any query from our mutation payload.
  query: Query
}

type Developer implements Node {
  nodeId: ID!
  id: Int!
  name: String!
  place: String
  salary: Int
  joiningDate: String
}

input DeveloperCondition {
  id: Int
  name: String
  place: String
  salary: Int
  joiningDate: String
}

input DeveloperInput {
  id: Int
  name: String!
  place: String
  salary: Int
  joiningDate: String
}

input DeveloperPatch {
  id: Int
  name: String
  place: String
  salary: Int
  joiningDate: String
}

type DevelopersConnection {
  # Information to aid in pagination.
  pageInfo: PageInfo!
  totalCount: Int
  edges: [DevelopersEdge]
  nodes: [Developer!]
}

type DevelopersEdge {
  # A cursor for use in pagination.
  cursor: String
  node: Developer!
}

enum DevelopersOrderBy {
  PRIMARY_KEY_ASC
  PRIMARY_KEY_DESC
  NATURAL
  ID_ASC
  ID_DESC
  NAME_ASC
  NAME_DESC
  PLACE_ASC
  PLACE_DESC
  SALARY_ASC
  SALARY_DESC
  JOINING_DATE_ASC
  JOINING_DATE_DESC
}

# The root mutation type which contains root level fields which mutate data.


interface Node {
  # A globally unique identifier. Can be used in various places throughout the system to identify this single value.
  nodeId: ID!
}

# Information about pagination in a connection.
type PageInfo {
  # When paginating forwards, are there more items?
  hasNextPage: Boolean!

  # When paginating backwards, are there more items?
  hasPreviousPage: Boolean!

  # When paginating backwards, the cursor to continue.
  startCursor: String

  # When paginating forwards, the cursor to continue.
  endCursor: String
}

# The root query type which gives access points into the data universe.
type Query implements Node {
   allDevelopers(

    # Read all values in the set before (above) this cursor.
    before: String,

    # Read all values in the set after (below) this cursor.
    after: String, first: Int, last: Int, offset: Int,

    # A condition to be used in determining which values should be returned by the collection.
    condition: DeveloperCondition): DevelopersConnection

  # Exposes the root query type nested one level down. This is helpful for Relay 1
  # which can only query top level fields if they are in a particular form.
  
  nodeId: ID!
}
schema {
query: Query
}

`;

export default [typeDefinitions];

是否可以在解析器中解析?如果是的话,谁能告诉我如何实现它

【问题讨论】:

  • 实现将取决于您使用的数据库。

标签: graphql graphql-js apollo-server


【解决方案1】:

如果您将 Mongo 和 Mongoose 与 Apollo Express GraphQL 一起使用,我发现了三种分页方法:

  1. 您可以在架构上创建cursor 字段并使用分页逻辑实现解析器,但如果您的架构包含联合、接口和不同的嵌套对象,我不推荐使用此方法类型,但如果你想自己实现它,这里是reference

  2. 您可以使用module,它将嵌套您的 Mongoose 模型并为您提供分页界面,但这需要对您的架构和解析器进行一些更改,比第三种方法更多的工作

    李>
  3. 您可以使用 Mongo / Mongoose 中的 sortskiplimit,但如果您自己这样做,您可以找到一些 issues,例如,如果文档按日期键排序它可以有相同日期的不同文档它可以复制文档并弄乱你的分页,所以我建议你在你的 Mongoose 架构上安装一个分页插件,并像 Mongoose 的任何其他方法一样使用它

【讨论】:

    猜你喜欢
    • 2020-12-25
    • 1970-01-01
    • 2020-12-21
    • 2021-10-04
    • 2021-12-01
    • 2019-11-15
    • 2019-02-03
    • 2018-06-23
    • 2020-09-11
    相关资源
    最近更新 更多