-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgraphql-test-script.html
More file actions
43 lines (38 loc) · 1.37 KB
/
graphql-test-script.html
File metadata and controls
43 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<script>
/*
Web Site Advantage: GraphQl Test
https://bigcommerce.websiteadvantage.com.au/
Copyright (C) 2023 Web Site Advantage
*/
var gqlQueryString = "query productById {site {product(entityId: {{product.id}}) {categories {edges {node {name path breadcrumbs(depth: 3) {edges {node {name}}} }}}}}}";
fetch('/graphql', {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{settings.storefront_api.token}}'
},
body: JSON.stringify({
query: gqlQueryString
})
})
.then(function (response) {
return response.json();
})
.then(function (result) {
console.log('graphql test', result);
// if a categories first breadcrumb is 'Brands' then build the URL from the categories path
result.data.site.product.categories.edges.forEach(function (category) {
var breadcrumbs = category.node.breadcrumbs.edges;
if (breadcrumbs[0].node.name == 'Brands') {
var path = category.node.path;
var brand = breadcrumbs[1].node.name;
console.log('brand', brand);
console.log('url', path);
}
});
})
.catch(function (error) {
console.error(error);
})
</script>