diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index b3928fea4c..0d4d1d4aea 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -4,7 +4,7 @@ import { getContextFactory as getContextFactoryVTEX, getResolvers as getResolversVTEX, } from './platforms/vtex' -import { typeDefs } from './typeDefs' +import { getTypeDefs } from './typeDefs' import type { Options as OptionsVTEX } from './platforms/vtex' export * from './__generated__/schema' @@ -18,7 +18,7 @@ const platforms = { }, } -export const getTypeDefs = () => typeDefs +export { getTypeDefs } from './typeDefs' export const getResolvers = (options: Options) => platforms[options.platform].getResolvers(options) @@ -29,5 +29,5 @@ export const getContextFactory = (options: Options) => export const getSchema = async (options: Options) => makeExecutableSchema({ resolvers: getResolvers(options), - typeDefs: getTypeDefs(), + typeDefs: getTypeDefs(options.platform), }) diff --git a/packages/api/src/platforms/vtex/clients/commerce/index.ts b/packages/api/src/platforms/vtex/clients/commerce/index.ts index 97c999d0d3..10850f5889 100644 --- a/packages/api/src/platforms/vtex/clients/commerce/index.ts +++ b/packages/api/src/platforms/vtex/clients/commerce/index.ts @@ -4,6 +4,7 @@ import type { Brand } from './types/Brand' import type { CategoryTree } from './types/CategoryTree' import type { OrderForm, OrderFormInputItem } from './types/OrderForm' import type { PortalPagetype } from './types/Portal' +import type { Region, RegionInput } from './types/Region' import type { Simulation, SimulationArgs, @@ -101,6 +102,17 @@ export const VtexCommerce = ( } ) }, + region: async ({ + postalCode, + country, + salesChannel, + }: RegionInput): Promise => { + return fetchAPI( + `${base}/api/checkout/pub/regions/?postalCode=${postalCode}&country=${country}&sc=${ + salesChannel ?? '' + }` + ) + }, }, } } diff --git a/packages/api/src/platforms/vtex/clients/commerce/types/Region.ts b/packages/api/src/platforms/vtex/clients/commerce/types/Region.ts new file mode 100644 index 0000000000..5f045854c1 --- /dev/null +++ b/packages/api/src/platforms/vtex/clients/commerce/types/Region.ts @@ -0,0 +1,7 @@ +export interface RegionInput { + postalCode: string + country: string + salesChannel?: number +} + +export type Region = Array<{ id: string }> diff --git a/packages/api/src/platforms/vtex/customTypeDefs/query.graphql b/packages/api/src/platforms/vtex/customTypeDefs/query.graphql new file mode 100644 index 0000000000..a60dfaa6ae --- /dev/null +++ b/packages/api/src/platforms/vtex/customTypeDefs/query.graphql @@ -0,0 +1,15 @@ +input RegionInput { + postalCode: String! + """ + Three letter country code according to ISO 3166-1 alpha 3. + """ + country: String! + """ + VTEX sales channel ID. + """ + salesChannel: Int +} + +extend type Query { + region(input: RegionInput): String +} diff --git a/packages/api/src/platforms/vtex/resolvers/query.ts b/packages/api/src/platforms/vtex/resolvers/query.ts index d4a962a514..78694df4ea 100644 --- a/packages/api/src/platforms/vtex/resolvers/query.ts +++ b/packages/api/src/platforms/vtex/resolvers/query.ts @@ -11,6 +11,7 @@ import type { } from '../../../__generated__/schema' import type { CategoryTree } from '../clients/commerce/types/CategoryTree' import type { Context } from '../index' +import type { RegionInput } from '../clients/commerce/types/Region' export const Query = { product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => { @@ -151,4 +152,21 @@ export const Query = { })), } }, + region: async ( + _: any, + { + input, + }: { + input: RegionInput + }, + { clients }: Context + ) => { + const data = await clients.commerce.checkout.region(input) + + if (data?.[0]?.id) { + return data[0].id + } + + return null + }, } diff --git a/packages/api/src/typeDefs/index.ts b/packages/api/src/typeDefs/index.ts index f441dbaea6..e8b045c764 100644 --- a/packages/api/src/typeDefs/index.ts +++ b/packages/api/src/typeDefs/index.ts @@ -21,29 +21,33 @@ import Seo from './seo.graphql' import Cart from './cart.graphql' import Status from './status.graphql' import PropertyValue from './propertyValue.graphql' +import VTEXQuery from '../platforms/vtex/customTypeDefs/query.graphql' +import type { Platform } from '../typings' -export const typeDefs = [ - Query, - Mutation, - Brand, - Breadcrumb, - Collection, - Facet, - Image, - PageInfo, - Product, - Seo, - Offer, - AggregateRating, - Review, - Author, - ProductGroup, - Organization, - AggregateOffer, - Order, - Cart, - Status, - PropertyValue, -] - .map(print) - .join('\n') +export const getTypeDefs = (platform?: Platform) => + [ + Query, + Mutation, + Brand, + Breadcrumb, + Collection, + Facet, + Image, + PageInfo, + Product, + Seo, + Offer, + AggregateRating, + Review, + Author, + ProductGroup, + Organization, + AggregateOffer, + Order, + Cart, + Status, + PropertyValue, + ] + .concat(platform === 'vtex' ? [VTEXQuery] : []) + .map(print) + .join('\n') diff --git a/packages/api/src/typings/index.ts b/packages/api/src/typings/index.ts new file mode 100644 index 0000000000..8dbe10cb8f --- /dev/null +++ b/packages/api/src/typings/index.ts @@ -0,0 +1 @@ +export type Platform = 'vtex'