-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshopify.js
199 lines (174 loc) · 4.38 KB
/
shopify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const domain = process.env.SHOPIFY_STORE_DOMAIN
const storefontAccessToken = process.env.SHOPIFY_STOREFRONT_ACCESSTOKEN
async function ShopifyData(query) {
const URL = `https://${domain}/api/2021-07/graphql.json`
const options = {
endpoint: URL,
method: "POST",
headers: {
"X-Shopify-Storefront-Access-Token": storefontAccessToken,
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({ query })
}
try {
const data = await fetch(URL, options).then( response => response.json())
return data
} catch (error) {
throw new Error("Products not fetched")
}
}
export async function getProductsInCollection() {
const query = `
{
collectionByHandle(handle: "frontpage") {
title
products(first: 25){
edges{
node{
id
title
handle
priceRange {
minVariantPrice {
amount
}
}
images(first: 5){
edges{
node {
originalSrc
altText
}
}
}
}
}
}
}
}
`
const response = await ShopifyData(query)
const allProducts = response.data.collectionByHandle.products.edges ? response.data.collectionByHandle.products.edges : []
return allProducts
}
export async function getAllProducts() {
const query = `{
products(first: 25) {
edges{
node {
handle
id
}
}
}
}`
const response = await ShopifyData(query)
const slugs = response.data.products.edges ? response.data.products.edges : []
return slugs
}
export async function getProduct( handle ) {
const query = `{
productByHandle(handle: "${ handle }") {
collections(first: 1) {
edges {
node {
products(first: 4) {
edges {
node {
priceRange {
minVariantPrice {
amount
}
}
handle
title
id
images(first: 3) {
edges {
node {
originalSrc
altText
}
}
}
}
}
}
}
}
}
id
title
handle
description
images(first: 5) {
edges {
node {
originalSrc
altText
}
}
}
options{
name
values
id
}
variants(first: 25) {
edges{
node{
selectedOptions{
name
value
}
image{
originalSrc
altText
}
title
id
price
}
}
}
}
}`
const response = await ShopifyData(query)
const product = response.data.productByHandle ? response.data.productByHandle : []
return product
}
export async function createCheckout(id, quantity) {
const query = `mutation {
checkoutCreate(input: { lineItems: [{ variantId: "${id}", quantity: ${quantity} }] }) {
checkout {
id
webUrl
}
}
}
`
const response = await ShopifyData(query)
const checkout = response.data.checkoutCreate.checkout ? response.data.checkoutCreate.checkout : []
return checkout
}
export async function updateCheckout(checkoutId, lineItems){
const lineItemsObject = lineItems.map(item => {
return `{
variantId: "${item.id}",
quantity: ${item.variantQuantity},
}`
})
const query = `mutation {
checkoutLineItemsReplace(lineItems: [${lineItemsObject}], checkoutId: "${checkoutId}") {
checkout{
id
webUrl
}
}
}`
const response = await ShopifyData(query)
const checkout = response.data.checkoutLineItemsReplace.checkout ? response.data.checkoutLineItemsReplace.checkout : []
return checkout
}