Skip to content

Commit

Permalink
bff GetBookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
YukiOnishi1129 committed Sep 10, 2024
1 parent 0e84875 commit 79afc30
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
15 changes: 14 additions & 1 deletion bff/apollo-gateway/src/app/bookmark/bookmark.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { UseGuards } from '@nestjs/common';
import { Resolver, Args, Mutation } from '@nestjs/graphql';
import { Resolver, Query, Args, Mutation, Context } from '@nestjs/graphql';
import {
Bookmark,
BookmarkConnection,
BookmarksInput,
CreateBookmarkInput,
DeleteBookmarkInput,
} from 'src/graphql/types/graphql';

import { BookmarkService } from './bookmark.service';
import { GraphQLContext } from '../../graphql/context.interface';
import { SupabaseAuthGuard } from '../auth/auth.guard';

@Resolver()
export class BookmarkResolver {
constructor(private readonly bookmarkService: BookmarkService) {}

@Query(() => BookmarkConnection)
@UseGuards(SupabaseAuthGuard)
async bookmarks(
@Args('input') input: BookmarksInput,
@Context() context: GraphQLContext,
): Promise<BookmarkConnection> {
const user = context.req.user;
return await this.bookmarkService.getBookmarks(user.id, input);
}

@Mutation(() => Bookmark)
@UseGuards(SupabaseAuthGuard)
async createBookmark(
Expand Down
66 changes: 66 additions & 0 deletions bff/apollo-gateway/src/app/bookmark/bookmark.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import {
CreateBookmarkInput,
Bookmark,
DeleteBookmarkInput,
BookmarksInput,
BookmarkConnection,
} from 'src/graphql/types/graphql';
import {
GetBookmarksRequest,
CreateBookmarkRequest,
DeleteBookmarkRequest,
} from 'src/grpc/bookmark/bookmark_pb';
Expand All @@ -20,6 +23,69 @@ export class BookmarkService {
private readonly grpcBookmarkClientService: GrpcBookmarkClientService,
) {}

async getBookmarks(
userId: string,
input: BookmarksInput,
): Promise<BookmarkConnection> {
const req = new GetBookmarksRequest();
req.setUserId(userId);
if (input?.first) req.setLimit(input.first);
if (input?.after) req.setCursor(input.after);
if (input?.keyword)
req.setKeyword(new StringValue().setValue(input.keyword));

return new Promise((resolve, reject) => {
const client = this.grpcBookmarkClientService.getGrpcBookmarkService();
client.getBookmarks(req, (err, res) => {
if (err) {
reject({
code: err?.code || 500,
message: err?.message || 'something went wrong',
});
return;
}

const resBookmarks = res.toObject();

const bookmarks: BookmarkConnection = {
edges: resBookmarks?.bookmarkedgeList
? resBookmarks.bookmarkedgeList.map((edge) => {
return {
cursor: edge.bookmark.id,
node: {
articleId: edge.bookmark.articleId,
articleUrl: edge.bookmark.articleUrl,
createdAt: convertTimestampToInt(edge.bookmark.createdAt),
description: edge.bookmark.description,
id: edge.bookmark.id,
isEng: edge.bookmark.isEng,
isRead: edge.bookmark.isRead,
platformFaviconUrl: edge.bookmark.platformFaviconUrl,
platformId: edge.bookmark?.platformId?.value,
platformName: edge.bookmark.platformName,
platformUrl: edge.bookmark.platformUrl,
publishedAt: edge.bookmark?.publishedAt
? convertTimestampToInt(edge.bookmark.publishedAt)
: undefined,
thumbnailUrl: edge.bookmark.thumbnailUrl,
title: edge.bookmark.title,
updatedAt: convertTimestampToInt(edge.bookmark.updatedAt),
},
};
})
: [],
pageInfo: {
endCursor: resBookmarks.pageInfo.endCursor,
hasNextPage: resBookmarks.pageInfo.hasNextPage,
hasPreviousPage: false,
},
};

resolve(bookmarks);
});
});
}

async createBookmark(input: CreateBookmarkInput): Promise<Bookmark> {
const req = new CreateBookmarkRequest();
req.setArticleId(input.articleId);
Expand Down

0 comments on commit 79afc30

Please sign in to comment.