Last active
October 29, 2021 18:40
-
-
Save Wurren/72c7c83a1ef581994a8eeeb57cd3fce3 to your computer and use it in GitHub Desktop.
Example of using Graphql with Fetch
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 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