From 06ea295857e508a830669cb402d2156204309b1e Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 13 May 2022 16:28:06 +0200 Subject: chore: add blog page widgets --- src/services/graphql/api.ts | 10 +++++++- src/services/graphql/thematics.query.ts | 11 +++++++++ src/services/graphql/thematics.ts | 34 ++++++++++++++++++++++++++ src/services/graphql/topics.query.ts | 11 +++++++++ src/services/graphql/topics.ts | 42 +++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/services/graphql/thematics.ts create mode 100644 src/services/graphql/topics.ts (limited to 'src/services') diff --git a/src/services/graphql/api.ts b/src/services/graphql/api.ts index 9811d86..5bed9f9 100644 --- a/src/services/graphql/api.ts +++ b/src/services/graphql/api.ts @@ -12,11 +12,13 @@ import { thematicBySlugQuery, thematicsListQuery, thematicsSlugQuery, + totalThematicsQuery, } from './thematics.query'; import { topicBySlugQuery, topicsListQuery, topicsSlugQuery, + totalTopicsQuery, } from './topics.query'; export type Mutations = typeof sendMailMutation; @@ -33,7 +35,9 @@ export type Queries = | typeof topicBySlugQuery | typeof topicsListQuery | typeof topicsSlugQuery - | typeof totalArticlesQuery; + | typeof totalArticlesQuery + | typeof totalThematicsQuery + | typeof totalTopicsQuery; export type ArticleResponse = { post: T; @@ -105,6 +109,8 @@ export type ResponseMap = { [topicsListQuery]: TopicsResponse>; [topicsSlugQuery]: TopicsResponse>; [totalArticlesQuery]: ArticlesResponse; + [totalThematicsQuery]: ThematicsResponse; + [totalTopicsQuery]: TopicsResponse; }; export type GraphQLResponse< @@ -162,6 +168,8 @@ export type VariablesMap = { [topicsListQuery]: EdgesVars; [topicsSlugQuery]: EdgesVars; [totalArticlesQuery]: null; + [totalThematicsQuery]: null; + [totalTopicsQuery]: null; }; export type FetchAPIProps = { diff --git a/src/services/graphql/thematics.query.ts b/src/services/graphql/thematics.query.ts index 76949ad..db8e751 100644 --- a/src/services/graphql/thematics.query.ts +++ b/src/services/graphql/thematics.query.ts @@ -114,3 +114,14 @@ export const thematicsSlugQuery = `query ThematicsSlug($first: Int = 10, $after: } } }`; + +/** + * Query the total number of thematics. + */ +export const totalThematicsQuery = `query ThematicsTotal { + thematics { + pageInfo { + total + } + } +}`; diff --git a/src/services/graphql/thematics.ts b/src/services/graphql/thematics.ts new file mode 100644 index 0000000..7003e08 --- /dev/null +++ b/src/services/graphql/thematics.ts @@ -0,0 +1,34 @@ +import { RawThematicPreview, TotalItems } from '@ts/types/raw-data'; +import { EdgesResponse, EdgesVars, fetchAPI, getAPIUrl } from './api'; +import { thematicsListQuery, totalThematicsQuery } from './thematics.query'; + +/** + * Retrieve the total number of thematics. + * + * @returns {Promise} - The thematics total number. + */ +export const getTotalThematics = async (): Promise => { + const response = await fetchAPI({ + api: getAPIUrl(), + query: totalThematicsQuery, + }); + + return response.thematics.pageInfo.total; +}; + +/** + * Retrieve the given number of thematics from API. + * + * @param {EdgesVars} props - An object of GraphQL variables. + * @returns {Promise>} The thematics data. + */ +export const getThematicsPreview = async ( + props: EdgesVars +): Promise> => { + const response = await fetchAPI< + RawThematicPreview, + typeof thematicsListQuery + >({ api: getAPIUrl(), query: thematicsListQuery, variables: props }); + + return response.thematics; +}; diff --git a/src/services/graphql/topics.query.ts b/src/services/graphql/topics.query.ts index 8783799..6cc525e 100644 --- a/src/services/graphql/topics.query.ts +++ b/src/services/graphql/topics.query.ts @@ -115,3 +115,14 @@ export const topicsSlugQuery = `query TopicsSlug($first: Int = 10, $after: Strin } } }`; + +/** + * Query the total number of topics. + */ +export const totalTopicsQuery = `query TopicsTotal { + topics { + pageInfo { + total + } + } +}`; diff --git a/src/services/graphql/topics.ts b/src/services/graphql/topics.ts new file mode 100644 index 0000000..0f59bad --- /dev/null +++ b/src/services/graphql/topics.ts @@ -0,0 +1,42 @@ +import { RawTopicPreview, TotalItems } from '@ts/types/raw-data'; +import { EdgesResponse, EdgesVars, fetchAPI, getAPIUrl } from './api'; +import { topicsListQuery, totalTopicsQuery } from './topics.query'; + +/** + * Retrieve the total number of topics. + * + * @returns {Promise} - The topics total number. + */ +export const getTotalTopics = async (): Promise => { + const response = await fetchAPI({ + api: getAPIUrl(), + query: totalTopicsQuery, + }); + + return response.topics.pageInfo.total; +}; + +/** + * Retrieve the given number of topics from API. + * + * @param {EdgesVars} props - An object of GraphQL variables. + * @returns {Promise>} The topics data. + */ +export const getTopicsPreview = async ( + props: EdgesVars +): Promise> => { + const response = await fetchAPI({ + api: getAPIUrl(), + query: topicsListQuery, + variables: props, + }); + + return response.topics; +}; + +export const getAllTopicsLinks = async () => { + const allTopics = []; + const initialTopics = await getTopicsPreview({ first: 1 }); + + if (!initialTopics.pageInfo.hasNextPage) return initialTopics; +}; -- cgit v1.2.3