简体   繁体   English

Apollo / GraphQL的光标分页一直给我错误

[英]Cursor Pagination with Apollo/GraphQL keeps giving me error

I'm trying to implement cursor pagination and followed the examples in the doc but I keep getting an error saying 'Cannot query field "cursor" on type "Query"' . 我正在尝试实现游标分页并按照文档中的示例进行操作,但我不断收到错误消息“无法查询字段”游标“类型”查询“'

I'm aware that the "cursor" field doesn't actually exist on the Accounts schema...but from what I'm reading from the docs.. you have to include it somewhere in the gql`` query. 我知道“游标”字段实际上并不存在于帐户架构中......但是我从文档中读取的内容......你必须将它包含在gql``查询的某个地方。 Furthermore, not sure if I'm missing anything but I'm a bit confused of how to structure my query to allow cursor pagination. 此外,不确定我是否遗漏了任何东西,但我对如何构造我的查询以允许光标分页感到困惑。

Original Query: (running this gives me no error) 原始查询:(运行这个没有错误)

const AccountsQuery = gql`
  query {
    accounts {
      accountName
      accountId
    }
  }
`;

New Query: (this gives "cannot find cursor field on accounts" error) 新查询:(这给出“无法在帐户上找到游标字段”错误)

const AccountsQuery = gql`
  query Accounts($cursor: String){
    accounts(cursor: $cursor) {
      cursor
      accountName
      accountId
    }
  }
`;

GraphQL wrapper: GraphQL包装器:

export default graphql(AccountsQuery, {
  props: ({ data: { loading, cursor, accounts, fetchmore } }) => ({
    loading,
    accounts,
    loadMoreEntries() {
      return fetchmore({
        query: AccountsQuery,
        variables: {
          cursor: cursor,
        },
        updateQuery: (previousResult, { fetchMoreResult }) => {
          const previousEntry = previousResult.entry;
          const newAccounts = fetchMoreResult.accounts;

          return {
            cursor: fetchMoreResult.cursor,
            entry: {
              accounts: [...newAccounts, ...previousEntry]
            },
          };
        },
      })
    }
  })
})(QuickViewContainer);

Any help would be appreciated to getting cursor pagination working! 任何帮助将不胜感激光标分页工作!

Sounds like the cursor field isn't getting implemented on the server. 听起来像cursor字段没有在服务器上实现。 Your Account type needs to have that field like so: 您的Account类型需要具有以下字段:

Account {
  cursor
  accountName
  accountId
}

For a convention on how to do cursor pagination, you should follow the standard Relay spec. 有关如何进行光标分页的约定,您应该遵循标准的中继规范。 You can read more about how it's implemented here in a Relay-compliant GraphQL API. 您可以在符合Relay标准的GraphQL API中阅读有关如何实现它的更多信息

This would make your query look like this: 这将使您的查询看起来像这样:

query {
  viewer {
    allAccounts {
      edges {
        cursor
        node {
          accountName
          accountId
        }
      }
    }
  }
}

Each edge account has a cursor that corresponds to a node and will be auto-populated with globally-unique opaque cursor IDs from the server. 每个边缘帐户都有一个与节点对应的游标,并且将从服务器自动填充全局唯一的不透明游标ID。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM