apollographql / apollographql/fullstack-tutorial

Launches pagination implementation is a bad example for reference

オープン
#78 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る
主要言語
TypeScript
スター
1.2k
フォーク
808
PR マージ指標
30日以内にマージされた PR はありません

説明

The GraphQL server-side cursor pagination example should be discarded. While the intent of the `paginateResults` function in `final/server/src/utils.js` is good, there are a number of significant flaws with this implementation.

The lowest hanging fruit is that there is a line of code at the end of the function - `results.slice(cursorIndex >= 0 ? cursorIndex + 1 : 0, cursorIndex >= 0);` which is unreachable.

However, the most egregious error is how the `launches` function is defined in `final/server/src/resolvers.js` - which gives the impression that data is being paged responsibly. It is **not**

Every time pagination is invoked, the API request loads ALL THE DATA from the API and then passes it to the pagination function. For the example SpaceX data, there are approximately 76 results that get loaded and then processed to return the 20 results the user is expecting:

```
Query: {
launches: async (_, { pageSize = 20, after }, { dataSources }) => {
const allLaunches = await dataSources.launchAPI.getAllLaunches();
// we want these in reverse chronological order
allLaunches.reverse();

const launches = paginateResults({
after,
pageSize,
results: allLaunches,
});

return {
launches,
cursor: launches.length ? launches[launches.length - 1].cursor : null,
// if the cursor of the end of the paginated results is the same as the
// last item in _all_ results, then there are no more results after this
hasMore: launches.length
? launches[launches.length - 1].cursor !==
allLaunches[allLaunches.length - 1].cursor
: false,
};
},
...
```

If that API endpoint were to return a massive amount of data (imagine 40,000 results), this resolver would load all 40,000 results and then find 20 to return. When the user would click load more, all 40,000 results would be loaded into memory again and then the next 20 results would be returned...effectively processing 80,000 results to display 40 to the user. EEK!

This is definitely something that should be revisited. Imagine a scenario with 30 users following a simple workflow of viewing the first 20 results and then loading an additional 20 results.

Additionally, if the result set grew (imagine 40,001 records), the newest record would never be displayed to the user because it would have been in a spot the cursor presumably has already visited, right?

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

調査の方向性

final/server/src/utils.js と final/server/src/resolvers.js を読み、paginateResults と Query.launches に注目してください。getAllLaunches が各リクエストにデータを提供する仕組みを追跡してください。意図されたページネーションの動作を定義し、1ページ目とそれ以降のページで cursor、pageSize、ordering、hasMore のセマンティクスが、結果セット全体を読み込まずに維持されることを確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
graphql, javascript, node.js
領域
backend-api-design, performance
issue の種類
リファクタリング
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。