Skip to content

Commit

Permalink
Added ads to the blog (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jadowacu1 authored Nov 19, 2024
1 parent 45a8f94 commit 93d1ea8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -133,6 +135,7 @@ const resolvers = mergeResolvers([
commentResolvers,
commentLikeResolvers,
commentReplyResolvers,
blogRelatedResolvers
]);
const typeDefs = mergeTypeDefs([
applicationCycleTypeDefs,
Expand Down Expand Up @@ -174,6 +177,7 @@ const typeDefs = mergeTypeDefs([
commentReplySchema,
commentLikeSchema,
jobApplicationTypeDefs,
blogRelatedArticlesSchema
]);

const server = new ApolloServer({
Expand Down Expand Up @@ -205,4 +209,4 @@ const server = new ApolloServer({
connect().then(() => {
console.log("Database connected!");
server.listen(PORT).then(({ url }) => console.info(`App on ${url}`));
});
});
46 changes: 46 additions & 0 deletions src/resolvers/blogRelatedArticlesResolver.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
},
},
};

17 changes: 17 additions & 0 deletions src/schema/blogRelatedArticlesSchema.ts
Original file line number Diff line number Diff line change
@@ -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]
}
`;

0 comments on commit 93d1ea8

Please sign in to comment.