Skip to content

Commit

Permalink
feat(commerce): add Cart module and addToCart method
Browse files Browse the repository at this point in the history
  • Loading branch information
RivierGrullon committed Oct 1, 2023
1 parent 0340d9e commit 6822da4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Leads,
Inventory,
Agents,
Cart,
} from './modules';

import { setContext } from '@apollo/client/link/context';
Expand Down Expand Up @@ -76,6 +77,7 @@ export default class KanvasCore {
public leads: Leads;
public inventory: Inventory;
public agents: Agents;
public cart: Cart;

constructor(protected options: Options) {
this.client = new ApolloClient({
Expand All @@ -92,6 +94,7 @@ export default class KanvasCore {
this.leads = new Leads(this.client);
this.inventory = new Inventory(this.client);
this.agents = new Agents(this.client);
this.cart = new Cart(this.client);
}

protected generateURL() {
Expand Down
16 changes: 16 additions & 0 deletions src/modules/commerce/cart/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ClientType } from '../../../index';
import { ADD_TO_CART_MUTATION } from '../../../mutations';
import { CartItemData, CartItemInput } from '../../../types/commerce';

export class Cart {
constructor(protected client: ClientType) {}

public async addToCart(input: CartItemInput): Promise<CartItemData> {
const response = await this.client.mutate({
mutation: ADD_TO_CART_MUTATION,
variables: { input },
});

return response.data
}
}
1 change: 1 addition & 0 deletions src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./cart"
1 change: 1 addition & 0 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './locations';
export * from './leads';
export * from './inventory';
export * from './agents';
export * from "./commerce"
12 changes: 12 additions & 0 deletions src/mutations/commerce.mutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { gql } from '@apollo/client/core';

export const ADD_TO_CART_MUTATION = gql`
mutation($input: [CartItemInput!]!) {
addToCart(items: $input) {
id
name
price
quantity
}
}
`;
3 changes: 1 addition & 2 deletions src/mutations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ export * from './users.mutation';
export * from './custom-fields.mutation';
export * from './leads.mutation';
export * from './inventory.mutation';


export * from './commerce.mutation';
17 changes: 17 additions & 0 deletions src/types/commerce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface CartItemInput {
input: {
quantity: number;
variant_id: number;
}[];
}

export interface CartItemData {
data: {
addToCart: {
id: number;
name: string;
price: number;
quantity: number;
}[];
};
}

0 comments on commit 6822da4

Please sign in to comment.