Skip to content

Commit 508feeb

Browse files
committed
Simple GraphQL example using Apollo Client
1 parent a527e81 commit 508feeb

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

assets/js/theme/global.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import maintenanceMode from './global/maintenanceMode';
1313
import carousel from './common/carousel';
1414
import loadingProgressBar from './global/loading-progress-bar';
1515
import svgInjector from './global/svg-injector';
16+
import basicGraphQLQuery from './global/basicGraphQLQuery';
1617

1718
export default class Global extends PageManager {
1819
onReady() {
@@ -28,5 +29,7 @@ export default class Global extends PageManager {
2829
maintenanceMode(this.context.maintenanceMode);
2930
loadingProgressBar();
3031
svgInjector();
32+
/* Inject token into function */
33+
basicGraphQLQuery(this.context.storefrontAPIToken);
3134
}
3235
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import ApolloClient from 'apollo-boost';
2+
import gql from 'graphql-tag';
3+
4+
5+
/**
6+
* Run a basic request against the GraphQL Storefront API using Apollo Client and log the results to the browser console
7+
*/
8+
export default function (token) {
9+
const client = new ApolloClient({
10+
headers: { Authorization: `Bearer ${token}` },
11+
});
12+
13+
client.query({
14+
query: gql`
15+
query MyFirstQuery {
16+
site {
17+
settings {
18+
storeName
19+
}
20+
products {
21+
edges {
22+
node {
23+
name
24+
sku
25+
prices {
26+
retailPrice {
27+
value
28+
currencyCode
29+
}
30+
price {
31+
value
32+
currencyCode
33+
}
34+
}
35+
defaultImage {
36+
url(width:1280)
37+
}
38+
options {
39+
entityId
40+
displayName
41+
values {
42+
entityId
43+
label
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
51+
`,
52+
})
53+
.then(data => console.log(data))
54+
.catch(error => console.error(error));
55+
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
"dependencies": {
99
"@babel/polyfill": "^7.4.4",
1010
"@bigcommerce/stencil-utils": "^4.2.0",
11+
"apollo-boost": "^0.4.4",
1112
"creditcards": "^3.0.1",
1213
"easyzoom": "^2.5.2",
1314
"foundation-sites": "^5.5.3",
15+
"graphql": "^14.4.2",
16+
"graphql-tag": "^2.10.1",
1417
"jquery": "^3.4.1",
1518
"jstree": "vakata/jstree",
1619
"lazysizes": "5.1.0",

templates/layout/base.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
{{~inject 'secureBaseUrl' settings.secure_base_url}}
3535
{{~inject 'cartId' cart_id}}
3636
{{~inject 'template' template}}
37+
{{!-- Inject Storefront API Token into JS context so we can use it in our JavaScript --}}
38+
{{~inject 'storefrontAPIToken' settings.storefront_api.token}}
3739
</head>
3840
<body>
3941
<svg data-src="{{cdn 'img/icon-sprite.svg'}}" class="icons-svg-sprite"></svg>

0 commit comments

Comments
 (0)