diff --git a/src/index.ts b/src/index.ts index bec0b7d..a7b3ce6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -86,6 +86,8 @@ import { commentLikeResolvers } from "./resolvers/commentLikeResolvers"; import { commentReplyResolvers } from "./resolvers/commentReplyResolvers"; import { jobApplicationTypeDefs } from "./schema/jobApplicationSchema"; import { jobApplicationResolver } from "./resolvers/jobApplicationResolver"; +import { blogRelatedResolvers } from "./resolvers/blogRelatedArticlesResolver"; +import { blogRelatedArticlesSchema } from "./schema/blogRelatedArticlesSchema"; const PORT = process.env.PORT || 3000; @@ -133,6 +135,7 @@ const resolvers = mergeResolvers([ commentResolvers, commentLikeResolvers, commentReplyResolvers, + blogRelatedResolvers ]); const typeDefs = mergeTypeDefs([ applicationCycleTypeDefs, @@ -174,6 +177,7 @@ const typeDefs = mergeTypeDefs([ commentReplySchema, commentLikeSchema, jobApplicationTypeDefs, + blogRelatedArticlesSchema ]); const server = new ApolloServer({ @@ -205,4 +209,4 @@ const server = new ApolloServer({ connect().then(() => { console.log("Database connected!"); server.listen(PORT).then(({ url }) => console.info(`App on ${url}`)); -}); +}); \ No newline at end of file diff --git a/src/resolvers/blogRelatedArticlesResolver.ts b/src/resolvers/blogRelatedArticlesResolver.ts new file mode 100644 index 0000000..b4513f8 --- /dev/null +++ b/src/resolvers/blogRelatedArticlesResolver.ts @@ -0,0 +1,46 @@ +import axios from "axios"; +import dotenv from 'dotenv'; +import { BlogModel } from "../models/blogModel"; + + +dotenv.config(); +interface articleDef { + image: string; + title: string; + url: string; + source: { name: string; }; + description: string; + publishedAt: String; +} +export const blogRelatedResolvers = { + Query: { + blogRelatedArticles: async (_: any,{ blogId }: { blogId: string }) => { + try{ + const blog = await BlogModel.findById(blogId); + const tags = blog?.tags; + + if (!tags || tags.length === 0) { + throw new Error('Blog tags are missing.'); + } + const keywords = tags[Math.floor(Math.random() * tags.length)]; + const response = await axios.get('https://gnews.io/api/v4/search', { + params: { + q: keywords, + token: process.env.Gnews_Api_Key, + lang: 'en', + },}); + return response.data.articles.map((article:articleDef ) => ({ + title: article.title, + url: article.url, + source: article.source.name, + description: article.description, + image: article.image, + publishedAt: article.publishedAt + })); + } catch (error: any) { + throw new Error("Failed to fetch related articles. Please try again later."); + } + }, + }, + }; + \ No newline at end of file diff --git a/src/schema/blogRelatedArticlesSchema.ts b/src/schema/blogRelatedArticlesSchema.ts new file mode 100644 index 0000000..78813fd --- /dev/null +++ b/src/schema/blogRelatedArticlesSchema.ts @@ -0,0 +1,17 @@ +import { gql } from "apollo-server-express"; + +export const blogRelatedArticlesSchema = gql` + +type Article { + title: String + url: String + source: String + description: String + image: String + publishedAt: String +} + +type Query { + blogRelatedArticles(blogId: String!): [Article] +} +`;