Skip to content

Commit

Permalink
Add Region query
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbrasileiro committed Mar 17, 2022
1 parent fbc1c1c commit b1f7cb4
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 28 deletions.
6 changes: 3 additions & 3 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -18,7 +18,7 @@ const platforms = {
},
}

export const getTypeDefs = () => typeDefs
export { getTypeDefs } from './typeDefs'

export const getResolvers = (options: Options) =>
platforms[options.platform].getResolvers(options)
Expand All @@ -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),
})
12 changes: 12 additions & 0 deletions packages/api/src/platforms/vtex/clients/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -101,6 +102,17 @@ export const VtexCommerce = (
}
)
},
region: async ({
postalCode,
country,
salesChannel,
}: RegionInput): Promise<Region> => {
return fetchAPI(
`${base}/api/checkout/pub/regions/?postalCode=${postalCode}&country=${country}&sc=${
salesChannel ?? ''
}`
)
},
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface RegionInput {
postalCode: string
country: string
salesChannel?: number
}

export type Region = Array<{ id: string }>
15 changes: 15 additions & 0 deletions packages/api/src/platforms/vtex/customTypeDefs/query.graphql
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 18 additions & 0 deletions packages/api/src/platforms/vtex/resolvers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
},
}
54 changes: 29 additions & 25 deletions packages/api/src/typeDefs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
1 change: 1 addition & 0 deletions packages/api/src/typings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Platform = 'vtex'

0 comments on commit b1f7cb4

Please sign in to comment.