Skip to content

Instantly share code, notes, and snippets.

@Wurren
Last active October 29, 2021 18:40
Show Gist options
  • Save Wurren/72c7c83a1ef581994a8eeeb57cd3fce3 to your computer and use it in GitHub Desktop.
Save Wurren/72c7c83a1ef581994a8eeeb57cd3fce3 to your computer and use it in GitHub Desktop.
Example of using Graphql with Fetch
const queryFragment = `
query {
products(first: 10) {
edges {
node {
id
title
}
}
}
}
`;
function getGraphqlResponse() {
// replace shop_id with correct store and update token in headers
return fetch(`https://{shop_id}.myshopify.com/api/2021-10/graphql.json`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': 'TOKEN-GOES-HERE',
},
body: JSON.stringify({
query: queryFragment,
}),
})
.then((resp) => resp.json())
.catch((err) => console.log(err));
}
// example use
getGraphqlResponse().then(data => {
console.log(data);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment