Skip to content

Commit

Permalink
refactor(core): use gql.tada on mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
deini committed Mar 15, 2024
1 parent be5fc87 commit ac1443f
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 73 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-cougars-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": minor
---

use gql.tada on all mutations
9 changes: 4 additions & 5 deletions apps/core/client/mutations/add-cart-line-item.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getSessionCustomerId } from '~/auth';

import { client } from '..';
import { graphql } from '../generated';
import { AddCartLineItemsDataInput } from '../generated/graphql';
import { graphql } from '../graphql';

export const ADD_TO_CART_LINE_ITEM_MUTATION = /* GraphQL */ `
const ADD_TO_CART_LINE_ITEM_MUTATION = graphql(`
mutation AddCartLineItem($input: AddCartLineItemsInput!) {
cart {
addCartLineItems(input: $input) {
Expand All @@ -14,14 +14,13 @@ export const ADD_TO_CART_LINE_ITEM_MUTATION = /* GraphQL */ `
}
}
}
`;
`);

export const addCartLineItem = async (cartEntityId: string, data: AddCartLineItemsDataInput) => {
const mutation = graphql(ADD_TO_CART_LINE_ITEM_MUTATION);
const customerId = await getSessionCustomerId();

const response = await client.fetch({
document: mutation,
document: ADD_TO_CART_LINE_ITEM_MUTATION,
variables: { input: { cartEntityId, data } },
customerId,
fetchOptions: { cache: 'no-store' },
Expand Down
10 changes: 4 additions & 6 deletions apps/core/client/mutations/add-checkout-shipping-info.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { client } from '..';
import { graphql } from '../generated';
import { CheckoutAddressInput } from '../generated/graphql';
import { graphql } from '../graphql';

export const ADD_CHECKOUT_SHIPPING_INFO_MUTATION = /* GraphQL */ `
const ADD_CHECKOUT_SHIPPING_INFO_MUTATION = graphql(`
mutation AddCheckoutShippingInfo($input: AddCheckoutShippingConsignmentsInput!) {
checkout {
addCheckoutShippingConsignments(input: $input) {
Expand Down Expand Up @@ -41,7 +41,7 @@ export const ADD_CHECKOUT_SHIPPING_INFO_MUTATION = /* GraphQL */ `
}
}
}
`;
`);

interface AddCheckoutShippingInfoProps {
cartId: string;
Expand All @@ -62,10 +62,8 @@ export const addCheckoutShippingInfo = async ({
cartItems,
shouldSaveAddress = false,
}: AddCheckoutShippingInfoProps) => {
const mutation = graphql(ADD_CHECKOUT_SHIPPING_INFO_MUTATION);

const response = await client.fetch({
document: mutation,
document: ADD_CHECKOUT_SHIPPING_INFO_MUTATION,
variables: {
input: {
checkoutEntityId: cartId,
Expand Down
10 changes: 4 additions & 6 deletions apps/core/client/mutations/assign-cart-to-customer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { client } from '..';
import { graphql } from '../generated';
import { AssignCartToCustomerInput } from '../generated/graphql';
import { graphql } from '../graphql';

export const ASSIGN_CART_TO_CUSTOMER_MUTATION = /* GraphQL */ `
const ASSIGN_CART_TO_CUSTOMER_MUTATION = graphql(`
mutation AssignCartToCustomer($assignCartToCustomerInput: AssignCartToCustomerInput!) {
cart {
assignCartToCustomer(input: $assignCartToCustomerInput) {
Expand All @@ -12,16 +12,14 @@ export const ASSIGN_CART_TO_CUSTOMER_MUTATION = /* GraphQL */ `
}
}
}
`;
`);

export const assignCartToCustomer = async (
customerId: string,
cartEntityId: AssignCartToCustomerInput['cartEntityId'],
) => {
const mutation = graphql(ASSIGN_CART_TO_CUSTOMER_MUTATION);

const response = await client.fetch({
document: mutation,
document: ASSIGN_CART_TO_CUSTOMER_MUTATION,
variables: {
assignCartToCustomerInput: {
cartEntityId,
Expand Down
9 changes: 4 additions & 5 deletions apps/core/client/mutations/create-cart.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getSessionCustomerId } from '~/auth';

import { client } from '..';
import { graphql } from '../generated';
import { CreateCartInput } from '../generated/graphql';
import { graphql } from '../graphql';

export const CREATE_CART_MUTATION = /* GraphQL */ `
const CREATE_CART_MUTATION = graphql(`
mutation CreateCart($createCartInput: CreateCartInput!) {
cart {
createCart(input: $createCartInput) {
Expand All @@ -14,14 +14,13 @@ export const CREATE_CART_MUTATION = /* GraphQL */ `
}
}
}
`;
`);

export const createCart = async (cartItems: CreateCartInput['lineItems']) => {
const mutation = graphql(CREATE_CART_MUTATION);
const customerId = await getSessionCustomerId();

const response = await client.fetch({
document: mutation,
document: CREATE_CART_MUTATION,
variables: {
createCartInput: {
lineItems: cartItems,
Expand Down
9 changes: 4 additions & 5 deletions apps/core/client/mutations/delete-cart-line-item.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getSessionCustomerId } from '~/auth';

import { client } from '..';
import { graphql } from '../generated';
import { graphql } from '../graphql';

export const DELETE_CART_LINE_ITEM = /* GraphQL */ `
const DELETE_CART_LINE_ITEM = graphql(`
mutation DeleteCartLineItem($input: DeleteCartLineItemInput!) {
cart {
deleteCartLineItem(input: $input) {
Expand All @@ -13,14 +13,13 @@ export const DELETE_CART_LINE_ITEM = /* GraphQL */ `
}
}
}
`;
`);

export const deleteCartLineItem = async (cartEntityId: string, lineItemEntityId: string) => {
const mutation = graphql(DELETE_CART_LINE_ITEM);
const customerId = await getSessionCustomerId();

const response = await client.fetch({
document: mutation,
document: DELETE_CART_LINE_ITEM,
variables: {
input: {
cartEntityId,
Expand Down
10 changes: 4 additions & 6 deletions apps/core/client/mutations/login.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { client } from '..';
import { graphql } from '../generated';
import { graphql } from '../graphql';

export const LOGIN_MUTATION = /* GraphQL */ `
const LOGIN_MUTATION = graphql(`
mutation Login($email: String!, $password: String!) {
login(email: $email, password: $password) {
customer {
entityId
}
}
}
`;
`);

export const login = async (email: string, password: string) => {
const mutation = graphql(LOGIN_MUTATION);

const response = await client.fetch({
document: mutation,
document: LOGIN_MUTATION,
variables: { email, password },
});

Expand Down
10 changes: 4 additions & 6 deletions apps/core/client/mutations/select-checkout-shipping-option.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { client } from '..';
import { graphql } from '../generated';
import { graphql } from '../graphql';

export const SELECT_CHECKOUT_SHIPPING_OPTION_MUTATION = /* GraphQL */ `
const SELECT_CHECKOUT_SHIPPING_OPTION_MUTATION = graphql(`
mutation SelectCheckoutShippingOption($input: SelectCheckoutShippingOptionInput!) {
checkout {
selectCheckoutShippingOption(input: $input) {
Expand Down Expand Up @@ -31,7 +31,7 @@ export const SELECT_CHECKOUT_SHIPPING_OPTION_MUTATION = /* GraphQL */ `
}
}
}
`;
`);

export const selectCheckoutShippingOption = async ({
checkoutEntityId,
Expand All @@ -42,10 +42,8 @@ export const selectCheckoutShippingOption = async ({
consignmentEntityId: string;
shippingOptionEntityId: string;
}) => {
const mutation = graphql(SELECT_CHECKOUT_SHIPPING_OPTION_MUTATION);

const response = await client.fetch({
document: mutation,
document: SELECT_CHECKOUT_SHIPPING_OPTION_MUTATION,
variables: {
input: {
checkoutEntityId,
Expand Down
10 changes: 4 additions & 6 deletions apps/core/client/mutations/submit-change-password.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'zod';

import { client } from '..';
import { graphql } from '../generated';
import { graphql } from '../graphql';

export const ChangePasswordSchema = z.object({
newPassword: z.string(),
Expand All @@ -14,7 +14,7 @@ interface SubmitChangePassword {
customerEntityId: number;
}

const SUBMIT_CHANGE_PASSWORD_MUTATION = /* GraphQL */ `
const SUBMIT_CHANGE_PASSWORD_MUTATION = graphql(`
mutation ChangePassword($input: ResetPasswordInput!) {
customer {
resetPassword(input: $input) {
Expand All @@ -28,15 +28,13 @@ const SUBMIT_CHANGE_PASSWORD_MUTATION = /* GraphQL */ `
}
}
}
`;
`);

export const submitChangePassword = async ({
newPassword,
token,
customerEntityId,
}: SubmitChangePassword) => {
const mutation = graphql(SUBMIT_CHANGE_PASSWORD_MUTATION);

const variables = {
input: {
token,
Expand All @@ -46,7 +44,7 @@ export const submitChangePassword = async ({
};

const response = await client.fetch({
document: mutation,
document: SUBMIT_CHANGE_PASSWORD_MUTATION,
variables,
});

Expand Down
10 changes: 4 additions & 6 deletions apps/core/client/mutations/submit-contact-form.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'zod';

import { client } from '..';
import { graphql } from '../generated';
import { graphql } from '../graphql';

export const ContactUsSchema = z.object({
companyName: z.string().optional(),
Expand All @@ -19,7 +19,7 @@ interface SubmitContactForm {
reCaptchaToken?: string;
}

const SUBMIT_CONTACT_FORM_MUTATION = /* GraphQL */ `
const SUBMIT_CONTACT_FORM_MUTATION = graphql(`
mutation submitContactUs($input: SubmitContactUsInput!, $reCaptchaV2: ReCaptchaV2Input) {
submitContactUs(input: $input, reCaptchaV2: $reCaptchaV2) {
__typename
Expand All @@ -31,15 +31,13 @@ const SUBMIT_CONTACT_FORM_MUTATION = /* GraphQL */ `
}
}
}
`;
`);

export const submitContactForm = async ({
formFields,
pageEntityId,
reCaptchaToken,
}: SubmitContactForm) => {
const mutation = graphql(SUBMIT_CONTACT_FORM_MUTATION);

const variables = {
input: {
pageEntityId,
Expand All @@ -49,7 +47,7 @@ export const submitContactForm = async ({
};

const response = await client.fetch({
document: mutation,
document: SUBMIT_CONTACT_FORM_MUTATION,
variables,
});

Expand Down
10 changes: 4 additions & 6 deletions apps/core/client/mutations/submit-reset-password.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'zod';

import { client } from '..';
import { graphql } from '../generated';
import { graphql } from '../graphql';

export const ResetPasswordSchema = z.object({
email: z.string().email(),
Expand All @@ -11,7 +11,7 @@ type SubmitResetPassword = z.infer<typeof ResetPasswordSchema> & {
reCaptchaToken?: string;
};

const SUBMIT_RESET_PASSWORD_MUTATION = /* GraphQL */ `
const SUBMIT_RESET_PASSWORD_MUTATION = graphql(`
mutation ResetPassword($input: RequestResetPasswordInput!, $reCaptcha: ReCaptchaV2Input) {
customer {
requestResetPassword(input: $input, reCaptchaV2: $reCaptcha) {
Expand All @@ -25,11 +25,9 @@ const SUBMIT_RESET_PASSWORD_MUTATION = /* GraphQL */ `
}
}
}
`;
`);

export const submitResetPassword = async ({ email, reCaptchaToken }: SubmitResetPassword) => {
const mutation = graphql(SUBMIT_RESET_PASSWORD_MUTATION);

const variables = {
input: {
email,
Expand All @@ -38,7 +36,7 @@ export const submitResetPassword = async ({ email, reCaptchaToken }: SubmitReset
};

const response = await client.fetch({
document: mutation,
document: SUBMIT_RESET_PASSWORD_MUTATION,
variables,
});

Expand Down
9 changes: 4 additions & 5 deletions apps/core/client/mutations/unassign-cart-from-customer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getSessionCustomerId } from '~/auth';

import { client } from '..';
import { graphql } from '../generated';
import { UnassignCartFromCustomerInput } from '../generated/graphql';
import { graphql } from '../graphql';

export const UNASSIGN_CART_FROM_CUSTOMER_MUTATION = /* GraphQL */ `
const UNASSIGN_CART_FROM_CUSTOMER_MUTATION = graphql(`
mutation UnassignCartFromCustomer(
$unassignCartFromCustomerInput: UnassignCartFromCustomerInput!
) {
Expand All @@ -16,16 +16,15 @@ export const UNASSIGN_CART_FROM_CUSTOMER_MUTATION = /* GraphQL */ `
}
}
}
`;
`);

export const unassignCartFromCustomer = async (
cartEntityId: UnassignCartFromCustomerInput['cartEntityId'],
) => {
const mutation = graphql(UNASSIGN_CART_FROM_CUSTOMER_MUTATION);
const customerId = await getSessionCustomerId();

const response = await client.fetch({
document: mutation,
document: UNASSIGN_CART_FROM_CUSTOMER_MUTATION,
variables: {
unassignCartFromCustomerInput: {
cartEntityId,
Expand Down
Loading

0 comments on commit ac1443f

Please sign in to comment.