diff options
Diffstat (limited to 'src/services/graphql/fetchers/thematics')
5 files changed, 255 insertions, 0 deletions
diff --git a/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts b/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts new file mode 100644 index 0000000..739c009 --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts @@ -0,0 +1,34 @@ +import type { GraphQLNodes, Nullable, SlugNode } from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; +import { fetchThematicsCount } from './fetch-thematics-count'; + +type ThematicsSlugsResponse = { + thematics: Nullable<GraphQLNodes<SlugNode>>; +}; + +const thematicsSlugsQuery = `query ThematicsSlugs($first: Int) { + thematics(first: $first) { + nodes { + slug + } + } +}`; + +/** + * Retrieve the WordPress thematics slugs. + * + * @returns {Promise<string[]>} The thematics slugs. + */ +export const fetchAllThematicsSlugs = async (): Promise<string[]> => { + const thematicsCount = await fetchThematicsCount(); + const response = await fetchGraphQL<ThematicsSlugsResponse>({ + query: thematicsSlugsQuery, + url: getGraphQLUrl(), + variables: { first: thematicsCount }, + }); + + if (!response.thematics) + return Promise.reject(new Error('Unable to find the thematics slugs.')); + + return response.thematics.nodes.map((node) => node.slug); +}; diff --git a/src/services/graphql/fetchers/thematics/fetch-thematic.ts b/src/services/graphql/fetchers/thematics/fetch-thematic.ts new file mode 100644 index 0000000..a9958bc --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-thematic.ts @@ -0,0 +1,96 @@ +import type { Nullable, WPThematic } from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type ThematicResponse = { + thematic: Nullable<WPThematic>; +}; + +const thematicQuery = `query Thematic($slug: ID!) { + thematic(id: $slug, idType: SLUG) { + acfThematics { + postsInThematic { + ... on Post { + acfPosts { + postsInTopic { + ... on Topic { + databaseId + slug + title + } + } + } + author { + node { + name + } + } + commentCount + contentParts { + beforeMore + } + databaseId + date + featuredImage { + node { + altText + mediaDetails { + height + width + } + sourceUrl + title + } + } + info { + wordsCount + } + modified + slug + title + } + } + } + contentParts { + afterMore + beforeMore + } + featuredImage { + node { + altText + mediaDetails { + height + width + } + sourceUrl + title + } + } + seo { + metaDesc + title + } + slug + title + } +}`; + +/** + * Retrieve a WordPress thematic by slug. + * + * @param {string} slug - The thematic slug. + * @returns {Promise<WPThematic>} The requested thematic. + */ +export const fetchThematic = async (slug: string): Promise<WPThematic> => { + const response = await fetchGraphQL<ThematicResponse>({ + query: thematicQuery, + url: getGraphQLUrl(), + variables: { slug }, + }); + + if (!response.thematic) + return Promise.reject( + new Error(`No thematic found for the following slug ${slug}.`) + ); + + return response.thematic; +}; diff --git a/src/services/graphql/fetchers/thematics/fetch-thematics-count.ts b/src/services/graphql/fetchers/thematics/fetch-thematics-count.ts new file mode 100644 index 0000000..29a3b17 --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-thematics-count.ts @@ -0,0 +1,43 @@ +import type { + GraphQLPageInfo, + GraphQLTaxonomyWhere, + Nullable, +} from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type ThematicsCountResponse = { + thematics: Nullable<{ + pageInfo: Pick<GraphQLPageInfo, 'total'>; + }>; +}; + +const thematicsCountQuery = `query ThematicsCount($search: String, $title: String) { + thematics(where: {search: $search, title: $title}) { + pageInfo { + total + } + } +}`; + +/** + * Retrieve the total of WordPress thematics. + * + * @param {GraphQLTaxonomyWhere} [input] - The input to filter the thematics. + * @returns {Promise<number>} The total number of thematics. + */ +export const fetchThematicsCount = async ( + input?: GraphQLTaxonomyWhere +): Promise<number> => { + const response = await fetchGraphQL<ThematicsCountResponse>({ + query: thematicsCountQuery, + url: getGraphQLUrl(), + variables: { ...input }, + }); + + if (!response.thematics) + return Promise.reject( + new Error('Unable to find the total number of thematics.') + ); + + return response.thematics.pageInfo.total; +}; diff --git a/src/services/graphql/fetchers/thematics/fetch-thematics-list.ts b/src/services/graphql/fetchers/thematics/fetch-thematics-list.ts new file mode 100644 index 0000000..f4d22c6 --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-thematics-list.ts @@ -0,0 +1,78 @@ +import type { + GraphQLConnection, + GraphQLEdgesInput, + GraphQLTaxonomyOrderBy, + GraphQLTaxonomyWhere, + Nullable, + WPThematicPreview, +} from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type ThematicsListResponse = { + thematics: Nullable<GraphQLConnection<WPThematicPreview>>; +}; + +const thematicsListQuery = `query ThematicsList($after: String, $before: String, $first: Int, $last: Int, $orderby: [PostObjectsConnectionOrderbyInput], $search: String, $title: String) { + thematics( + after: $after + before: $before + first: $first + last: $last + where: {orderby: $orderby, search: $search, title: $title} + ) { + edges { + cursor + node { + contentParts { + beforeMore + } + databaseId + featuredImage { + node { + altText + mediaDetails { + height + width + } + sourceUrl + title + } + } + slug + title + } + } + } +}`; + +export type FetchThematicsListInput = GraphQLEdgesInput & { + orderBy?: GraphQLTaxonomyOrderBy; + where?: GraphQLTaxonomyWhere; +}; + +/** + * Retrieve a paginated list of WordPress thematics. + * + * @param {FetchThematicsListInput} input - The input to retrieve thematics. + * @returns {Promise<GraphQLConnection<WPThematicPreview>>} The paginated thematics. + */ +export const fetchThematicsList = async ({ + orderBy, + where, + ...vars +}: FetchThematicsListInput): Promise<GraphQLConnection<WPThematicPreview>> => { + const response = await fetchGraphQL<ThematicsListResponse>({ + query: thematicsListQuery, + url: getGraphQLUrl(), + variables: { + ...vars, + ...where, + orderBy: orderBy ? [orderBy] : undefined, + }, + }); + + if (!response.thematics) + return Promise.reject(new Error('No thematics found.')); + + return response.thematics; +}; diff --git a/src/services/graphql/fetchers/thematics/index.ts b/src/services/graphql/fetchers/thematics/index.ts new file mode 100644 index 0000000..c002793 --- /dev/null +++ b/src/services/graphql/fetchers/thematics/index.ts @@ -0,0 +1,4 @@ +export * from './fetch-all-thematics-slugs'; +export * from './fetch-thematic'; +export * from './fetch-thematics-count'; +export * from './fetch-thematics-list'; |
