Making BigCommerce GraphQL query with JS
While we recommend adding your GraphQL queries to stencil template front matter wherever possible. You can also make requests from JavaScript within your theme or in Script Manager.
See this example from BigCommerce:
fetch('/graphql', {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{ settings.storefront_api.token }}'
},
body: JSON.stringify({
query: `
query MyFirstQuery {
site {
settings {
storeName
}
products {
edges {
node {
name
sku
prices {
retailPrice {
value
currencyCode
}
price {
value
currencyCode
}
}
}
}
}
}
}
`
}),
})
.then(res => res.json())
.then(json => console.log(json));<br>