aboutsummaryrefslogtreecommitdiffstats
path: root/src/services
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-13 16:28:06 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-13 16:28:06 +0200
commit06ea295857e508a830669cb402d2156204309b1e (patch)
tree87db074b639c6bf1cd3b8bbe27858662196dc6b9 /src/services
parentdab72bb270ee2ee47a0b472d5e9e240cba7cbf0f (diff)
chore: add blog page widgets
Diffstat (limited to 'src/services')
-rw-r--r--src/services/graphql/api.ts10
-rw-r--r--src/services/graphql/thematics.query.ts11
-rw-r--r--src/services/graphql/thematics.ts34
-rw-r--r--src/services/graphql/topics.query.ts11
-rw-r--r--src/services/graphql/topics.ts42
5 files changed, 107 insertions, 1 deletions
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<T> = {
post: T;
@@ -105,6 +109,8 @@ export type ResponseMap<T> = {
[topicsListQuery]: TopicsResponse<EdgesResponse<T>>;
[topicsSlugQuery]: TopicsResponse<EdgesResponse<T>>;
[totalArticlesQuery]: ArticlesResponse<T>;
+ [totalThematicsQuery]: ThematicsResponse<T>;
+ [totalTopicsQuery]: TopicsResponse<T>;
};
export type GraphQLResponse<
@@ -162,6 +168,8 @@ export type VariablesMap = {
[topicsListQuery]: EdgesVars;
[topicsSlugQuery]: EdgesVars;
[totalArticlesQuery]: null;
+ [totalThematicsQuery]: null;
+ [totalTopicsQuery]: null;
};
export type FetchAPIProps<T extends Queries | Mutations> = {
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<number>} - The thematics total number.
+ */
+export const getTotalThematics = async (): Promise<number> => {
+ const response = await fetchAPI<TotalItems, typeof totalThematicsQuery>({
+ 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<EdgesResponse<RawThematicPreview>>} The thematics data.
+ */
+export const getThematicsPreview = async (
+ props: EdgesVars
+): Promise<EdgesResponse<RawThematicPreview>> => {
+ 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<number>} - The topics total number.
+ */
+export const getTotalTopics = async (): Promise<number> => {
+ const response = await fetchAPI<TotalItems, typeof totalTopicsQuery>({
+ 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<EdgesResponse<RawTopicPreview>>} The topics data.
+ */
+export const getTopicsPreview = async (
+ props: EdgesVars
+): Promise<EdgesResponse<RawTopicPreview>> => {
+ const response = await fetchAPI<RawTopicPreview, typeof topicsListQuery>({
+ 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;
+};