Skip to content

Commit

Permalink
Moving session from query to resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbrasileiro committed Mar 23, 2022
1 parent 2a80fcb commit 2fb1863
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 38 deletions.
12 changes: 6 additions & 6 deletions packages/api/src/__generated__/schema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/api/src/platforms/vtex/resolvers/mutation.ts
@@ -1,5 +1,7 @@
import { validateCart } from './validateCart'
import session from './session'

export const Mutation = {
validateCart,
session,
}
18 changes: 0 additions & 18 deletions packages/api/src/platforms/vtex/resolvers/query.ts
Expand Up @@ -8,12 +8,9 @@ import type {
QueryAllProductsArgs,
QuerySearchArgs,
QueryCollectionArgs,
QuerySessionArgs,
StoreSession,
} from '../../../__generated__/schema'
import type { CategoryTree } from '../clients/commerce/types/CategoryTree'
import type { Context } from '../index'
import ChannelParser from '../utils/channel'

export const Query = {
product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => {
Expand Down Expand Up @@ -171,19 +168,4 @@ export const Query = {
familyName: profile?.lastName?.value ?? '',
}
},
session: async (
_: any,
{ session }: QuerySessionArgs,
{ clients }: Context
): Promise<StoreSession> => {
const channelParser = new ChannelParser(session?.channel ?? '')
const regionData = await clients.commerce.checkout.region({
postalCode: String(session.postalCode ?? '').replace(/\D/g, ''),
country: session.country ?? '',
})

channelParser.updateChannel({ regionId: regionData?.[0]?.id })

return { ...session, channel: channelParser.stringify() }
},
}
19 changes: 19 additions & 0 deletions packages/api/src/platforms/vtex/resolvers/session.ts
@@ -0,0 +1,19 @@
import type { Context } from '..'
import type { MutationSessionArgs } from '../../../__generated__/schema'
import ChannelParser from '../utils/channel'

export default async function sessionResolvers(
_: any,
{ session }: MutationSessionArgs,
{ clients }: Context
) {
const channelParser = new ChannelParser(session?.channel ?? '')
const regionData = await clients.commerce.checkout.region({
postalCode: String(session.postalCode ?? '').replace(/\D/g, ''),
country: session.country ?? '',
})

channelParser.updateChannel({ regionId: regionData?.[0]?.id })

return { ...session, channel: channelParser.stringify() }
}
14 changes: 14 additions & 0 deletions packages/api/src/typeDefs/mutation.graphql
@@ -1,4 +1,18 @@
type StoreSession {
channel: String
country: String
postalCode: String
}

input IStoreSession {
channel: String
country: String
postalCode: String
}

type Mutation {
# Returns the order if anything changed with the order. Null if the order is valid
validateCart(cart: IStoreCart!): StoreCart

session(session: IStoreSession!): StoreSession!
}
14 changes: 0 additions & 14 deletions packages/api/src/typeDefs/query.graphql
Expand Up @@ -51,18 +51,6 @@ type StorePerson {
familyName: String!
}

type StoreSession {
channel: String
country: String
postalCode: String
}

input IStoreSession {
channel: String
country: String
postalCode: String
}

type Query {
product(locator: [IStoreSelectedFacet!]!): StoreProduct!

Expand All @@ -81,6 +69,4 @@ type Query {
allCollections(first: Int!, after: String): StoreCollectionConnection!

person: StorePerson!

session(session: IStoreSession!): StoreSession!
}

0 comments on commit 2fb1863

Please sign in to comment.