Skip to content

Instantly share code, notes, and snippets.

@cgartner-redstage
Created June 4, 2020 12:24
Show Gist options
  • Save cgartner-redstage/84ee2c93ce033c06bae606046ab3d7bb to your computer and use it in GitHub Desktop.
Save cgartner-redstage/84ee2c93ce033c06bae606046ab3d7bb to your computer and use it in GitHub Desktop.
Get product info with GraphQL Bigcommerce
<script>
var jsContext = JSON.parse({{jsContext}});
var entities = jsContext.products.map(function (product) {
return product.id
});
fetch('/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{ settings.storefront_api.token }}'
},
body: JSON.stringify({
query: `
query SingleProduct {
site {
products (entityIds: [`+ entities.toString() +`]) {
edges {
node {
id
entityId
name
width {
...DimensionFields
}
height {
...DimensionFields
}
depth {
...DimensionFields
}
}
}
}
}
}
fragment DimensionFields on Measurement {
value
unit
}
`
}),
})
.then(res => res.json())
.then(json => console.log(json));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment