File tree Expand file tree Collapse file tree 3 files changed +68
-1
lines changed
Expand file tree Collapse file tree 3 files changed +68
-1
lines changed Original file line number Diff line number Diff line change @@ -86,6 +86,8 @@ import { commentLikeResolvers } from "./resolvers/commentLikeResolvers";
8686import { commentReplyResolvers } from "./resolvers/commentReplyResolvers" ;
8787import { jobApplicationTypeDefs } from "./schema/jobApplicationSchema" ;
8888import { jobApplicationResolver } from "./resolvers/jobApplicationResolver" ;
89+ import { blogRelatedResolvers } from "./resolvers/blogRelatedArticlesResolver" ;
90+ import { blogRelatedArticlesSchema } from "./schema/blogRelatedArticlesSchema" ;
8991
9092const PORT = process . env . PORT || 3000 ;
9193
@@ -133,6 +135,7 @@ const resolvers = mergeResolvers([
133135 commentResolvers ,
134136 commentLikeResolvers ,
135137 commentReplyResolvers ,
138+ blogRelatedResolvers
136139] ) ;
137140const typeDefs = mergeTypeDefs ( [
138141 applicationCycleTypeDefs ,
@@ -174,6 +177,7 @@ const typeDefs = mergeTypeDefs([
174177 commentReplySchema ,
175178 commentLikeSchema ,
176179 jobApplicationTypeDefs ,
180+ blogRelatedArticlesSchema
177181] ) ;
178182
179183const server = new ApolloServer ( {
@@ -205,4 +209,4 @@ const server = new ApolloServer({
205209connect ( ) . then ( ( ) => {
206210 console . log ( "Database connected!" ) ;
207211 server . listen ( PORT ) . then ( ( { url } ) => console . info ( `App on ${ url } ` ) ) ;
208- } ) ;
212+ } ) ;
Original file line number Diff line number Diff line change 1+ import axios from "axios" ;
2+ import dotenv from 'dotenv' ;
3+ import { BlogModel } from "../models/blogModel" ;
4+
5+
6+ dotenv . config ( ) ;
7+ interface articleDef {
8+ image : string ;
9+ title : string ;
10+ url : string ;
11+ source : { name : string ; } ;
12+ description : string ;
13+ publishedAt : String ;
14+ }
15+ export const blogRelatedResolvers = {
16+ Query : {
17+ blogRelatedArticles : async ( _ : any , { blogId } : { blogId : string } ) => {
18+ try {
19+ const blog = await BlogModel . findById ( blogId ) ;
20+ const tags = blog ?. tags ;
21+
22+ if ( ! tags || tags . length === 0 ) {
23+ throw new Error ( 'Blog tags are missing.' ) ;
24+ }
25+ const keywords = tags [ Math . floor ( Math . random ( ) * tags . length ) ] ;
26+ const response = await axios . get ( 'https://gnews.io/api/v4/search' , {
27+ params : {
28+ q : keywords ,
29+ token : process . env . Gnews_Api_Key ,
30+ lang : 'en' ,
31+ } , } ) ;
32+ return response . data . articles . map ( ( article :articleDef ) => ( {
33+ title : article . title ,
34+ url : article . url ,
35+ source : article . source . name ,
36+ description : article . description ,
37+ image : article . image ,
38+ publishedAt : article . publishedAt
39+ } ) ) ;
40+ } catch ( error : any ) {
41+ throw new Error ( "Failed to fetch related articles. Please try again later." ) ;
42+ }
43+ } ,
44+ } ,
45+ } ;
46+
Original file line number Diff line number Diff line change 1+ import { gql } from "apollo-server-express" ;
2+
3+ export const blogRelatedArticlesSchema = gql `
4+
5+ type Article {
6+ title: String
7+ url: String
8+ source: String
9+ description: String
10+ image: String
11+ publishedAt: String
12+ }
13+
14+ type Query {
15+ blogRelatedArticles(blogId: String!): [Article]
16+ }
17+ ` ;
You can’t perform that action at this time.
0 commit comments