diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-20 15:37:08 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-20 15:37:08 +0200 |
| commit | f4c7ab4e306d2f04324853e67032d370abd65d0c (patch) | |
| tree | 2c7d1ad467d6c52bc134202f0d33f7524f9056fa /src/services | |
| parent | bbd63400f94b43fde04449e0c71d14763d893e6a (diff) | |
chore: handle blog pagination when JS is disabled
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/graphql/api.ts | 9 | ||||
| -rw-r--r-- | src/services/graphql/articles.query.ts | 12 | ||||
| -rw-r--r-- | src/services/graphql/articles.ts | 28 |
3 files changed, 48 insertions, 1 deletions
diff --git a/src/services/graphql/api.ts b/src/services/graphql/api.ts index 9f68ddc..009aea4 100644 --- a/src/services/graphql/api.ts +++ b/src/services/graphql/api.ts @@ -2,6 +2,7 @@ import { settings } from '@utils/config'; import { articleBySlugQuery, articlesCardQuery, + articlesEndCursor, articlesQuery, articlesSlugQuery, totalArticlesQuery, @@ -28,6 +29,7 @@ export type Queries = | typeof articlesQuery | typeof articleBySlugQuery | typeof articlesCardQuery + | typeof articlesEndCursor | typeof articlesSlugQuery | typeof commentsQuery | typeof thematicBySlugQuery @@ -100,9 +102,15 @@ export type NodesResponse<T> = { nodes: T[]; }; +export type EndCursor = Pick< + EdgesResponse<Pick<PageInfo, 'endCursor'>>, + 'pageInfo' +>; + export type ResponseMap<T> = { [articleBySlugQuery]: ArticleResponse<T>; [articlesCardQuery]: ArticlesResponse<NodesResponse<T>>; + [articlesEndCursor]: ArticlesResponse<EndCursor>; [articlesQuery]: ArticlesResponse<EdgesResponse<T>>; [articlesSlugQuery]: ArticlesResponse<EdgesResponse<T>>; [commentsQuery]: CommentsResponse<NodesResponse<T>>; @@ -213,6 +221,7 @@ export type SendMailVars = { export type VariablesMap = { [articleBySlugQuery]: BySlugVar; [articlesCardQuery]: EdgesVars; + [articlesEndCursor]: EdgesVars; [articlesQuery]: EdgesVars; [articlesSlugQuery]: EdgesVars; [commentsQuery]: ByContentIdVar; diff --git a/src/services/graphql/articles.query.ts b/src/services/graphql/articles.query.ts index e02ca8e..7bd0901 100644 --- a/src/services/graphql/articles.query.ts +++ b/src/services/graphql/articles.query.ts @@ -184,3 +184,15 @@ export const totalArticlesQuery = `query PostsTotal($search: String = "") { } } }`; + +/** + * Query the end cursor based on the queried posts number. + */ +export const articlesEndCursor = `query EndCursorAfter($first: Int) { + posts(first: $first) { + pageInfo { + hasNextPage + endCursor + } + } +}`; diff --git a/src/services/graphql/articles.ts b/src/services/graphql/articles.ts index 41052c4..f130872 100644 --- a/src/services/graphql/articles.ts +++ b/src/services/graphql/articles.ts @@ -7,10 +7,18 @@ import { import { getAuthorFromRawData } from '@utils/helpers/author'; import { getImageFromRawData } from '@utils/helpers/images'; import { getPageLinkFromRawData } from '@utils/helpers/pages'; -import { EdgesResponse, EdgesVars, fetchAPI, getAPIUrl, PageInfo } from './api'; +import { + EdgesResponse, + EdgesVars, + EndCursor, + fetchAPI, + getAPIUrl, + PageInfo, +} from './api'; import { articleBySlugQuery, articlesCardQuery, + articlesEndCursor, articlesQuery, articlesSlugQuery, totalArticlesQuery, @@ -173,3 +181,21 @@ export const getAllArticlesSlugs = async (): Promise<string[]> => { return response.posts.edges.map((edge) => edge.node.slug); }; + +/** + * Retrieve the last cursor. + * + * @param {EdgesVars} props - An object of GraphQL variables. + * @returns {Promise<string>} - The end cursor. + */ +export const getArticlesEndCursor = async ( + props: EdgesVars +): Promise<string> => { + const response = await fetchAPI<EndCursor, typeof articlesEndCursor>({ + api: getAPIUrl(), + query: articlesEndCursor, + variables: { ...props }, + }); + + return response.posts.pageInfo.endCursor; +}; |
