Skip to content

Commit dda085a

Browse files
authored
feat: reviews query resolver (#2649)
## What's the purpose of this pull request? To add reviews query resolver on graphQL ## How it works? It defines a few new types and adds a new query resolver called: `reviews` wich retrieves an array of reviews for a specific product. For input we have: - productId (`required`) - after and first for pagination, (both are `optional`) - sort (`optional`) - rating (`optional`)(for filtering) ## How to test it? run the api graphql server locally with the following command: ```bash yarn dev:server ``` and make a query call ## References [JIRA TASK: SFS-2094](https://vtex-dev.atlassian.net/browse/SFS-2094) ![image](https://github.com/user-attachments/assets/d07547af-329e-4dbe-8b83-75694ec18fbc) ![image](https://github.com/user-attachments/assets/f9bcef6d-359e-464e-893a-74f1ffc06464) ## Checklist <em>You may erase this after checking them all 😉</em> **PR Description** - [ ] Adds graphQL `Reviews` types - [ ] Creates a new query resolver for `Reviews`
1 parent 93262b4 commit dda085a

File tree

7 files changed

+211
-1
lines changed

7 files changed

+211
-1
lines changed

packages/api/src/__generated__/schema.ts

+63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/src/platforms/vtex/clients/commerce/types/ProductReview.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ export enum ProductReviewsInputOrderBy {
2626
locale = 'Locale',
2727
}
2828

29+
export type ProductReviewsInputOrderWay = 'asc' | 'desc'
30+
2931
export interface ProductReviewsInput {
3032
searchTerm?: string
3133
from?: number
3234
to?: number
3335
orderBy?: ProductReviewsInputOrderBy
34-
orderWay?: 'asc' | 'desc'
36+
orderWay?: ProductReviewsInputOrderWay
3537
status?: boolean
3638
productId?: string
3739
rating?: number

packages/api/src/platforms/vtex/resolvers/query.ts

+32
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ import type {
2121
QuerySellersArgs,
2222
QueryShippingArgs,
2323
QueryRedirectArgs,
24+
QueryReviewsArgs,
2425
} from '../../../__generated__/schema'
2526
import type { CategoryTree } from '../clients/commerce/types/CategoryTree'
2627
import type { Context } from '../index'
2728
import { isValidSkuId, pickBestSku } from '../utils/sku'
2829
import type { SearchArgs } from '../clients/search'
30+
import {
31+
ProductReviewsInputOrderBy,
32+
type ProductReviewsInputOrderWay,
33+
} from '../clients/commerce/types/ProductReview'
2934

3035
export const Query = {
3136
product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => {
@@ -335,4 +340,31 @@ export const Query = {
335340
sellers,
336341
}
337342
},
343+
reviews: async (
344+
_: unknown,
345+
{ productId, after, first, rating, sort }: QueryReviewsArgs,
346+
ctx: Context
347+
) => {
348+
const {
349+
clients: { commerce },
350+
} = ctx
351+
352+
const from = after ?? 0
353+
const to = from + (first ?? 6)
354+
355+
const [orderByKey, orderWay] = sort?.split('_') as [
356+
keyof typeof ProductReviewsInputOrderBy,
357+
ProductReviewsInputOrderWay,
358+
]
359+
360+
return await commerce.reviews.list({
361+
productId,
362+
from,
363+
to,
364+
orderBy: ProductReviewsInputOrderBy[orderByKey],
365+
orderWay,
366+
status: true,
367+
rating: rating ?? undefined,
368+
})
369+
},
338370
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
type StoreProductReview {
2+
"""
3+
Review ID.
4+
"""
5+
id: String!
6+
"""
7+
Product ID.
8+
"""
9+
productId: String!
10+
"""
11+
Review rating.
12+
"""
13+
rating: Int!
14+
"""
15+
Review title.
16+
"""
17+
title: String!
18+
"""
19+
Review content.
20+
"""
21+
text: String!
22+
"""
23+
Review author name.
24+
"""
25+
reviewerName: String
26+
"""
27+
Review author ID.
28+
"""
29+
shopperId: String!
30+
"""
31+
Review creation date.
32+
"""
33+
reviewDateTime: String!
34+
"""
35+
Indicates if the review was made by a verified purchaser.
36+
"""
37+
verifiedPurchaser: Boolean!
38+
"""
39+
Indicates if the review was approved by the store owner.
40+
"""
41+
approved: Boolean!
42+
}
43+
44+
type StoreProductListReviewsRange {
45+
"""
46+
Total number of reviews.
47+
"""
48+
total: Int!
49+
"""
50+
Index of the first review
51+
"""
52+
from: Int!
53+
"""
54+
Index of the last review
55+
"""
56+
to: Int!
57+
}
58+
59+
type StoreProductListReviewsResult {
60+
"""
61+
Array of product reviews.
62+
"""
63+
data: [StoreProductReview!]!
64+
range: StoreProductListReviewsRange!
65+
}
66+
67+
enum StoreProductListReviewsSort {
68+
"""
69+
Sort by review creation date, from newest to oldest.
70+
"""
71+
reviewDateTime_desc
72+
"""
73+
Sort by review creation date, from oldest to newest.
74+
"""
75+
reviewDateTime_asc
76+
"""
77+
Sort by review rating, from highest to lowest.
78+
"""
79+
rating_desc
80+
"""
81+
Sort by review rating, from lowest to highest.
82+
"""
83+
rating_asc
84+
}

packages/api/src/typeDefs/query.graphql

+27
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,33 @@ type Query {
338338
salesChannel: String
339339
): SellersData
340340
@cacheControl(scope: "public", sMaxAge: 120, staleWhileRevalidate: 3600)
341+
342+
"""
343+
Returns a list of approved reviews for a specific product.
344+
"""
345+
reviews(
346+
"""
347+
Product Id
348+
"""
349+
productId: String!
350+
"""
351+
Reviews results sorting mode
352+
"""
353+
sort: StoreProductListReviewsSort = reviewDateTime_desc
354+
"""
355+
Reviews pagination argument, indicating how many items should be returned from the complete result list.
356+
"""
357+
first: Int = 6
358+
"""
359+
Reviews pagination argument, indicating the cursor corresponding with the item after which the items should be fetched.
360+
"""
361+
after: Int = 0
362+
"""
363+
Rating filter
364+
"""
365+
rating: Int
366+
): StoreProductListReviewsResult
367+
@cacheControl(scope: "public", sMaxAge: 120, staleWhileRevalidate: 3600)
341368
}
342369

343370
"""

packages/api/test/schema.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const QUERIES = [
6767
'shipping',
6868
'redirect',
6969
'sellers',
70+
'reviews',
7071
]
7172

7273
const MUTATIONS = ['validateCart', 'validateSession', 'subscribeToNewsletter']

packages/core/test/server/index.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const QUERIES = [
7171
'shipping',
7272
'redirect',
7373
'sellers',
74+
'reviews',
7475
]
7576

7677
const MUTATIONS = ['validateCart', 'validateSession', 'subscribeToNewsletter']

0 commit comments

Comments
 (0)