diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..26b0c33 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,14 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +### Changed +- Renamed `review` query to `rating`. +- Renamed `reviews` query to `ratings`. + +### Added +- New query `reviews`. diff --git a/README.md b/README.md deleted file mode 100644 index ebb3ddb..0000000 --- a/README.md +++ /dev/null @@ -1,27 +0,0 @@ -# YourViews GraphQL resolvers - -To add your YourViews token you must create a file in VBase at the `secret_keys` bucket called `keys.json` with this format: - -``` -{ - "yourviewsToken": "", - "appId": "" -} -``` - -Example curl: - -``` -curl --request PUT \ - --url http://vbase.aws-us-east-1.vtex.io///buckets/vtex.yourviews-graphql/secret_keys/files/keys.json \ - --header 'authorization: ' \ - --header 'content-type: application/json' \ - --data '{ - "yourviewsToken": "", - "appId": "" -}' -``` - -To grab your vtex token, run on the terminal: - -`vtex local token` diff --git a/graphql/schema.graphql b/graphql/schema.graphql index 364908f..f89f644 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -1,19 +1,57 @@ -type Element { +type RatingElement { ProductId: Int, TotalRatings: Int, Rating: Float, } -type Review { +type Rating { HasErrors: Boolean, - Element: [Element], + Element: [RatingElement], + ErrorList: [String], + Total: Int, + CurrentPage: Int +} + +type Fields { + Name: String, + Values: [String] +} + +type User { + Name: String, + Email: String, + CPF: String, + City: String, + State: String, + UserId: String, + ExhibitionName: String, +} + +type ReviewElement { + ReviewId: Int, + Rating: Int, + Review: String, + Date: String, + Likes: Int, + Dislikes: Int, + CustomFields: [Fields], + User: User, + ReferenceOrder: String, + ReviewTitle: String, + BoughtProduct: Boolean +} + +type Reviews { + HasErrors: Boolean, + Element: [ReviewElement], ErrorList: [String], Total: Int, CurrentPage: Int } type Query { - review(productId: String): Review @withSecretKeys - reviews(productIds: [String!]): Review @withSecretKeys + rating(productId: String): Rating @withSecretKeys + ratings(productIds: [String!]): Rating @withSecretKeys + reviews(productId: String): Reviews @withSecretKeys } diff --git a/node/clients/yourviews.ts b/node/clients/yourviews.ts index a67f96a..9cd20da 100644 --- a/node/clients/yourviews.ts +++ b/node/clients/yourviews.ts @@ -14,23 +14,29 @@ export default class YourViewsClient extends ExternalClient { this.token = secretKeys.yourviewsToken } - public async getReview (productId: string): Promise { - return this.get(this.routes.review(productId), { - metric: 'yourviews-get-review', + public async getRating (productId: string): Promise { + return this.get(this.routes.rating(productId), { + metric: 'yourviews-get-rating', }) } - public async getReviews (productIds: string[]): Promise { - return this.get(this.routes.reviews(productIds), { + public async getRatings (productIds: string[]): Promise { + return this.get(this.routes.ratings(productIds), { + metric: 'yourviews-get-ratings', + }) + } + + public async getReviews (productId: string): Promise { + return this.get(this.routes.reviews(productId), { metric: 'yourviews-get-reviews', }) } private get routes () { return { - // Review endpoints - review: (productId: string) => `/api/${this.appId}/review/reviewshelf?productIds=${productId}`, - reviews: (productIds: string[]) => `/api/${this.appId}/review/reviewshelf?productIds=${productIds.join(',')}`, + rating: (productId: string) => `/api/${this.appId}/review/reviewshelf?productIds=${productId}`, + ratings: (productIds: string[]) => `/api/${this.appId}/review/reviewshelf?productIds=${productIds.join(',')}`, + reviews: (productId: string) => `/api/${this.appId}/review?productId=${productId}`, } } diff --git a/node/package.json b/node/package.json index 954b01d..241b0bb 100644 --- a/node/package.json +++ b/node/package.json @@ -24,10 +24,10 @@ "@vtex/api": "^3.8.1", "tslint": "^5.12.0", "tslint-config-vtex": "^2.1.0", - "typescript": "^3.4.2" + "typescript": "3.7.3" }, "scripts": { "lint": "tsc --noEmit && tslint --fix -c tslint.json './**/*.ts'" }, - "version": "0.0.4" + "version": "1.0.0" } diff --git a/node/resolvers/review/index.ts b/node/resolvers/review/index.ts index c142c8c..169517d 100644 --- a/node/resolvers/review/index.ts +++ b/node/resolvers/review/index.ts @@ -1,9 +1,11 @@ import { - review, + rating, + ratings, reviews, } from './yourviewsResolver' export const queries = { - review, + rating, + ratings, reviews, } diff --git a/node/resolvers/review/yourviewsResolver.ts b/node/resolvers/review/yourviewsResolver.ts index 3053169..9e6a67c 100644 --- a/node/resolvers/review/yourviewsResolver.ts +++ b/node/resolvers/review/yourviewsResolver.ts @@ -1,18 +1,22 @@ -interface ReviewArgs { +interface SingleProductArgs { productId: string } -export const review = async (_: any, args: ReviewArgs, ctx: Context) => { - const data = await ctx.clients.yourviews.getReview(args.productId) - console.log('data', data) +export const rating = async (_: any, args: SingleProductArgs, ctx: Context) => { + const data = await ctx.clients.yourviews.getRating(args.productId) return data } -interface ReviewsArgs { +interface MultipleProductsArg { productIds: string[] } -export const reviews = async (_: any, args: ReviewsArgs, ctx: Context) => { - const data = await ctx.clients.yourviews.getReviews(args.productIds) +export const ratings = async (_: any, args: MultipleProductsArg, ctx: Context) => { + const data = await ctx.clients.yourviews.getRatings(args.productIds) + return data +} + +export const reviews = async (_: any, args: SingleProductArgs, ctx: Context) => { + const data = await ctx.clients.yourviews.getReviews(args.productId) return data }