Created
February 21, 2022 19:52
-
-
Save serifcolakel/0728a8ada332b6465a90f4564f5489db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const domain = process.env.SHOPIFY_STORE_DOMAIN; | |
const storeFrontAccessToken = process.env.SHOPIFY_STOREFRONT_ACCESTOKEN; | |
async function ShopifyData(query) { | |
const URL = `${domain}/api/2022-01/graphql.json`; | |
const options = { | |
endpoint: URL, | |
method: "POST", | |
headers: { | |
"X-Shopify-Storefront-Access-Token": storeFrontAccessToken, | |
"Accept": "application/json", | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify({ query }), | |
}; | |
try { | |
const data = await fetch(URL, options).then(response => { | |
return 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment