From 08d3391c2dcf73e3c4589255b658bee3270bc1d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=E1=BB=8Bnh=20Duy=20H=C6=B0ng?= <57101685+HUNG-rushb@users.noreply.github.com> Date: Sat, 6 Jan 2024 18:23:14 +0700 Subject: [PATCH] ok --- src/Type_Definitions/Post_Post.js | 9 ++++++ src/resolvers/Query/POST.js | 52 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/Type_Definitions/Post_Post.js b/src/Type_Definitions/Post_Post.js index 5098f61..5766988 100644 --- a/src/Type_Definitions/Post_Post.js +++ b/src/Type_Definitions/Post_Post.js @@ -35,6 +35,11 @@ const postDefs = gql` limit: Int after: String ): PostPagination! + tagSearchPosts( + data: TagSearchPostInput! + limit: Int + after: String + ): PostPagination! } type PostPagination { @@ -75,6 +80,10 @@ const postDefs = gql` postId: String! } + input TagSearchPostInput { + tagName: String! + } + input SearchQueryInput { userId: ID! searchString: String! diff --git a/src/resolvers/Query/POST.js b/src/resolvers/Query/POST.js index 8632003..92b3ddf 100644 --- a/src/resolvers/Query/POST.js +++ b/src/resolvers/Query/POST.js @@ -664,6 +664,58 @@ const postQuery = { }, }; }, + tagSearchPosts: async (parent, args, info) => { + const { after, limit = DEFAULT_LIMIT } = args; + const { tagName } = args.data; + console.log({ tagName }); + + const [result, count] = await Promise.all([ + prisma.post.findMany({ + take: limit || DEFAULT_LIMIT, + ...(after && { + skip: 1, + }), + where: { + tag: { has: tagName }, + image: { + isNot: null, + }, + }, + include: { + image: true, + }, + ...(after && { + cursor: { + id: after, + }, + }), + }), + prisma.post.count(), + ]); + + // console.log('Result', result); + // console.log('count', count); + + const hasNextPage = + result.length !== 0 && result.length < count && result.length === limit; + // console.log('hasNextPage', hasNextPage); + + const nodes = result.map((each) => ({ + node: each, + cursor: each.id, + })); + + return { + edges: shuffleArrayExceptFirstAndLast(nodes), + pageInfo: { + hasNextPage, + hasPreviousPage: after ? true : false, + // startCursor, + startCursor: nodes.length === 0 ? '' : nodes[0].cursor, + endCursor: nodes.length === 0 ? '' : nodes.slice(-1)[0].cursor, + }, + }; + }, }; /**