From 221f4a37222fa9619a222e06b58c7a14615ef644 Mon Sep 17 00:00:00 2001 From: igorbrasileiro Date: Thu, 17 Mar 2022 11:29:09 -0300 Subject: [PATCH] Use region query from faststore --- .eslintrc | 2 +- @generated/graphql/index.ts | 310 ++++++++---------- CHANGELOG.md | 1 - package.json | 2 +- .../common/PostalCode/PostalCodeInput.tsx | 2 +- src/server/index.js | 18 +- src/server/platforms/vtex.js | 47 --- yarn.lock | 7 +- 8 files changed, 144 insertions(+), 245 deletions(-) delete mode 100644 src/server/platforms/vtex.js diff --git a/.eslintrc b/.eslintrc index aaea76e53..64316bb74 100644 --- a/.eslintrc +++ b/.eslintrc @@ -4,5 +4,5 @@ "rules": { "react/prop-types": "off" }, - "ignorePatterns": ["static/~partytown/*", "src/server/platforms/vtex.js"] + "ignorePatterns": ["static/~partytown/*"] } diff --git a/@generated/graphql/index.ts b/@generated/graphql/index.ts index 979da390f..5466e7cce 100644 --- a/@generated/graphql/index.ts +++ b/@generated/graphql/index.ts @@ -54,9 +54,9 @@ export type Directory = Node & { atime: Scalars['Date'] atimeMs: Scalars['Float'] base: Scalars['String'] - birthTime: Scalars['Date'] /** @deprecated Use `birthTime` instead */ birthtime: Maybe + birthTime: Scalars['Date'] /** @deprecated Use `birthTime` instead */ birthtimeMs: Maybe changeTime: Scalars['Date'] @@ -183,8 +183,8 @@ export type DirectoryFieldsEnum = | 'atime' | 'atimeMs' | 'base' - | 'birthTime' | 'birthtime' + | 'birthTime' | 'birthtimeMs' | 'changeTime' | 'children' @@ -302,8 +302,8 @@ export type DirectoryFilterInput = { atime: InputMaybe atimeMs: InputMaybe base: InputMaybe - birthTime: InputMaybe birthtime: InputMaybe + birthTime: InputMaybe birthtimeMs: InputMaybe changeTime: InputMaybe children: InputMaybe @@ -381,9 +381,9 @@ export type File = Node & { atime: Scalars['Date'] atimeMs: Scalars['Float'] base: Scalars['String'] - birthTime: Scalars['Date'] /** @deprecated Use `birthTime` instead */ birthtime: Maybe + birthTime: Scalars['Date'] /** @deprecated Use `birthTime` instead */ birthtimeMs: Maybe changeTime: Scalars['Date'] @@ -510,8 +510,8 @@ export type FileFieldsEnum = | 'atime' | 'atimeMs' | 'base' - | 'birthTime' | 'birthtime' + | 'birthTime' | 'birthtimeMs' | 'changeTime' | 'children' @@ -629,8 +629,8 @@ export type FileFilterInput = { atime: InputMaybe atimeMs: InputMaybe base: InputMaybe - birthTime: InputMaybe birthtime: InputMaybe + birthTime: InputMaybe birthtimeMs: InputMaybe changeTime: InputMaybe children: InputMaybe @@ -729,6 +729,39 @@ export type GatsbyImagePlaceholder = | 'NONE' | 'TRACED_SVG' +export type Internal = { + content: Maybe + contentDigest: Scalars['String'] + description: Maybe + fieldOwners: Maybe>> + ignoreType: Maybe + mediaType: Maybe + owner: Scalars['String'] + type: Scalars['String'] +} + +export type InternalFilterInput = { + content: InputMaybe + contentDigest: InputMaybe + description: InputMaybe + fieldOwners: InputMaybe + ignoreType: InputMaybe + mediaType: InputMaybe + owner: InputMaybe + type: InputMaybe +} + +export type IntQueryOperatorInput = { + eq: InputMaybe + gt: InputMaybe + gte: InputMaybe + in: InputMaybe>> + lt: InputMaybe + lte: InputMaybe + ne: InputMaybe + nin: InputMaybe>> +} + export type IStoreCart = { order: IStoreOrder } @@ -766,39 +799,6 @@ export type IStoreSelectedFacet = { value: Scalars['String'] } -export type IntQueryOperatorInput = { - eq: InputMaybe - gt: InputMaybe - gte: InputMaybe - in: InputMaybe>> - lt: InputMaybe - lte: InputMaybe - ne: InputMaybe - nin: InputMaybe>> -} - -export type Internal = { - content: Maybe - contentDigest: Scalars['String'] - description: Maybe - fieldOwners: Maybe>> - ignoreType: Maybe - mediaType: Maybe - owner: Scalars['String'] - type: Scalars['String'] -} - -export type InternalFilterInput = { - content: InputMaybe - contentDigest: InputMaybe - description: InputMaybe - fieldOwners: InputMaybe - ignoreType: InputMaybe - mediaType: InputMaybe - owner: InputMaybe - type: InputMaybe -} - export type Mutation = { validateCart: Maybe } @@ -946,8 +946,8 @@ export type QueryDirectoryArgs = { atime: InputMaybe atimeMs: InputMaybe base: InputMaybe - birthTime: InputMaybe birthtime: InputMaybe + birthTime: InputMaybe birthtimeMs: InputMaybe changeTime: InputMaybe children: InputMaybe @@ -984,8 +984,8 @@ export type QueryFileArgs = { atime: InputMaybe atimeMs: InputMaybe base: InputMaybe - birthTime: InputMaybe birthtime: InputMaybe + birthTime: InputMaybe birthtimeMs: InputMaybe changeTime: InputMaybe children: InputMaybe @@ -1142,6 +1142,7 @@ export type RegionInput = { /** Three letter country code according to ISO 3166-1 alpha 3. */ country: Scalars['String'] postalCode: Scalars['String'] + /** VTEX sales channel ID. */ salesChannel: InputMaybe } @@ -1831,12 +1832,6 @@ export type SitePageContext = { slug: Maybe } -export type SitePageContextFilterInput = { - _xparams: InputMaybe - id: InputMaybe - slug: InputMaybe -} - export type SitePageContext_Xparams = { slug: Maybe } @@ -1845,6 +1840,12 @@ export type SitePageContext_XparamsFilterInput = { slug: InputMaybe } +export type SitePageContextFilterInput = { + _xparams: InputMaybe + id: InputMaybe + slug: InputMaybe +} + export type SitePageEdge = { next: Maybe node: SitePage @@ -1897,7 +1898,6 @@ export type SitePageFieldsEnum = | 'context___id' | 'context___slug' | 'id' - | 'internalComponentName' | 'internal___content' | 'internal___contentDigest' | 'internal___description' @@ -1906,6 +1906,7 @@ export type SitePageFieldsEnum = | 'internal___mediaType' | 'internal___owner' | 'internal___type' + | 'internalComponentName' | 'isCreatedByStatefulCreatePages' | 'matchPath' | 'parent___children' @@ -1947,7 +1948,6 @@ export type SitePageFieldsEnum = | 'parent___parent___parent___children' | 'parent___parent___parent___id' | 'path' - | 'pluginCreatorId' | 'pluginCreator___browserAPIs' | 'pluginCreator___children' | 'pluginCreator___children___children' @@ -2046,6 +2046,7 @@ export type SitePageFieldsEnum = | 'pluginCreator___resolve' | 'pluginCreator___ssrAPIs' | 'pluginCreator___version' + | 'pluginCreatorId' export type SitePageFilterInput = { children: InputMaybe @@ -3558,7 +3559,7 @@ export type RegionQueryQueryVariables = Exact<{ input: InputMaybe }> -export type RegionQueryQuery = { region: string | null | undefined } +export type RegionQueryQuery = { region: string | null } export type ProductSummary_ProductFragment = { slug: string @@ -3668,19 +3669,13 @@ export type ProductGalleryQueryQuery = { export type HomePageQueryQueryVariables = Exact<{ [key: string]: never }> export type HomePageQueryQuery = { - site: - | { - siteMetadata: - | { - title: string | null | undefined - description: string | null | undefined - titleTemplate: string | null | undefined - } - | null - | undefined - } - | null - | undefined + site: { + siteMetadata: { + title: string | null + description: string | null + titleTemplate: string | null + } | null + } | null allStoreProduct: { nodes: Array<{ slug: string @@ -3708,19 +3703,13 @@ export type HomePageQueryQuery = { export type SearchPageQueryQueryVariables = Exact<{ [key: string]: never }> export type SearchPageQueryQuery = { - site: - | { - siteMetadata: - | { - titleTemplate: string | null | undefined - title: string | null | undefined - description: string | null | undefined - } - | null - | undefined - } - | null - | undefined + site: { + siteMetadata: { + titleTemplate: string | null + title: string | null + description: string | null + } | null + } | null } export type CollectionPageQueryQueryVariables = Exact<{ @@ -3728,33 +3717,20 @@ export type CollectionPageQueryQueryVariables = Exact<{ }> export type CollectionPageQueryQuery = { - site: - | { - siteMetadata: - | { - titleTemplate: string | null | undefined - title: string | null | undefined - description: string | null | undefined - } - | null - | undefined - } - | null - | undefined - collection: - | { - seo: { title: string; description: string } - breadcrumbList: { - itemListElement: Array<{ - item: string - name: string - position: number - }> - } - meta: { selectedFacets: Array<{ key: string; value: string }> } - } - | null - | undefined + site: { + siteMetadata: { + titleTemplate: string | null + title: string | null + description: string | null + } | null + } | null + collection: { + seo: { title: string; description: string } + breadcrumbList: { + itemListElement: Array<{ item: string; name: string; position: number }> + } + meta: { selectedFacets: Array<{ key: string; value: string }> } + } | null allStoreProduct: { nodes: Array<{ slug: string @@ -3784,56 +3760,43 @@ export type ProductPageQueryQueryVariables = Exact<{ }> export type ProductPageQueryQuery = { - site: - | { - siteMetadata: - | { - title: string | null | undefined - description: string | null | undefined - titleTemplate: string | null | undefined - siteUrl: string | null | undefined - } - | null - | undefined - } - | null - | undefined - product: - | { - slug: string - sku: string - gtin: string - name: string - description: string - id: string - seo: { title: string; description: string } - brand: { name: string } - breadcrumbList: { - itemListElement: Array<{ - item: string - name: string - position: number - }> - } - image: Array<{ url: string; alternateName: string }> - offers: { - lowPrice: number - highPrice: number - priceCurrency: string - offers: Array<{ - availability: string - price: number - priceValidUntil: string - priceCurrency: string - itemCondition: string - listPrice: number - seller: { identifier: string } - }> - } - isVariantOf: { productGroupID: string; name: string } - } - | null - | undefined + site: { + siteMetadata: { + title: string | null + description: string | null + titleTemplate: string | null + siteUrl: string | null + } | null + } | null + product: { + slug: string + sku: string + gtin: string + name: string + description: string + id: string + seo: { title: string; description: string } + brand: { name: string } + breadcrumbList: { + itemListElement: Array<{ item: string; name: string; position: number }> + } + image: Array<{ url: string; alternateName: string }> + offers: { + lowPrice: number + highPrice: number + priceCurrency: string + offers: Array<{ + availability: string + price: number + priceValidUntil: string + priceCurrency: string + itemCondition: string + listPrice: number + seller: { identifier: string } + }> + } + isVariantOf: { productGroupID: string; name: string } + } | null allStoreProduct: { nodes: Array<{ slug: string @@ -3863,29 +3826,26 @@ export type ValidateCartMutationMutationVariables = Exact<{ }> export type ValidateCartMutationMutation = { - validateCart: - | { - order: { - orderNumber: string - acceptedOffer: Array<{ - quantity: number - price: number - listPrice: number - seller: { identifier: string } - itemOffered: { - sku: string - name: string - gtin: string - image: Array<{ url: string; alternateName: string }> - brand: { name: string } - isVariantOf: { productGroupID: string; name: string } - } - }> + validateCart: { + order: { + orderNumber: string + acceptedOffer: Array<{ + quantity: number + price: number + listPrice: number + seller: { identifier: string } + itemOffered: { + sku: string + name: string + gtin: string + image: Array<{ url: string; alternateName: string }> + brand: { name: string } + isVariantOf: { productGroupID: string; name: string } } - messages: Array<{ text: string; status: StoreStatus }> - } - | null - | undefined + }> + } + messages: Array<{ text: string; status: StoreStatus }> + } | null } export type CartMessageFragment = { text: string; status: StoreStatus } diff --git a/CHANGELOG.md b/CHANGELOG.md index 0af9c38eb..f6d13b93f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `EmptyState` at the `ProductGallery` section. - `IconSVG` component to load SVG Icons. - `Suggestions` component. -- `Region` query for VTEX plataform. - Query region when user enter a new postal code. ### Changed diff --git a/package.json b/package.json index 014abd398..48d4e7332 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "@envelop/graphql-jit": "^1.1.1", "@envelop/parser-cache": "^2.2.0", "@envelop/validation-cache": "^2.2.0", - "@faststore/api": "^1.6.7", + "@faststore/api": "https://pkg.csb.dev/vtex/faststore/commit/558136c9/@faststore/api", "@faststore/sdk": "^1.6.7", "@faststore/ui": "^1.6.7", "@graphql-tools/schema": "^8.3.3", diff --git a/src/components/common/PostalCode/PostalCodeInput.tsx b/src/components/common/PostalCode/PostalCodeInput.tsx index d2ad13ba3..845624899 100644 --- a/src/components/common/PostalCode/PostalCodeInput.tsx +++ b/src/components/common/PostalCode/PostalCodeInput.tsx @@ -39,7 +39,7 @@ export default function PostalCodeInput() { setPostalCode(value) request(RegionQuery, { input: { - postalCode: value.replace('-', ''), + postalCode: value, country: country ?? 'BRA', salesChannel: null, }, diff --git a/src/server/index.js b/src/server/index.js index baa65622b..ca61f37d4 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -3,18 +3,16 @@ const { envelop, useExtendContext, useMaskedErrors, - useSchema, + useAsyncSchema, } = require('@envelop/core') const { useGraphQlJit } = require('@envelop/graphql-jit') const { useParserCache } = require('@envelop/parser-cache') const { useValidationCache } = require('@envelop/validation-cache') const { getContextFactory, getSchema } = require('@faststore/api') const { GraphQLError } = require('graphql') -const { mergeSchemas } = require('@graphql-tools/schema') const persisted = require('../../@generated/graphql/persisted.json') const storeConfig = require('../../store.config') -const { getSchema: getVTEXSchema } = require('./platforms/vtex') const persistedQueries = new Map(Object.entries(persisted)) @@ -27,16 +25,6 @@ const apiOptions = { const apiSchema = getSchema(apiOptions) -const getMergedSchemas = async () => { - const schemas = [await apiSchema] - - if (storeConfig.platform === 'vtex') { - schemas.push(getVTEXSchema()) - } - - return mergeSchemas({ schemas }) -} - const apiContextFactory = getContextFactory(apiOptions) const isBadRequestError = (err) => { @@ -56,7 +44,7 @@ const formatError = (err) => { const getEnvelop = async () => envelop({ plugins: [ - useSchema(await getMergedSchemas()), + useAsyncSchema(apiSchema), useExtendContext(apiContextFactory), useMaskedErrors({ formatError }), useGraphQlJit(), @@ -94,6 +82,6 @@ const execute = async (options, envelopContext = {}) => { module.exports = { execute, - getSchema: getMergedSchemas, + getSchema: () => apiSchema, getContextFactory: () => apiContextFactory, } diff --git a/src/server/platforms/vtex.js b/src/server/platforms/vtex.js deleted file mode 100644 index 57a802202..000000000 --- a/src/server/platforms/vtex.js +++ /dev/null @@ -1,47 +0,0 @@ -const { makeExecutableSchema } = require('@graphql-tools/schema') -const storeConfig = require('../../../store.config') - -const fetch = (...args) => - import('node-fetch').then(({ default: defaultFetch }) => - defaultFetch(...args) - ) - -const account = storeConfig.api.storeId -const environment = storeConfig.api.environment - -const typeDefs = ` - input RegionInput { - postalCode: String! - """ - Three letter country code according to ISO 3166-1 alpha 3. - """ - country: String! - salesChannel: Int - } - - type Query { - region(input: RegionInput): String - } -` - -const resolvers = { - Query: { - region: async (_, { input: { postalCode, country, salesChannel } }) => { - const data = await fetch( - `https://${account}.${environment}.com.br/api/checkout/pub/regions/?postalCode=${postalCode}&country=${country}&sc=${ - salesChannel ?? '' - }` - ).then((res) => res.json()) - - if (data?.[0]?.id) { - return data[0].id - } - - return null - }, - }, -} - -const getSchema = () => makeExecutableSchema({ typeDefs, resolvers }) - -module.exports = { getSchema } diff --git a/yarn.lock b/yarn.lock index 0c345ffed..97ccbca3c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1472,10 +1472,9 @@ minimatch "^3.0.4" strip-json-comments "^3.1.1" -"@faststore/api@^1.6.7": - version "1.6.7" - resolved "https://registry.yarnpkg.com/@faststore/api/-/api-1.6.7.tgz#8bc76ab1b8c57539f985c310d0419cc0b879daf2" - integrity sha512-q4DTn1f874wuClZsKbe3d81pHaoLyL1rjh3ye//4NmIVGHM5mO2N9ex7+RS+p4JieQDQ7/lylnCiPJ+TKUF5oA== +"@faststore/api@https://pkg.csb.dev/vtex/faststore/commit/558136c9/@faststore/api": + version "1.6.10" + resolved "https://pkg.csb.dev/vtex/faststore/commit/558136c9/@faststore/api#212909be9897b12f831d709d30941cb05299c7c4" dependencies: "@graphql-tools/schema" "^8.2.0" "@rollup/plugin-graphql" "^1.0.0"