Skip to content

Commit 93d1ea8

Browse files
authored
Added ads to the blog (#148)
1 parent 45a8f94 commit 93d1ea8

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ import { commentLikeResolvers } from "./resolvers/commentLikeResolvers";
8686
import { commentReplyResolvers } from "./resolvers/commentReplyResolvers";
8787
import { jobApplicationTypeDefs } from "./schema/jobApplicationSchema";
8888
import { jobApplicationResolver } from "./resolvers/jobApplicationResolver";
89+
import { blogRelatedResolvers } from "./resolvers/blogRelatedArticlesResolver";
90+
import { blogRelatedArticlesSchema } from "./schema/blogRelatedArticlesSchema";
8991

9092
const PORT = process.env.PORT || 3000;
9193

@@ -133,6 +135,7 @@ const resolvers = mergeResolvers([
133135
commentResolvers,
134136
commentLikeResolvers,
135137
commentReplyResolvers,
138+
blogRelatedResolvers
136139
]);
137140
const typeDefs = mergeTypeDefs([
138141
applicationCycleTypeDefs,
@@ -174,6 +177,7 @@ const typeDefs = mergeTypeDefs([
174177
commentReplySchema,
175178
commentLikeSchema,
176179
jobApplicationTypeDefs,
180+
blogRelatedArticlesSchema
177181
]);
178182

179183
const server = new ApolloServer({
@@ -205,4 +209,4 @@ const server = new ApolloServer({
205209
connect().then(() => {
206210
console.log("Database connected!");
207211
server.listen(PORT).then(({ url }) => console.info(`App on ${url}`));
208-
});
212+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
`;

0 commit comments

Comments
 (0)