Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bookmark): bff bookmarks query #153

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 21 additions & 0 deletions bff/apollo-gateway/src/graphql/types/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,23 @@ export class DeleteBookmarkInput {
userId: string;
}

export class BookmarksInput {
userId: string;
keyword?: Nullable<string>;
first?: Nullable<number>;
after?: Nullable<string>;
last?: Nullable<number>;
before?: Nullable<string>;
}

export interface Node {
id: string;
}

export abstract class IQuery {
abstract articles(articlesInput: ArticlesInput): ArticleConnection | Promise<ArticleConnection>;

abstract bookmarks(input: BookmarksInput): BookmarkConnection | Promise<BookmarkConnection>;
}

export class Article implements Node {
Expand Down Expand Up @@ -111,6 +122,16 @@ export class Bookmark implements Node {
updatedAt: number;
}

export class BookmarkConnection {
edges: BookmarkEdge[];
pageInfo: PageInfo;
}

export class BookmarkEdge {
cursor: string;
node: Bookmark;
}

export class Category implements Node {
id: string;
name: string;
Expand Down
65 changes: 60 additions & 5 deletions bff/apollo-gateway/src/grpc/bookmark/bookmark_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import * as google_protobuf_timestamp_pb from "google-protobuf/google/protobuf/t
import * as google_protobuf_wrappers_pb from "google-protobuf/google/protobuf/wrappers_pb";

export class GetBookmarksResponse extends jspb.Message {
clearBookmarksList(): void;
getBookmarksList(): Array<Bookmark>;
setBookmarksList(value: Array<Bookmark>): GetBookmarksResponse;
addBookmarks(value?: Bookmark, index?: number): Bookmark;
clearBookmarkedgeList(): void;
getBookmarkedgeList(): Array<BookmarkEdge>;
setBookmarkedgeList(value: Array<BookmarkEdge>): GetBookmarksResponse;
addBookmarkedge(value?: BookmarkEdge, index?: number): BookmarkEdge;

hasPageInfo(): boolean;
clearPageInfo(): void;
getPageInfo(): PageInfo | undefined;
setPageInfo(value?: PageInfo): GetBookmarksResponse;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): GetBookmarksResponse.AsObject;
Expand All @@ -27,7 +32,31 @@ export class GetBookmarksResponse extends jspb.Message {

export namespace GetBookmarksResponse {
export type AsObject = {
bookmarksList: Array<Bookmark.AsObject>,
bookmarkedgeList: Array<BookmarkEdge.AsObject>,
pageInfo?: PageInfo.AsObject,
}
}

export class PageInfo extends jspb.Message {
getEndCursor(): string;
setEndCursor(value: string): PageInfo;
getHasNextPage(): boolean;
setHasNextPage(value: boolean): PageInfo;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): PageInfo.AsObject;
static toObject(includeInstance: boolean, msg: PageInfo): PageInfo.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: PageInfo, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): PageInfo;
static deserializeBinaryFromReader(message: PageInfo, reader: jspb.BinaryReader): PageInfo;
}

export namespace PageInfo {
export type AsObject = {
endCursor: string,
hasNextPage: boolean,
}
}

Expand Down Expand Up @@ -293,3 +322,29 @@ export namespace Bookmark {
updatedAt?: google_protobuf_timestamp_pb.Timestamp.AsObject,
}
}

export class BookmarkEdge extends jspb.Message {

hasBookmark(): boolean;
clearBookmark(): void;
getBookmark(): Bookmark | undefined;
setBookmark(value?: Bookmark): BookmarkEdge;
getCursor(): string;
setCursor(value: string): BookmarkEdge;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): BookmarkEdge.AsObject;
static toObject(includeInstance: boolean, msg: BookmarkEdge): BookmarkEdge.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: BookmarkEdge, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): BookmarkEdge;
static deserializeBinaryFromReader(message: BookmarkEdge, reader: jspb.BinaryReader): BookmarkEdge;
}

export namespace BookmarkEdge {
export type AsObject = {
bookmark?: Bookmark.AsObject,
cursor: string,
}
}
Loading
Loading